24.01.2014 Views

Programming 2012

Programming 2012

Programming 2012

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Programming</strong> <strong>2012</strong><br />

Laboratory session no. 2<br />

Problem 1 (1p)<br />

Ask Prolog the following queries:<br />

• Does X unify with list [1,2,3,4];<br />

• Does X unify with list whose head is 1;<br />

• Does X unify with list whose first two elements are 1 and 2;<br />

• Does X unify with a singleton list? Singleton list is a list of length 1;<br />

• Does X unify with a list of length 3?<br />

Problem 2 (1p)<br />

Define in Prolog the predicates listed below:<br />

• first(X) satisfied when X unifies with list [1,2,3,4];<br />

• second(X) satisfied when X unifies with list whose head is 1;<br />

• third(X) satisfied when X unifies with list whose first two elements are<br />

1 and 2;<br />

• fourth(X) satisfied when X unifies with a singleton list. Singleton list<br />

is a list of length 1;<br />

• fifth(X) satisfied when X unifies with a list of length 3?<br />

Problem 3 (1p)<br />

Define in Prolog predicate even/2 such that even(N) is satisfied when N is<br />

an even number. Provide two implementations of the predicate. First using<br />

modulo division and second – recursion.<br />

Problem 4 (3p)<br />

We represent binary trees with labelled internal nodes in Prolog using the<br />

functor node/3 for internal nodes and the atom leaf/0 for leaves. E.g., the<br />

structure:<br />

represents the tree:<br />

node(leaf, 3, node(leaf, 5, leaf))<br />

3<br />

5<br />

1


Define predicates mirror/2, which creates the mirror image of a tree and<br />

flatten/2, which creates a list of the tree labels in the infix order.<br />

2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!