0
votes

I've been working on this procedure in Scheme (specifically Pretty Big) and I've hit a brick wall. What I'm trying to do is create a procedure that can create other procedures or functions. For the most part I think I'm on the correct track.

(define (function-maker function-specs)

  (let* [(specs (map cdr function-specs))
         (name (caar specs))
         (args (cadr function-specs))
         (body (cdr(cdr function-specs)))]

    (eval(list 'define name
          (list 'lambda args body)))))

Current Output:

Above is the current output and I'll explain to the best of my knowledge what is happening. The input takes in three arguments essentially. A name, to name the function. Arguments for the function and finally the body of the function.

The three arguments are then listed together and then it's supposed to create a new function. The picture above shows the error that I'm reaching. Thanks for the help!

1

1 Answers

0
votes

To see what's happening, get rid of the eval and examine the list:

(define (function-maker function-specs)

  (let* [(specs (map cdr function-specs))
         (name (caar specs))
         (args (cadr function-specs))
         (body (cdr(cdr function-specs)))]

    (list 'define name
          (list 'lambda args body))))

> (function-maker '((name: add5) (x) (+ x 5)))
'(define add5 (lambda (x) ((+ x 5))))

As you can see, there are too many parentheses in the body, so you're attempting to use the resulting number as a procedure.

You want

(body (car (cdr (cdr function-specs))))

or

(body (caddr function-specs))