How to express {2n+3m+1|n,m∈N} in list comprehension form? (N is the set of natural numbers including 0)

前端 未结 6 1146
北海茫月
北海茫月 2021-01-14 08:12

How do I express {2n+3m+1|n,m∈N} in list comprehension form? N is the set of natural numbers, including 0.

6条回答
  •  天命终不由人
    2021-01-14 08:35

    my 0.2:

    trans = concat [ f n | n <- [1..]]
     where 
      mklst x = (\(a,b) -> a++b).unzip.(take x).repeat
      f n | n `mod` 2 == 0 = r:(mklst n (u,l))
          | otherwise      = u:(mklst n (r,d))
      u = \(a,b)->(a,b+1)
      d = \(a,b)->(a,b-1)
      l = \(a,b)->(a-1,b)
      r = \(a,b)->(a+1,b)
    
    mkpairs acc (f:fs) = acc':mkpairs acc' fs
                      where acc' = f acc
    allpairs = (0,0):mkpairs (0,0) trans          
    result = [2*n + 3*m + 1 | (n,m) <- allpairs]
    

提交回复
热议问题