为什么以下两个表达式有不同的结果
Why the following two expressions have different results
fibs = 1:1:[x+y|x <- fibs, y <- tail fibs]
returns
[1,1,2,3,4,5,6,7,8,9]
fibs = 1:1:[x+y|(x, y) <- zip fibs (tail fibs)]
returns
[1,1,2,3,5,8,13,21,34,55...]
第一个是评估 fibs
和 tail fibs
的笛卡尔积(即每个组合),而第二个是评估成对配对。
Prelude> [(x,y) | x <- [1,2,3], y <- [4,5,6]]
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]
Prelude> [(x,y) | (x,y) <- [1,2,3] `zip` [4,5,6]]
[(1,4),(2,5),(3,6)]
fibs = 1:1:[x+y|x <- fibs, y <- tail fibs]
returns
[1,1,2,3,4,5,6,7,8,9]
fibs = 1:1:[x+y|(x, y) <- zip fibs (tail fibs)]
returns
[1,1,2,3,5,8,13,21,34,55...]
第一个是评估 fibs
和 tail fibs
的笛卡尔积(即每个组合),而第二个是评估成对配对。
Prelude> [(x,y) | x <- [1,2,3], y <- [4,5,6]]
[(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]
Prelude> [(x,y) | (x,y) <- [1,2,3] `zip` [4,5,6]]
[(1,4),(2,5),(3,6)]