Wednesday, February 24, 2016

My first hour with prolog (Prolog 101)

I started with prolog and it is hard. It really is!
Here I found a portable "IDE" and here a video how to use it: .
I created a test project to summarize a list of numbers:
File > Edit > [select a file with following content: *.pl]

sumOfItems([], 0).
sumOfItems([H | T], Output) :-
    sumOfItems(T, OutputInner),
    Output is H + OutputInner.

File > Consult > [select same file]
1 ?- sumOfItems([1,2,3,4,5],X).
X = 15.
... from now on changes to the file are taken over if they were compiled.
To check that I added the following function and compiled:
avgOfItems([], 0).
avgOfItems([H], Result) :-
 Result is H.
avgOfItems([H|T], Result) :-
 avgOfItems(T, ResultInner),
 Result is (H + ResultInner)/2.
avgOfItems was callable...

kind regards,

No comments: