我需要在let表单和未命名的过程表单之间进行转换 [英] I need help converting between the let form and unnamed procedure form
问题描述
let程序是这样的。
(define max-recursive (m1(m1))
(m1(car lst))
(m1(m1(max)递归(cdr lst))))
(if(> m0 m1)
m0
m1
)
)
))
以及我到目前为止所做的是这个
<$ (lambda(lst)
(if(null?(cdr lst))
(car lst)
((lambda(lst) m0 m1)
(if(> m0 m1)
m0
m1
)
)
car lst(max-recursive(cdr lst)) )
)))
任何帮助将不胜感激谢谢。
你几乎明白了!在表达式 car lst
周围只有几个括号丢失。试试这个:
(define max-recursive
(lambda(lst)
(if(null? (cdr lst))
(car lst)
((lambda(m0 m1)
(if(> m0 m1)
m0
m1))
(car lst);这里是错误
(max-recursive(cdr lst)))))
解释如下。 let
形式如下:
(let((x 10 ))
x)
...只是 lambda
表达式应用于某些参数。以前的 let
等价于:
((lambda(x )
x)
10)
请注意,在两种情况下,值 10
结束绑定到一个名为 x
的变量,并且 let
表达式与 lambda
表达式的主体相同
im trying to convert from a let-form to an unamed procedure form and i just can't get the hang of it.
the let procedure is this.
(define max-recursive (lambda (lst)
(if (null? (cdr lst))
(car lst)
(let ((m0 (car lst))
(m1 (max-recursive (cdr lst))))
(if (> m0 m1)
m0
m1
)
)
)))
and what i've done so far is this
(define max-recursive (lambda (lst)
(if (null? (cdr lst))
(car lst)
((lambda (m0 m1)
(if (> m0 m1)
m0
m1
)
)
car lst (max-recursive (cdr lst)))
)))
any help would be appreciated thank you.
You almost got it! only a couple of parenthesis were missing around the expression car lst
. Try this:
(define max-recursive
(lambda (lst)
(if (null? (cdr lst))
(car lst)
((lambda (m0 m1)
(if (> m0 m1)
m0
m1))
(car lst) ;here was the error
(max-recursive (cdr lst))))))
The explanation is as follows. A let
form like this one:
(let ((x 10))
x)
... is just syntactic sugar for a lambda
expression applied to some parameters. The previous let
is equivalent to this:
((lambda (x)
x)
10)
Notice that in both cases the value 10
ends up bound to a variable named x
, and the body of the let
expression is the same as the body of the lambda
expression
这篇关于我需要在let表单和未命名的过程表单之间进行转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!