Building a '2d' Haskell list
Let testFn be a function that takes two Ints.
I am trying to write a function that builds an N x M two-di开发者_如何转开发mensional table (a list of lists) containing all the values of (testFn i j) for i < N and j < M.
How can this be done?
A list comprehension would be the easiest:
[[testFn i j | j <- [0..(m-1)]] | i <- [0..(n-1)]]
[ [testFn i j | j<-[0..(M-1)] ] | i<-[0..(N-1)] ]
where M
and N
are preprocessor macros to be replaced for the actual variable names (which must begin with a lowercase letter) in the process of making the code valid Haskell.
精彩评论