Haskell让表达式评估

问题描述 投票:2回答:3

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

haskell stream expression evaluation let
3个回答
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
投票

为了了解发生了什么,我们会仔细地命名每个实体:

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)
© www.soinside.com 2019 - 2024. All rights reserved.