Haskell让表达式评估

时间:2019-04-16 19:24:59

标签: haskell stream expression evaluation let

我正在练习评估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]。有人可以一步一步解释为什么这是输出

2 个答案:

答案 0 :(得分:6)

这是分步说明:

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]

我们完成了。

答案 1 :(得分:1)

要了解发生了什么情况,我们会仔细命名每个实体的出现:

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相同,但在操作上却有很大不同。虽然后者是线性的,但前者是二次函数w.r.t。它的时间复杂度。

要了解实际含义,请比较以下时间:

> f 1.01 !! 4000
1.9297236994732192e17
(1.28 secs, 1614556912 bytes)

> iterate (* 1.01) 1 !! 4000
1.9297236994732192e17
(0.00 secs, 12990984 bytes)