Hello,
I need your help with Prolog, please.
I have to define the power function using the product function.
I implemented this way but I get a syntax error.
summe(null, Y, Y).
summe(nach(X), Y, nach(Z)) :-
summe(X, Y, Z).
produkt(null,_,null).
produkt(nach(X), Y, Z) :-
produkt(X, Y, P),
summe(P, Y, Z).
potenz(X,null,,1).
potenz(X,Y,Z) :-
Y>0,
K is X,
Potenz(X,Y-1,produkt(X,K,Z))
The syntax errors point out exactly the line number and column where the errors occurred:
ERROR: /tmp/power.pl:10:15: Syntax error: Operator expected
ERROR: /tmp/power.pl:14:31: Syntax error: Unexpected end of file
(There’s one more error that will show up when you fix those - the error message is a bit obscure: it’s caused by Potenz
instead of potenz
.)
Hello,
Beside syntax you may also want to consider the meaning of the syntax you used.
I have a bit difficulty understand your intent in the code – perhaps you want to add some comments, to help explain what each line intends to do.
Btw, unlike many other languages, in prolog, for example, nach(X) when placed as an argument, is not a function call that returns a value, but simply a data structure that is used for matching during a predicate call – e.g. summe(nach(X), Y, nach(Z))
is matched by a call such as summe(nach(2), 3, nach(8)) … just to pick some numbers, but not by summe(2,3,5).
And similarly, product(X, K, Z) as argument isn’t a function call, but simply a data structure …
But, as mentioned, i don’t know what your intent was when you wrote these syntactic terms.
Dan