∶
/ \
(1 ∶
/ \
(2 ∶
/ \
(3 ∶
/ \
(4
‥
/ \
‥ (4 / \
‥ (3 / \
‥ (2
/ \
[ ] (1
∶
/ \
(4 ∶
/ \
(3 ∶
/ \
(2 ∶
/ \
(1
(4∶((3∶(2∶(1∶ )
∶
/ \
(1 ∶
/ \
(2 ∶
/ \
(3 ∶
/ \
(4
(1:((2:(3:(4: )
(( ‥(1‥(2)‥(3)‥(4
var acc = [ ]
foreach(x in xs)
acc = acc ‥x)
return acc
./012∶ (3
./010‥ (3
(3=[(1,(2, (3, (4]
!"#$% ∶ =)$*+,),-= !"#$%λ..λ-..∶-
!"#$#‥ =%*0*%1*= !"#$# λ..λ-.-∶.
!"#$% ∷3 →5 →5→5→3→5
!"#$% ! * =*
!"#$% ! *.:.1 =! .!"#$% ! * .1
!"#$# ∷5 →3 →5→5→3→5
!"#$# ! * =*
!"#$# ! *.:.1=!"#$# !! * . .1
∶ =λ..λ-..∶-
‥=λ..λ-.-∶.
‥=!#)7 ∶
8ℎ*%* !#)7 ! . -=! - .
∶
/ \
ℎ 7
..
/ \
7 ℎ
equivalent to
[(4,(3, (2, (1]
91:;797<
2:=:23:
[(1,(2, (3, (4]
> id = foldr (:) []
> rev = foldl (flip (:)) []
> id [1,2,3,4]
[1,2,3,4]
> rev [1,2,3,4]
[4,3,2,1]
> id = foldr (\x y -> x:y) []
> rev = foldl (\x y -> y:x) []
> id [1,2,3,4]
[1,2,3,4]
> rev [1,2,3,4]
[4,3,2,1]
56789:6:
∶ =>?ℎ∶
=>?ℎ
56789:6:
∶ =>?ℎ A
=>?ℎ 6
var acc = e
foreach(x in xs)
acc = f (acc, x)
return acc
folding a list right and left using Cons and Nil results in the identity and reverse functions