/*  definition of 'facts'                     */
/*  lowercase = atom, uppercase = variable    */
student(lukas).
student(frederic).
teacher(schmidhofer).

/*  comma is 'AND'                            */
students(X, Y) :- student(X), student(Y).

/*  two clauses with same structure are 'OR'  */
name_exists(X) :- student(X).
name_exists(X) :- teacher(X).

/*  possible queries:

     students(lukas, andreas).     -> false
     students(lukas, frederic).    -> true
     students(lukas, schmidhofer). -> false
     name_exists(lukas).           -> true
     name_exists(schmidhofer).     -> true
     name_exists(andreas).         -> false   */


/*  recursion in prolog                       */
/*  lists cannot be indexed. Only head can
    be taken from top (here: variable X).     */

ismember([Value|_], Value).
ismember([X|Xs], Value) :- Value \= X, ismember(Xs, Value).

/* query:

       ?- ismember([1, 34, 5, 6], 4).
       false.
       
       ?- ismember([1, 3, 4, 5, 6], 4).
       true.                                  */

sum([], Sum, Sum).
sum([X|Xs], IterSum, TotalSum) :-
        IterSum2 is IterSum + X,
        sum(Xs, IterSum2, TotalSum).
sum(Xs, Sum) :- sum(Xs, 0, Sum).

/* query:

       ?- sum([1,3, 42], Sum).
       Sum = 46.                              */
