1
votes

I am working through a project as a means to better understand Scheme. The goal of my scheme project is to take the input on the left of the following arrow and produce the output that is on the right side:

(* (+ 1 a) (+ 1 a))
 ==>
(let* ((g128572 (+ 1 a)))
  (* g128572 g128572))

To detail exactly what needs to happen within the program, a let* needs to be generated. The bindings of the let will be a series of randomly generated 'variable' strings which has a value associated with it. That value is derived from the left side of the input. Anything that appears more than once will appear within the let bindings. The body of the let will be the expression with the variable name inserted instead of the expression.

My question is the following:

Is it only possible to return it in the form that the following code returns?

(define (example)
  (cons 'let* (cons '((g128572 (+ 1 a)))
              (cons '(* g128572 g128572)
                    '()))))

Where the return value is '(let* ((g128572 (+ 1 a))) (* g128572 g128572)), which is technically a list and would need to be in the form (eval '(let* ((g128572 (+ 1 a))) (* g128572 g128572))) in order to actually evaluate, or can I actually return the function itself, where all I would need to have is (let* ((g128572 (+ 1 a))) (* g128572 g128572)), which would successfully run and evaluate by itself?

Hopefully I am clear in what I am asking.

1
Did you mean to write (+ a 1) and (+ 1 a) in your example?rajashekar
Oh whoops. It should all be the same expression.Wesley Brandt

1 Answers

0
votes

In mit-scheme there is no unique variable-name generator. You must implement your own.

What you want to do actually is named converting the code in CPS. For this you do not need much philosophy, it is quite simple to do.

Just google and see how to implement CPS conversion using lambda-generators, not let*. The book of Friedman or the course of Krishnamurti is enough to learn to do it.