Prolog - 用值替换原子并评估 [英] Prolog - Substitute atom with value and evaluate

查看:30
本文介绍了Prolog - 用值替换原子并评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定

Result = 4* (3*x)^3*3,

VarValue = x:2,

如何获得以下输出

Value = 2592 ;

如果我必须定义以下谓词:

if I have to defined the following predicate:

evaluate(Result,Value,VarValue)

我尝试执行以下操作:

evaluate(Result, Value, VarValue) :- member(VarValue, [x:X]).

并试图将 X 代入等式中......我想不出从那里开始的好方法.

and trying to substitute X into the equation.. I could not think of a nice way to go from there.

有没有办法只使用以下内置谓词:

Is there a way to only use the following built-predicates :

 //, /, +, -, ^, *,=..,>, <,
 atom, is_list, functor, arg, integer, number, member, append

推荐答案

我认为 joel76 的解决方案可以更紧凑一些:

I think joel76 solution can be made a bit more compact:

exp_symbols(Symbols, Expr, WithSym) :-
    Expr =.. [F|Args],
    ( memberchk(F:V, Symbols) -> G = V ; G = F ),
    maplist(exp_symbols(Symbols), Args, ArgsSWithSym),
    WithSym =.. [G|ArgsSWithSym].

evaluate(Exp, LstVars, Val) :-
    exp_symbols(LstVars, Exp, NewExp),
    Val is NewExp.

这篇关于Prolog - 用值替换原子并评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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