I need help converting between the let form and un

2019-07-30 01:37发布

问题:

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