Is there a way to do operations in the predicate parameters?

Something like that:

increment(X, X2 is X + 1). 

Is there any way to do this?

+4
source share
3 answers
 no. 

well, if there is no shell predicate for increment/3 that will perform the operation, then call the actual increment/3 it is considered valid.

edit: suggests that you want to write a wrapper for foo(Arg1, Arg2, Arg3) so that you can pass the expression as the second argument. The wrapper will be:

 efoo(Arg1, Expr, Arg3):- Arg2 is Expr, foo(Arg1, Arg2, Arg3) 

if you want to do this a lot, you may need to distract this behavior:

 eval_call(Predicate, Expressions):- maplist(evaluate, Expression, Args), call(Predicate,Args). evaluate(Expr, Arg):- Arg is Expr. 
+2
source

there is succ / 2, which requires semantics, and a little more

 ?- succ(3,X). X = 4. ?- succ(X,7). X = 6. 

The question title seems to require something more general, and as @thanosQR already pointed out, this syntax needs some modification.

+1
source

No, the standard idiom does this separately, for example:

 X2 is X + 1, increment(X,X2). 
0
source

Source: https://habr.com/ru/post/1445406/


All Articles