开发者

Building a '2d' Haskell list

开发者 https://www.devze.com 2023-04-08 21:43 出处:网络
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) f

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号