我正在做评估 let 表达式的练习题,但我不明白这个表达式的输出。
这是表达式:
let a = 2
b = 1:[i * 2 | i <- b]
f a = 1:[i * a | i <- (f a)]
in take (a+2) (f (head (tail b) ))
输出应该是 [1,2,4,8]。有人可以一步一步地解释为什么这是输出吗
步说明:
let a = 2
b = 1:[i * 2 | i <- b]
f a = 1:[i * a | i <- (f a)]
in take (a+2) (f (head (tail b) ))
那里有两个不同的变量称为a
,一个阴影另一个,所以首先让我们重命名其中一个以避免意外混淆它们:
let outer_a = 2
b = 1:[i * 2 | i <- b]
f a = 1:[i * a | i <- (f a)]
in take (outer_a+2) (f (head (tail b) ))
现在我们可以代入outer_a
并评估+
:
let b = 1:[i * 2 | i <- b]
f a = 1:[i * a | i <- (f a)]
in take 4 (f (head (tail b) ))
根据map
重写列表推导:
let b = 1:map (* 2) b
f a = 1:map (* a) (f a)
in take 4 (f (head (tail b) ))
使用 iterate
而不是显式递归:
let b = iterate (* 2) 1
f a = iterate (* a) 1
in take 4 (f (head (tail b) ))
评估b
的前两个步骤:
let b = 1:2:iterate (* 2) 4
f a = iterate (* a) 1
in take 4 (f (head (tail b) ))
b
替代:
let f a = iterate (* a) 1
in take 4 (f (head (tail (1:2:iterate (* 2) 4)) ))
评估tail
:
let f a = iterate (* a) 1
in take 4 (f (head (2:iterate (* 2) 4) ))
评估head
:
let f a = iterate (* a) 1
in take 4 (f 2)
f a
替代:
take 4 (iterate (* 2) 1)
评估iterate
几次:
take 4 (1:2:4:8:iterate (* 2) 16)
评估take
:
[1,2,4,8]
我们完成了。
为了了解发生了什么,我们仔细命名每个实体:
let a = 2
b = 1 : [i * 2 | i <- b]
f a = 1 : [i * a | i <- f a]
in take (a+2) (f (head (tail b)))
==
let b = (b1:bs1)
(b1:bs1) = 1 : [i * 2 | i <- b]
in take 4 (f (head (tail b)))
==
let b1 = 1
bs1 = [i * 2 | i <- (b1:bs1)]
in take 4 (f (head bs1))
==
let b1 = 1
bs1 = [i * 2 | i <- [b1]] ++ [i * 2 | i <- bs1]
in take 4 (f (head bs1))
==
let bs1 = [i * 2 | i <- [1]] ++ [i * 2 | i <- bs1]
in take 4 (f (head bs1))
==
let bs1 = (b2:bs2)
(b2:bs2) = [1 * 2] ++ [i * 2 | i <- bs1]
in take 4 (f b2)
==
let (b2:bs2) = 2 : [i * 2 | i <- (b2:bs2)]
in take 4 (f b2)
==
let bs2 = [i * 2 | i <- (2:bs2)]
f a = 1 : [i * a | i <- f a] -- same as before
in take 4 (f 2)
==
let xs = f 2
f 2 = 1 : [i * 2 | i <- f 2]
in take 4 xs
==
let (x1:xs1) = 1 : [i * 2 | i <- f 2]
in take 4 (x1:xs1)
==
let xs1 = [i * 2 | i <- f 2]
in take 4 (1:xs1)
==
let xs1 = [i * 2 | i <- f 2]
in 1 : take 3 xs1
==
let (x2:xs2) = [i * 2 | i <- (y1:ys1)]
(y1:ys1) = 1 : [i * 2 | i <- f 2]
in 1 : take 3 (x2:xs2)
==
let (x2:xs2) = [i * 2 | i <- (1:ys1)]
ys1 = [i * 2 | i <- f 2]
in 1 : take 3 (x2:xs2)
==
let (x2:xs2) = 2 : [i * 2 | i <- ys1]
ys1 = [i * 2 | i <- f 2]
in 1 : take 3 (x2:xs2)
==
let xs2 = [i * 2 | i <- ys1]
ys1 = [i * 2 | i <- f 2]
in 1 : take 3 (2:xs2)
==
let xs2 = [i * 2 | i <- ys1]
ys1 = [i * 2 | i <- f 2]
in 1 : 2 : take 2 xs2
==
let (x3:xs3) = [i * 2 | i <- (y2:ys2)]
(y2:ys2) = [i * 2 | i <- (z1:zs1)]
(z1:zs1) = 1 : [i * 2 | i <- f 2]
in 1 : 2 : take 2 (x3:xs3)
==
let (x3:xs3) = [i * 2 | i <- (y2:ys2)]
(y2:ys2) = 2 : [i * 2 | i <- zs1]
zs1 = [i * 2 | i <- f 2]
in 1 : 2 : take 2 (x3:xs3)
==
let (x3:xs3) = 4 : [i * 2 | i <- ys2]
ys2 = [i * 2 | i <- zs1]
zs1 = [i * 2 | i <- f 2]
in 1 : 2 : take 2 (x3:xs3)
==
let xs3 = [i * 2 | i <- ys2]
ys2 = [i * 2 | i <- zs1]
zs1 = [i * 2 | i <- f 2]
in 1 : 2 : 4 : take 1 xs3
==
let (x4:xs4) = [i * 2 | i <- (y3:ys3)]
(y3:ys3) = [i * 2 | i <- (z2:zs2)]
(z2:zs2) = [i * 2 | i <- (w1:ws1)]
(w1:ws1) = 1 : [i * 2 | i <- f 2]
in 1 : 2 : 4 : take 1 (x4:xs4)
==
let (x4:xs4) = [i * 2 | i <- (y3:ys3)]
(y3:ys3) = [i * 2 | i <- (z2:zs2)]
(z2:zs2) = 2 : [i * 2 | i <- ws1]
ws1 = [i * 2 | i <- f 2]
in 1 : 2 : 4 : take 1 (x4:xs4)
==
let (x4:xs4) = [i * 2 | i <- (y3:ys3)]
(y3:ys3) = 4 : [i * 2 | i <- zs2]
zs2 = [i * 2 | i <- ws1]
ws1 = [i * 2 | i <- f 2]
in 1 : 2 : 4 : take 1 (x4:xs4)
==
let (x4:xs4) = 8 : [i * 2 | i <- ys3]
ys3 = [i * 2 | i <- zs2]
zs2 = [i * 2 | i <- ws1]
ws1 = [i * 2 | i <- f 2]
in 1 : 2 : 4 : take 1 (x4:xs4)
==
1 : 2 : 4 : 8 : take 0 xs4
==
1 : 2 : 4 : 8 : []
在上面的推导中,我们使用了列表推导的属性,其中
[ ... | ... <- (xs ++ ys)]
===
[ ... | ... <- xs ] ++ [ ... | ... <- ys]
因此
[ ... | ... <- (x : ys)]
===
[ ... | ... <- [x] ] ++ [ ... | ... <- ys]
f a
产生与iterate (* a) 1
相同的结果,但在操作上却大不相同。后者是线性的,而前者是二次函数,即时间复杂度。
要了解它在实践中的含义,请比较以下时间:
> f 1.01 !! 4000
1.9297236994732192e17
(1.28 secs, 1614556912 bytes)
> iterate (* 1.01) 1 !! 4000
1.9297236994732192e17
(0.00 secs, 12990984 bytes)