I’m creating a program that allow a user introduce a lambda function only writing it in the interface writing box like this:
(lambda (p q) (and (not p) q))
This is the result “(lambda (p q) (and (not p) q))” because scheme convert it to a string instead of a procedure that can be evaluated.
How can i convert this string into a procedure?
In Scheme, the
evalfunction only operates on lists of symbols, so this would work fine:But this would give an error:
To convert a string from the UI into a list of symbols (AKA an S-expression), you’ll need to call the
readfunction. Regularly,readonly operates on “ports” (file stream), so you’ll need to convert your string into an input port before it’s usable withread:All that’s left is to evaluate the string, and call the lambda that you get as a result:
Here’s how you can now calculate the number 3:
You find the APIs for
readandopen-input-stringat the R5RS and SRFI websites.