折
Fold Right 连续将一个双参数函数从左到右应用于列表中的每个元素,从基值开始:
foldr: (a b -> b) b (listof a) -> b
> (foldr + 0 (list 1 2 3 4))
10
> (foldr string-append "" (list "h" "e" "l" "l" "o"))
"hello"
> (foldr cons empty (list 1 2 3 4))
(list 1 2 3 4)
向左折叠在相反方向执行相同的操作:
foldl: (a b -> b) b (listof a) -> b
> (foldl + 0 (list 1 2 3 4)
10
> (foldl string-append "" (list "h" "e" "l" "l" "o"))
"olleh"
> (foldl cons empty (list 1 2 3 4))
(list 4 3 2 1)