P. Lance
P. Lance

Reputation: 189

Define functions based on procedures stored in list

I'm new to Scheme and functional programming in general and I'm just starting to write something more complicated. I was wondering if it was possible to define functions based on procedures that are stored in a list.

Let's say we have the following list of other lists that represent procedures (+ 2 3) and (* 3 4):

'((+ 2 3) (* 3 4))

Now I want to write something that would take in any list representing procedure (for example (+ 2 3)) and define a function based on it that I can use later on.

So if I were to pick (+ 2 3), I'd want the following function to be declared:

(define (funct_name) (+ 2 3))

So basically, what I'm looking for is a function that would define other functions and it must be written in the script. So perhaps something like this?

(define (def_functions_fromlist) ls) ;this would define the function  
;corresponding to procedure in ls 

Thank you in advance and I'm sorry if I seem too confused here.

Upvotes: 0

Views: 47

Answers (1)

Unlocked
Unlocked

Reputation: 648

Return an anonymous function that evaluates the list, as so:

(define (def-prod-from ls)
    (lambda () (eval ls)))

This will work for any input, not just lists. That could be an issue, but probably isn't.

Upvotes: 1

Related Questions