prolog# Prolog - writing a query to calculate total of four given numbers

First off, let me say that I am quite a beginner in Prolog. I can create simple rules and facts (I know how to, for example, recursively define the predecessor relation etc).

However, now I am given a problem where I am given four numbers 40, 10, 12, 30 and I need to add them via two rules (these numbers actually represent electrical resistances). I have these two rules:

```
series(R1,R2,Re) :- Re is R1 + R2.
parallel(R1,R2,Re) :- Re is ((R1 * R2) / (R1 + R2))
```

40 and 10 need to be added via the parallel rule, the total of them then needs to be added to 12 via the series rule, the sum of which needs to be added to 30 via the parallel rule.

I am completely stumped by how to do this. I have two main questions.

- Is there any way I can somehow calculate all the sums (up until the last one) in the list of facts and rules (same as when I would, for example, define that Ann is the mother of Tom by writing the fact mother(ann, tom). ), and then write a single query which would be the last calculation (which is parallel of 30 and the other result, let's call it Rx), which would look like this:

```
parallel(30,Rx,X).
```

Where of course Rx would be a numerical value. I know that if I input a query of this kind, I get the result as output.

- If not, what's the easiest way to understand how this would work?

Thank you for your time!

Solution

40 and 10 need to be added via the parallel rule, the total of them then needs to be added to 12 via the series rule, the sum of which needs to be added to 30 via the parallel rule.

These steps become this code:

```
some_rule_name_here(Answer) :-
parallel(40, 10, Step1),
series(Step1, 12, Step2),
parallel(Step2, 30, Answer).
```

Then query:

```
?- some_rule_name_here(X).
```

- Prolog, X element before element Y on list
- What is the difference between once and cut in prolog?
- Getting the seed set by the ECLiPSe on loading
- Get simple Prolog example to work
- How to do recursion in a L-system inspired rewrite System, without DCG
- Maximum number of atoms in SICStus Prolog 4
- How to create a list dynamically in Prolog?
- Efficient solution for the same-fringe problem for binary trees
- Prolog Syntax for Dependent Conditions
- Guard clauses in prolog?
- phrase_from_stream/2 nontermination (Stream from http_open/3)
- SWI-Prolog: [FATAL ERROR: Could not find system resources]
- DCG LaTeX printer for FOL prover
- Prolog: a person is a sibling of himself?
- When can I safely avoid storing temporary calculations in Prolog?
- gprolog: Getting a stacktrace after an exception
- Prolog - Solution of a riddle
- Is the structure of my Prolog expression isomorphic to the structure of the Liar Paradox?
- Syntax error: Operator expected in Prolog
- Assertion Failure in SWI-Prolog When Using pyswip to Consult a Prolog File
- Retract by partial match?
- Prolog - Applying predicate with one fixed arguments over list
- swi prolog game assignment . I want the player to unlock the door when the player has key but i can't seem to make the function work
- Logging interactive queries in SWI-Prolog
- Why does Prolog automatically backtrack in one case but not in another?
- maplist raises uncaught exception: error(existence_error(procedure,maplist/2),top_level/0)
- ECLiPSe create 2D matrix with C++ interfacing
- Prolog: Compress a list that contains segments of continuous numbers to a list which contains list of range of segments [start,end]
- How can i convert a list into a circular list in Prolog?
- uncaught exception: error(instantiation_error,(=<)/2)