使用列表元素的操作 [英] Operations using list elements
本文介绍了使用列表元素的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这种格式的列表,(+ 2 3).其中第一个字符是可以应用于其他两个元素的数学符号.我似乎无法让它进行操作.我想为前面的例子返回 5.
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.
我已经试过了:
((car '(+ 2 3)) (cadr '(+ 2 3)) (caddr '(+ 2 3)))
但我收到以下错误:
应用程序:不是程序.
推荐答案
你可以试试 eval ,应该立即执行:
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)
)))
这篇关于使用列表元素的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文