I need help converting between the let form and unnamed procedure form

|▌冷眼眸甩不掉的悲伤 提交于 2020-02-05 04:54:58

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10018646/i-need-help-converting-between-the-let-form-and-unnamed-procedure-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!