Pergunta

i have this assignment to do, where i need to parse a wrong written recursive procedure, and fix it. for example: This:

(let ((fib (lambda (n) 
             (cond    ((= n 0) 1) 
                         ((= n 1) 1) 
                        (else (+ (fib (- n 1)) (fib (- n 2))))))))
           (fib n))

Transform to this:

(let ((fib (lambda (n fib-param) 
                    (cond ((= n 0) 1) 
                              ((= n 1) 1) 
                             (else (+ (fib-param (- n 1) fib-param)  
                                      (fib-param (- n 2) fib-param))))))) 
  (fib n fib)) 

The procedure is given as a quote with 3 parts: the "let" , the of the let, and the body. i want to parse the second part (meaning, i want to make a list that every term in it will be a single word from the expression of the "let") but i cant seem to work it out, no matter what i tried.

I'm using drRacket scheme.

Thanks and sorry for the long message.

Foi útil?

Solução

You may want to read: http://www.dreamsongs.com/Files/WhyOfY.pdf, which explains how to do this transformation. Your assignment is a classic programming languages technique for doing recursion with procedure application alone.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top