Monday, March 30, 2009

Adventures in Prolog - Link List I

Today's post is merely a compilation of interesting resources on Prolog.

Learn Prolog Now - An online (and paperback) book to learn Prolog

Aspect-Oriented Programming Prolog

Adventures in Prolog - another online book to learn Prolog

P99-Ninety-Nine Prolog Problems - a set of Prolog challenges organized into different degree of difficulty and domains

So happy reading and solving problems :D

Tuesday, March 17, 2009

Adventures in Prolog - computations with Lists V

Today's post presents a predicate defined for lists that is as simple as yesterdays rem_dups/2. flatten/2 is concerned with flattening an arbitrary list to a one-dimensional list/array.The idea behind flatten is pretty simple, i.e. if a list is non-flat we lower its dimensionality by taking theinner of that list and process it further until we reach a one-dimensional list.

%flatten/2
flatten([], []).
flatten([[]], []).
flatten([[HT]], X) :- flatten([HT], X).
flatten([[H]T], X) :- flatten([HT], X).
flatten([H[T]], X) :- flatten([HT], X).
flatten([HT], X) :-
flatten(T, Y),
append([H], Y, X).

Monday, March 16, 2009

Adventures in Prolog - computations with lists IV

Today's post is merely a modification to yesterdays post, i.e. the remove duplicate functions will be streamlined. rem_dups2/2 is a replication of rem_dups/2 with less dependencies, i.e. it does not use the remove/3 predicate thereby improving performance. A drawback is that rem_dups2 does not preserve order of list elements when producing output, i.e. the last occurence of a duplicate list element will be preserved in the output list.

rem_dups2([X[]], [X]).

rem_dups2([HT], X) :-
member(H, T),
!,
rem_dups2(T, X);
rem_dups2(T, Y),
append([H], Y, X).

Sunday, March 15, 2009

Adventures in Prolog - Computations with lists III

This week's post is concerned with another operation on lists, i.e. how to compute a set given a list. This means that given a list containing any values, we wish to remove duplicates from this list to obtain a set containing unique members. The predicate rem_dups/2 does exactly this.

rem_dups([X[]], [X]).

rem_dups([HT], X) :-
member(H, T),
!,
remove(H, T, Y),
rem_dup([HY], X);
rem_dup(T, Y),
append([H], Y, X).

The problem forumulation basically says that if the head of a list is contained within the tail of the list it is a duplicate. Consequently, this duplicate (and any further duplicate of H) is removed from T. Since we want to retain H, we need to connect it with the list that does not contain any instance of H, i.e. X. This procedure is followed for all elements of the list. If a list element does not have a duplicate it is not removed from the list.

rem_dup makes use of auxiliary predicates like remove/3, append/3 and member/2. member/2 checks whether some X is element of some list L. remove/3 removes some X from list L1 yielding the result list L2. append/3 does the opposite it appends some X to an existing list L1 yielding L2.

Thursday, March 05, 2009

Adventures in Prolog - Link

Since I am quite occupied these days, I will only post a link to some intellectual stimulus today.

If you follow this link you will be taken to "The First 10 Prolog Programming Contests". So indulge some brain food.