所以我有这行代码:
(foldl cons '() '(1 2 3 4))
运行时得到的输出是这个:
'(4 3 2 1)
您能向我解释为什么我没有得到’(1 2 3 4)
吗?
我阅读了文档,但是对于foldl的工作方式仍然有些困惑。另外,如果我想定义foldl,如何在球拍中指定该函数可以将可变数量的列表作为参数?
谢谢!
是。根据左折的定义,
(foldl cons '() '(1 2 3))
=
(foldl cons (cons 1 '()) '(2 3))
=
(foldl cons (cons 2 (cons 1 '())) '(3))
=
(foldl cons (cons 3 (cons 2 (cons 1 '()))) '())
=
(cons 3 (cons 2 (cons 1 '())))