0

so I'm trying to experiment with some code and change between the two scheme expression methods of let and lambda.

The code I have is as follows:

(let splice ((l '()) (m (car s)) (r (cdr s)))
        (append
          (map (lambda (x) (cons m x)) (perm (append l r)))
          (if (null? r) '()
        (splice (cons m l) (car r) (cdr r)))))

I'm trying to change the outermost let definitions to lambda format, but it's a bit confusing due to the nested nature of the code. What I have attempted to do so far is:

(lambda (splice (l m r))
        (append
            (map (lambda (x) (cons m x)) (perm (append l r)))
            (if (null? r) '()
    (cut (cons m l) (car r) (cdr r)))))
(('()) (car upList) (cdr upList))

This is clearly wrong, but I don't know how to proceed further...

  • You’re almost right: since lambdas do not have names, your args list is wrong (it should be `(lambda (l m r) ...)`), and you need to assign it to a binding, probably with `letrec`. Then you want to invoke it using that name, passing it the initial arguments. – Alexis King Apr 01 '16 at 16:34
  • Thanks, I'll try that out! What about the inner block such as `(splice (cons m l) (car r) (cdr r))` where the let definitions are used recursively? Will those work with the binded lambda or will that need to be modified further? – Starscreen60 Apr 01 '16 at 16:48

1 Answers1

0

I wrote a post about how let is transformed into lambda behind the scenes, which you may find helpful.

According to the expansion described in my post, your code would expand into:

((rec (splice l m r)
   (append (map (lambda (x) (cons m x)) (perm (append l r)))
           (if (null? r)
               '()
               (splice (cons m l) (car r) (cdr r)))))
 '() (car s) (cdr s))

which then expands to:

((letrec ((splice (lambda (l m r)
                    (append (map (lambda (x) (cons m x)) (perm (append l r)))
                            (if (null? r)
                                '()
                                (splice (cons m l) (car r) (cdr r)))))))
   splice)
 '() (car s) (cdr s))
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • Thank you for clarifying the way the expansion is handled. Thank you also @AlexisKing for clarifying my doubts about the transformation syntax :) – Starscreen60 Apr 01 '16 at 19:16