Applying a symbol as a procedure

后端 未结 3 997
囚心锁ツ
囚心锁ツ 2021-01-21 19:32

Suppose I have a simple symbol:

> \'+
+

Is there any way I can apply that symbol as a procedure:

> ((do-something-with \'         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-01-21 19:58

    Lucas's answer is great. For untrusted input you can make a white list of allowed symbols/operators.

    (define do-something (lambda (op)
                           (cond
                             ((equal? op `+) +)
                             ((equal? op `-) -)
                             ((equal? op `*) *)
                             ((equal? op `/) /)
                             ((equal? op `^) ^))))
    
    ((do-something `+) 1 2)
    

提交回复
热议问题