1
votes

Im trying to achieve this:

   sum(bookmark(num)),
   bookmark(bookmark(bum)),
   bookmark(bookmark(bookmark(num))).

   sum(bookmark(num), bookmark(bookmark(num)), A).

to return А = bookmark(bookmark(bookmark(num))) Im not close yet, since i fail at the bookmark function as stated below.

bookmark(e1, e2) :-
    e1 \= 0, 
    e2 \= 0. 

/*#e1 = element-one, e2 = element-two*/

sum(e1, e2, result) :- 
    bookmark(e1, e2),
    e1 is (1+1),
    e2 is (1+1),
    result = (e1 + e2).

Im checking if the bookmark function returns true, then the simple addition will follow. Problem is: if the input for the bookmark function is any two numbers, it returns false. Example with bookmark(1, 2) - false. Example with bookmark(0, 0) - false. Any help why this doesn't work will be greatly appreciated.

1
Variables start with a capital letter. is is arithmetic evaluation. = is syntactic equalityfalse

1 Answers

1
votes

In Prolog variables start with an uppercase. So e1 is not a variable, but a costant, E1 is a variable.

bookmark(E1, E2) :-
    E1 \= 0, 
    E2 \= 0.

sum(E1, E2, Result) :- 
    bookmark(E1, E2),
    E1 is (1+1),
    E2 is (1+1),
    Result = E1 + E2.

The above will however only succeed if E1 and E2 are both 2, and then Result will be 2 + 2 (not 4, just 2 + 2, or in canonical form +(2, 2)).