So I’m new to Scheme. I’m trying to make a function that defines global functions using a specification of the form ((name: name) (args: args) (body: body)) so for example
(fn-maker '((name: mult5) (x) (* x 5)))
would make it so globally I could call
(mult5 3)
and get 15.
Here’s what I have so far:
(define (fn-maker fn-spec)
(let* (spec (map cdr fn-spec))
(name (caar spec))
(args (cadr fn-spec))
(body (caaddr (cdaddr fn-spec))))
(lambda (args)
body)))
The main thing I’m confused on at the moment is how to make lambda use those args. As it stands, lambda creates a new local variable named “args” instead of evaluating the list that is behind args. Is there a way around this? My current thought process is that I should be using some form of let across the list provided by args but I’m not sure what that would look like or even how to go about structuring it.
This is homework so I’m most definitely not looking for code (cheating and all that) but rather a point in the right direction and some critique. Thank you.
Update: To anyone who happens upon this in the future, it is possible to do this code very simply using some clever quoting. No macros needed. Also, as it turns out, eval in Pretty Big evaluates in the global by default.
You need a way to “export” a function definition created inside a procedure, in such a way that the definition exists outside, on the global environment. Your implementation so far will simply return a
lambdaform. Hint: there’s a way to do this (I remember seeing it in Racket), but it might be specific to the Scheme interpreter in use; and I’m not sure if it’s available in the Pretty Big language.As a side note – if the
fn-specparameter is going to receive a list with the name and the body of the new function, then make sure that only a single parameter is passed when callingfn-maker, something like this:Also, consider using a macro instead of a plain procedure…