gpt4 book ai didi

scheme - 初学者 : Curried functions in Scheme

转载 作者:行者123 更新时间:2023-12-03 18:21:31 24 4
gpt4 key购买 nike

我正在使用 SICP 讲座和文本来自己了解 Scheme。我正在看一个练习,上面写着“表达式 E 的应用程序是 (E E1,...En) 形式的表达式。这包括 n=0 的情况,对应于表达式 (E)。Curried 应用程序E 的应用要么是 E 的应用,要么是 E 的 Curried 应用的应用。”

(编辑:我更正了上面的引用......我最初错误地引用了定义。)

任务是定义计算为 3 的过程的 Curried 应用程序

(define foo1
(lambda (x)
(* x x)))

我真的不明白这里的想法,阅读关于 Curriying 的维基百科条目并没有真正帮助。

任何人都可以帮助更清楚地解释这里的要求吗?

实际上,即使给我这个问题的答案也会有所帮助,因为在这个问题之后还有五个要解决。 ...我只是没有得到基本的想法。

补充:即使在布赖恩坎贝尔冗长的解释之后,我仍然有些迷茫。

(foo1 (sqrt 3)))被认为是 foo 的应用程序,因此是 foo 的柯里化(Currying)应用程序?

看起来太简单了,但也许...

打字 (((foo1 2 )) 2)进入 DrScheme 给出以下错误(我有点预料到)
procedure application: expected procedure, given: 4 (no arguments)

重读后 What is Currying?我知道我也可以将 foo1 重新定义为:
(define (foo1 a)
(lambda (b)
(* a b)))

那么我可以输入
((foo1 3 ) 4)

12



但这并没有真正让我更接近于产生 3 作为输出,而且看起来这并不是真正对原始 foo1 进行柯里化(Currying),它只是重新定义它。

该死,20 年的 C 编程并没有让我为此做好准备。 :-) :-)

最佳答案

嗯,与通常更清晰的书籍风格相比,这个问题的措辞相当困惑。实际上,如果您从 here 获取问题集,您可能错误地引用了问题集。 ;这可能会导致您的困惑。

我将为您分解定义,并提供一些示例可以帮助您弄清楚发生了什么。

An application of an expression E is an expression of the form (E E1 ... En).



这是一个应用程序的示例:
(foo 1 2)      ; This is an application of foo
(bar 1) ; This is an application of bar

This includes the case n=0, corresponding to an expression (E).


(baz)          ; This is an application of baz

A Curried application of E is either an application of E or an application of a Curried application of E�...........



这是您引用错误的那个;以上是我在网上找到的问题集的定义。

这个定义有两个部分。从第一个开始:

A Curried application of E is either an application of E


(foo 1 2)       ; (1) A Curried application of foo, since it is an application of foo
(bar 1) ; (2) A Curried application of bar, since it is an application of bar
(baz) ; (3) A Curried application of baz, since it is an application of baz

or an application of a Curried application of E


((foo 1 2) 3)   ; (4) A Curried application of foo, since it is an application of (1)
((bar 1)) ; (5) A Curried application of bar, since it is an application of (2)
((baz) 1 2) ; (6) A Curried application of baz, since it is an application of (3)
(((foo 1 2) 3)) ; A Curried application of foo, since it is an application of (4)
(((bar 1)) 2) ; A Curried application of bar, since it is an application of (5)
; etc...

这是否为您提供了入门所需的帮助?

编辑 :是的, (foo1 (sqrt 3))foo1 的 Curried 应用程序;就是这么简单。这不是一个很好的问题,因为在许多实现中,您实际上会得到 2.9999999999999996 或类似的东西;不可能有一个准确返回 3 的值,除非您的 Scheme 有某种精确的 algebraic numbers 表示形式。 .

你的第二个例子确实是一个错误。 foo1返回一个整数,该整数无效。只有后面的一些例子,函数的应用程序的递归情况是有效的。看看 foo3 , 例如。

编辑 2 : 我刚查了SICP,看来这里的概念直到1.3节才解释,而这个作业只提到了1.1节。如果您还没有,我建议您尝试通读第 1.3 节。

关于scheme - 初学者 : Curried functions in Scheme,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/694162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com