2
votes

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.

1
s/let/letrec/ ;p Hint: This is the Y combinator. - leppie
Will you forgive me if i say i didn't understand your answer? :D I'm kinda new to scheme... - matmiz
It is not an answer, it is a comment :) The first part was joke (albeit a working solution, to just replace let with letrec). - leppie
i would love to do it, but it's for a course in the university and the output should be exactly as they asked,just like the example i gave you. - matmiz

1 Answers

1
votes

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.