# Arithmetic operator as an argument

**URL:** <https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899>\
**Category:** Help!\
**Created:** [September 9, 2020, 6:19am UTC](https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899 "2020-09-09T06:19:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![moraneus](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@moraneus](https://swi-prolog.discourse.group/u/moraneus)\
**Post date:** [September 9, 2020, 6:19am UTC](https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899/1 "2020-09-09T06:19:44Z")

</div>

Assume I want to write calculator.  
Is there any way to use the arithmetic operator as an argument  
like this:

opp(A, B, Op, Res) :- Op(A, B, Res))

opp(1, 2, +, Res)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 9, 2020, 9:45am UTC](https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899/2 "2020-09-09T09:45:30Z")

</div>

This is going to be 10x more complicated than what you ask, but because it is 10x more complex and works you should take that to mean that this is a valid way to do it.

Look at this code: [https://github.com/maths/PRESS/blob/026ff5d5e2a572fd05e4313792a177dab6682660/util/tidy.pl](https://github.com/maths/PRESS/blob/026ff5d5e2a572fd05e4313792a177dab6682660/util/tidy.pl)

that is part of [PRESS](https://github.com/maths/PRESS): PRolog Equation Solving System

---

<div class="post-metadata">

**Author:** ![volker-wysk](https://avatars.discourse-cdn.com/v4/letter/v/858c86/32.png) [@volker-wysk](https://swi-prolog.discourse.group/u/volker-wysk)\
**Post date:** [September 9, 2020, 10:05am UTC](https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899/3 "2020-09-09T10:05:20Z")

</div>

Hi

> ```
> moraneus 
> 
> ```
> 
> September 9  
> Assume I want to write calculator.  
> Is there any way to use the arithmetic operator as an argument  
> like this:
> 
> opp(A, B, Op, Res) :- Op(A, B, Res))
> 
> opp(1, 2, +, Res)

I don’t know. But you could encapsulate the operator in a predicate.  
Like this (untested):

```prolog
plus(A, B, C) :-
   C is A+B.

opp(A, B, Op, C) :-
   call(Op, A, B, C).

opp(1,2,plus,Res)

```

Bye  
Volker

---

<div class="post-metadata">

**Author:** ![Boris](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/boris/32/7486_2.png) [@Boris](https://swi-prolog.discourse.group/u/Boris)\
**Post date:** [September 9, 2020, 10:24am UTC](https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899/4 "2020-09-09T10:24:55Z")

</div>

One clean approach that works for arithmetic is to make an expression that you then evaluate using is/2. Like this:

```prolog
opp(A, B, Op, R) :-
    Expr =.. [Op, A, B],
    R is Expr.

```

With this I get:

```prolog
?- opp(1, 2, +, R).
R = 3.

?- opp(1, 2, *, R).
R = 2.

?- opp(1, 2, -, R).
R = -1.

?- opp(1, 2, /, R).
R = 0.5.

```

---

<div class="post-metadata">

**Author:** ![moraneus](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@moraneus](https://swi-prolog.discourse.group/u/moraneus)\
**Post date:** [September 9, 2020, 11:14am UTC](https://swi-prolog.discourse.group/t/arithmetic-operator-as-an-argument/2899/5 "2020-09-09T11:14:03Z")

</div>

This 100 times more complicated 🙂
