Scheme using lambda

I have code that uses multiple lambdas. It basically returns a number that is in the middle.

(define foo (lambda (x) (letrec ((h (lambda (yz) (cond ((null? y) 'undefined) ((null? (cdr y)) (car z)) (else (h (cddr y) (cdr z))))))) ((lambda (y) (hyy)) x)))) 

I need to rewrite the code so that it does not use any lambda. How to do it?

+4
source share
2 answers

It depends on the context and what you have been taught. If you place more context, you are likely to get a better answer.

However, first of all, note that

 (define (foo x) (+ x 1)) 

equivalently

 (define foo (lambda (x) (+ x 1))). 

To get rid of letrec rewrite it in an internal definition.

+2
source

To remove all shortcuts in an expression, you can do this:

  • Replace the procedure definition from this form: (define f (lambda (x) x)) with this equivalent form: (define (fx) x)
  • Replace letrec expression with internal definition
  • Replace lambda in the last line with another internal definition, name it and name it at the end
  • Even simpler: note that you really don't need the latest lambda , as this is equivalent to a direct call (hxx)

After successive application of each of the above substitutions, the procedure ends as follows:

 (define (foo x) (define (hyz) (cond ((null? y) 'undefined) ((null? (cdr y)) (car z)) (else (h (cddr y) (cdr z))))) (hxx)) 

Please note that lambda not actually eliminated, they are still under the hood - hidden behind syntactic sugar.

+4
source

Source: https://habr.com/ru/post/1415695/


All Articles