1
votes

I will describe my problem on example.

I'll get (play '(left nothing right left)). Some of the names in the list are real procedures, others i want to skip.

(define (left)
   'left
)

I need to interpret procedures with names in the list. What is the solution?

When I try ( (car '(left nothing right left))) I get error : procedure application: expected procedure, given: left (no arguments)

2

2 Answers

4
votes

(car '(left nothing right left)) evaluates to the symbol left, which is the name of a procedure, but not actually a procedure, so you can't call it.

You'll want to build an association list mapping symbols to procedures:

(define actions `((left . ,(lambda () 'left))
                  (right . ,(lambda () 'right))
                  (nothing . ,(lambda () (display "I'm staying put")))))

then you can call the appropriate function for the first element in your list as

((cdr (assoc (car '(left nothing right left)) actions)))
0
votes

You can also use quasiquoting to construct a list containing a mixture of symbols you want evaluated and others you don't, e.g.

(play `(,left nothing nothing ,right nothing))

left and right will expand to whatever you've defined them as (such as a procedure) while nothing is not un-quoted so it will be left as a symbol. play would then have to test each member to see if it's a procedure, something like:

(define (play xs)(for-each (lambda (x)(if (procedure? x)(x) x)) xs))