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 is the grandfather of Y
- Why does this Prolog program not terminate?
- Blocks-World Problem in Prolog keeps oscillating between the same two states
- Moving elements in a list-of-lists in prolog
- How do I tell Prolog that a specific tuple is not in a list
- Homework, want to solve with Prolog
- Writing a DCG containing same string reversed
- Prolog fact doesn't get interpreted
- I wrote a program to handle unit conversions in Prolog, but I always get stack-overflows: Do you know why?
- Composition of substitutions for unary addition in prolog?
- Prolog - first list is sublist of second list?
- Prolog doesn't return the correct query (to my knowledge) when added the (X \= Y) clause
- Is this Prolog expression isomorphic to the Liar Paradox?
- Prolog map coloring (4 colors map) code explanation
- Trying to calculate a hash with crypto_data_hash causes SWI session to crash - am I doing something wrong or is this a bug?
- Prolog existence_error procedure in basic example
- Translation to DCG Semicontext not working
- I need to write a turbo prolog program, which will remove all palindromes from the list
- create a list in prolog in range of two numbers
- Delete empty lists from lists of lists in Prolog
- How to keep Prolog from going back and forth between the same two steps forever?
- Prolog no_duplicate function
- How does SWI Prolog handle lists under the hood?
- I'm not understanding why underscore gets grounded in meta-predicates
- Marquee in Prolog
- How do I express and solve this trivial logic puzzle?
- Prolog - get best value among permutations of list
- Using \==/2 or dif/2
- What would be an elegant/idiomatic way to introduce a predicte on a list of atoms?
- Conditional writing in Prolog