Hi all. I am a beginning gprolog user and would have a few very basic
questions that prevent me from going further.
I have this prolog program which checks if elements of the first list are
products of pair of elements of the second list. For example, L1=[2, 4, 6],
L2=[1, 2, 2, 2, 3, 2] is true because the product of the first two elements
of L2 (1 and 2) is the first element of L1 (2), and the second pair of L2
(2, 3) is the second element of L1 (6) and so on. Here is my code:

rules.pl:
product([], []).
product([L1Head|L1Tail], [L2First|[L2Second|L2Tail]]) :- L1Head is L2First
* L2Second, iloczyn(L1Tail, L2Tail).

and it seems to be working:
| ?- [rules].
compiling /home/wujek/Playground/rules.pl for byte code...
/home/wujek/Playground/rules.pl compiled, 2 lines read - 755 bytes written,
13 ms

yes
| ?- product([6], [3, 2]).

yes
| ?- product([4, -6, 25], [1, 4, -3, 2, 5, 5]).

yes
| ?- product([4, -6, 25], [1, 4, -3, 2, 5, 4]).

no
| ?- product([4, -6, 25], [1, 4, -3, 2, 5]).

no

The questions are:
1. my first attempt in the first goal was:
L1Head = L2First * L2Second, which apparently isn't true. Why doesn't it
work? Why do I need the 'is' word there? It looks like prolog doesn't
compute the value before unification, but why?
2. When I rewrite the program so:
product([], []).
product(L1, L2) :- [L1Head|L1Tail] = L1, [L2First|[L2Second|L2Tail]] = L2,
L1Head is L2First * L2Second, product(L1Tail, L2Tail).

prolog asks me each time if it is true, instead of just saying 'yes' as
before (it does say 'no' right away, tho):
| ?- product([], []).

true ? % I have to press [enter] here

yes

Why does this happen?

Regards,
wujek
_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog

Reply via email to