Operations using list elements

最后都变了- 提交于 2019-12-11 14:03:30

问题


I have a list in this format, (+ 2 3). Where the first character is a math symbol that can be applied to the other two elements. I cannot seem to get it to do the operations. I want to return 5 for the previous example.

I've tried this:

((car '(+ 2 3)) (cadr '(+ 2 3)) (caddr '(+ 2 3)))

But I get the following error:

application: not a procedure.


回答1:


You can try eval , should do it straight away:

> (eval '(+ 1 2))
3

If you'd like to have more control over the input, write a funcion:

(define solver
     (lambda (exp_lst)
             (let ((op (car exp_lst))
                   (vars (cdr exp_lst)))
              ...do/check stuff...
             (apply (eval op) vars)
)))


来源:https://stackoverflow.com/questions/48558746/operations-using-list-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!