折
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)