Haskell list comprehension to map
I am new to hask开发者_如何学Goell . I was wondering If I can do the following thing using just map and concat ?
[ (x,y+z) | x<-[1..10], y<-[1..x], z<-[1..y] ]
Yes:
concat $ concat $ map (\x -> map (\y -> map (\z -> (x,y+z)) [1..y]) [1..x]) [1..10]
Although the official translation uses concatMap:
concatMap (\x -> concatMap (\y -> concatMap (\z -> [(x,y+z)]) [1..y]) [1..x]) [1..10]
精彩评论