计算代数表达式 [英] Evaluating an algebraic expression

查看:63
本文介绍了计算代数表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个我遇到问题的测试复习题.你如何编写一个方法来计算一个带有运算符 plus 的代数表达式,.以下是一些测试查询:

This is a test review question that I am having trouble with. How do you write a method to evaluate an algebraic expression with the operators plus, minus and times. Here are some test queries:

simplify(Expression, Result, List)

?- simplify(plus(times(x,y),times(3 ,minus(x,y))),V,[x:4,y:2]).
          V = 14

?- simplify(times(2,plus(a,b)),Val,[a:1,b:5]).
          Val = 12

?- simplify(times(2,plus(a,b)),Val,[a:1,b:(-5)]).
          Val = -8 .

我得到的只是这些示例查询,没有其他解释.但我很确定该方法应该剖析第一个参数,即代数表达式,用 x 和 y 替换它们在第三个参数(列表)中的值.第二个参数应该是计算表达式后的结果.

All I was given were these sample queries and no other explanation. But I am pretty sure the method is supposed to dissect the first argument, which is the algebraic expression, substituting x and y for their values in the 3rd argument (List). The second argument should be the result after evaluating the expression.

我认为其中一种方法应该是 simplify(V, Val, L) :- member(V:Val, L).理想情况下应该只剩下 4 种方法……但我不知道该怎么做.

I think one of the methods should be simplify(V, Val, L) :- member(V:Val, L). Ideally there should only be 4 more methods... but I'm not sure how to go about this.

推荐答案

从小处着手,写下你所知道的.

Start small, write down what you know.

simplify(plus(times(x,y),times(3 ,minus(x,y))),V,[x:4,y:2]):- V = 14.

是一个完美的开始:(+ (* 4 2) (* 3 (- 4 2))) = 8 + 3*2 = 14.但是,当然,

is a perfectly good start: (+ (* 4 2) (* 3 (- 4 2))) = 8 + 3*2 = 14. But then, of course,

simplify(times(x,y),V,[x:4,y:2]):- V is 4*2.

甚至更好.还有,

simplify(minus(x,y),V,[x:4,y:2]):- V is 4-2.
simplify(plus(x,y),V,[x:4,y:2]):- V is 4+2.
simplify(x,V,[x:4,y:2]):- V is 4.

所有完美的 Prolog 代码.但当然,我们真正的意思是,很明显,是

all perfectly good Prolog code. But of course what we really mean, it becomes apparent, is

simplify(A,V,L):- atom(A), getVal(A,L,V).
simplify(C,V,L):- compound(C), C =.. [F|T], 
  maplist( simp(L), T, VS),       % get the values of subterms
  calculate( F, VS, V).           % calculate the final result

simp(L,A,V):- simplify(A,V,L).    % just a different args order

等等.getVal/3 将需要以某种方式从 L 列表中检索值,并且 calculate/3 以实际执行计算,给定符号操作名称和计算值列表.

etc. getVal/3 will need to retrieve the values somehow from the L list, and calculate/3 to actually perform the calculation, given a symbolic operation name and the list of calculated values.

研究maplist/3=../2.

(未完成,未测试).

好的,maplist=.. 一样是一种矫枉过正:您的所有术语都可能采用 op(A,B).所以定义可以简化

OK, maplist was an overkill, as was =..: all your terms will probably be of the form op(A,B). So the definition can be simplified to

simplify(plus(A,B),V,L):-
  simplify(A,V1,L),
  simplify(B,V2,L),
  V is V1 + V2.         % we add, for plus

simplify(minus(A,B),V,L):-
  % fill in the blanks
  .....
  V is V1 - V2.         % we subtract, for minus

simplify(times(A,B),V,L):-
  % fill in the blanks
  .....
  V is .... .           % for times we ...

simplify(A,V,L):-
  number(A),
  V = .... .            % if A is a number, then the answer is ...

最后一种可能是 xy 等,满足 atom/1.

and the last possibility is, x or y etc., that satisfy atom/1.

simplify(A,V,L):-
  atom(A),
  retrieve(A,V,L).

因此上述子句的最后一次调用可能看起来像 retrieve(x,V,[x:4, y:3]),或者看起来像 retrieve(y,V,[x:4, y:3]).实施起来应该很简单.

So the last call from the above clause could look like retrieve(x,V,[x:4, y:3]), or it could look like retrieve(y,V,[x:4, y:3]). It should be a straightforward affair to implement.

这篇关于计算代数表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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