Dennis Snippert

Prolog

This is a page for myself to remind me of some functions in prolog.

A good tool for implementing prolog programm's is:

<TODO: link to SWI prolog program>

Simple family database for prolog:

female(josephien).
male(gerard).
male(marc). male(dennis).

mother(josephien, marc).
mother(josephien, dennis).
father(gerard, marc).
father(gerard, dennis).

In prolog there can be an anonymous variable "_". For example: if you want to ask if gerard is a father, the name of the son is not important. So you can use the following statement:

?- father(gerard, _).

Prolog will then answer with yes.

A variable can also be suppressed with an "_" If it's before a variable the variable will not be displayed in the result. Example:

?- father(_Vader, Child).
Child = marc.

Yes.

The following example shows how there can be asked if a father and a mother are parents of a child:

Parents(Father, Mother, Child):-
father(Father, Child),
mother(Mother, Child).

The following example shows a grandMother example:

grandMother(GrandMother, Child):-
mother(GrandMother, Mother),
mother(Mother, Child).

grandFather(GrandFather, Child):-
father(GrandFather, Father),
father(Father, Child).
Lists:

Member function:

member(X, [X|Tail]).
member(X, [Y|Tail]) :-
member(x, Tail).

Concatination:

conc([],L,L).
conc([X|L1], L2, [X|L3]):-
conc(L1, L2, L3).

Append:

Append two lists with the following structure: append(List1, List2, List1And2).

append([], List, List).
append([Element, List1], List2, [Element| List1And2]):-
append(List1, List2, List1And2).

Length:

Calculate the length of a list with the following structure: length(List, Number); Where number is the lenght of List

lenght([], 0).
length([Element|Tail], L):-
length(Tail, M),
L is M+1.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.