How to create a list of list of numbers in Haskell

前端 未结 2 950
隐瞒了意图╮
隐瞒了意图╮ 2021-01-25 12:58

I need to create a function that makes a board of size row and column and then populate that with zeros. mkBoard 2 3 would make [[0,0,0],[0,0,0]]

I don\'t really know w

2条回答
  •  后悔当初
    2021-01-25 13:43

    There is no need to use list comprehension here. We can make use of replicate :: Int -> a -> [a] that will convert an Int n and an a to a list that repeats that value n times.

    We thus can construct a list of three 0s with replicate 3 0. We can then use replicate a second time to construct a list that contains that list two times for example.

    mkBoard :: Num n => Int -> Int -> [[n]]
    mkBoard m n = replicate m (replicate n 0)
    

提交回复
热议问题