解决LISP中的Infix算术运算 [英] Solving Infix Arithmatic in LISP

查看:86
本文介绍了解决LISP中的Infix算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(defun solve (L)
    (cond
    ((null L) nil)
    (t(eval (list (car (cdr L)) (car L) (car (cdr (cdr L))))))))    

我拥有的代码是一个简单的评估程序,只要输入类似于'(5 + 4),它就可以正常工作.但是我希望能够解决其他输入,例如'(5 +(3-1))'(6 + 5)-(4/2).我的问题显然是如何处理括号.我尝试将'(的字面值与((equal(car L)'()(solve(cdr L)))进行比较封闭圆括号.有没有办法检查原子是否是圆括号?

The code I have is a simple evaluate program that works fine as long as the input is something like '(5 + 4). However I want to be able to solve other inputs such as '(5 +( 3 - 1)) and '(6 + 5) - (4 /2 ). My problem obviously being how to handle the parentheses. I tried comparing the literal value of '( as in ((equal (car L) '( ) (solve(cdr L))) but that only throws all of my close parentheses out of whack. Is there a way to check if an atom is a parentheses?

推荐答案

我希望,如果这是一个家庭作业问题,那么您可以查看火星的评论,而不是我的答案.如果仅接受我的代码,您就不会做得很好.另一方面,如果您真正通读了所有内容并真正理解了它,那就好了.

I'm hoping that if this is a homework question, you look at Mars's comments instead of my answer. You're not going to do well if you just take my code. On the other hand, if you actually read through everything and really understand it, that's something.

就像火星所说,我们需要递归.很简单;如果我们有电话,请退回.

Like Mars said, we need to recurse. It's simple enough; if we have a number, return it.

(defun solve (expression)
  (if (atom expression)
      expression
    'havent-dealt-with-this-yet))
[22]> (solve '3)
3

如果我们有一个列表,那么第二件事,假设它是运算符,然后按照您在示例中所做的方式进行评估:

If we've got a list, take the second thing, assume it's the operator, and eval it the way you did in our sample:

(defun solve (expression)
  (if (atom expression)
      expression
    (eval (list (second expression)
                (first expression)
                (third expression)))))
[25]> (solve 3)
3
[26]> (solve '(3 + 4))
7

很酷.但是在这里,我们只不过是您的原始代码.如果我们有一个嵌套的表达式会发生什么?

Cool. But here we're no further than your original code. What happens if we have a nested expression?

[27]> (solve '(3 + (2 * 2)))

*** - EVAL: 2 is not a function name; try using a symbol instead

嗯,这不起作用.我们不能仅仅假设(first expression) or(第三个表达式)中的任何一个都是值.他们可能是表达自己.但是我们需要弄清楚表达式的值.假设我们有一种获取表达式并找到其值的方法.然后我们的代码可以是:

Well, this isn't working. We can't just assume that either of (first expression) or(third expression)` are values; they may be expressions themselves. But we need to figure out the value of the expression. Let's assume we have a way to take an expression and find its value. Then our code can be:

(defun solve (expression)
  (if (atom expression)
      expression
    (eval (list (second expression)
                (find-expression-value (first expression))
                (find-expression-value (third expression))))))

等等! solve是一个接受表达式并找到其值的函数!

Wait! solve is a function that takes an expression and finds its value!

(defun solve (expression)
  (if (atom expression)
      expression
    (eval (list (second expression)
                (solve (first expression))
                (solve (third expression))))))
[43]> (solve 3)
3
[44]> (solve '(3 + 2))
5
[45]> (solve '((3 + 1) * (6 / 2)))
12

ta-da!

这篇关于解决LISP中的Infix算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆