Haskell 中特定列表理解的解释
Explanation of specific list comprehension in Haskell
我有一个关于列表理解的问题
[(x,y)| x<-[1..2], y<-[x..3], let z = x+y, odd z]
为什么这个计算结果为:
[(1,2),(2,3)]
?
z 要去哪里?
谢谢
您的谓词是 "z = x + y for all z odd"。如果你"unroll"流量:
z = 谓词,y(x) 所以:
x = 1,2
y (1) = 1,2,3
y (2) = 2,3
基于谓词过滤的值的组合:
x+y <= filter(z)
1+1 = 2 NO
1+2 = 3 OK
1+3 = 4 NO
2+2 = 4 NO
2+3 = 5 OK
所以正确的答案是 x = 1 and y = 2 and x = 2 and y =3 => [(1,2), (2,3)]
我有一个关于列表理解的问题
[(x,y)| x<-[1..2], y<-[x..3], let z = x+y, odd z]
为什么这个计算结果为:
[(1,2),(2,3)]
?
z 要去哪里?
谢谢
您的谓词是 "z = x + y for all z odd"。如果你"unroll"流量:
z = 谓词,y(x) 所以:
x = 1,2
y (1) = 1,2,3
y (2) = 2,3
基于谓词过滤的值的组合:
x+y <= filter(z)
1+1 = 2 NO
1+2 = 3 OK
1+3 = 4 NO
2+2 = 4 NO
2+3 = 5 OK
所以正确的答案是 x = 1 and y = 2 and x = 2 and y =3 => [(1,2), (2,3)]