关于球拍中的折叠功能的问题。 (功能编程)

问题描述 投票:1回答:1

所以我有这行代码:

(foldl cons '() '(1 2 3 4))

运行时得到的输出是这个:

'(4 3 2 1)

您能向我解释为什么我没有得到’(1 2 3 4)吗?

我阅读了文档,但是对于foldl的工作方式仍然有些困惑。另外,如果我想定义foldl,如何在球拍中指定该函数可以将可变数量的列表作为参数?

谢谢!

functional-programming racket fold purely-functional foldleft
1个回答
0
投票

是。根据左折的定义,

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