# Week 47

## Exercise Tuesday (22.11.2011)

1) Implement the programs from Tasks 1 and 2 of the previous exercise and use the cut operator where it makes sense.

2) Consider the following Prolog program (introduced exactly like this in lecture notes for DM509 at least 4 years old):

income(peter,400000).

foreigner(peter).

average_taxpayerWrong(X) :- foreigner(X), fail.

average_taxpayerWrong(X) :- income(X,I), I < 500000.

Here, `fail/0` is used to force a finite failure. The query
`?- average_taxpayerWrong(peter).` should clearly fail since Peter is
a foreigner. Why does it not fail? How can you repair the program such
that it works as one would expect?

Shorten the program to three clauses by having just one
clause for `average_taxpayer/1` and keeping the clauses for `income/2` and `foreigner/1`.

3) Write definitions for a predicate `divide/3` that performs division of natural numbers.
For example, the query `?- divide(13,3,D)` should yield the answer `D = 4`.
Use a generate-and-test approach, i.e., use a predicate `is_nat/1` defined as follows:

is_nat(0).

is_nat(X) :- is_nat(Y), X is Y+1.

Calling `is_nat/1` with a variable as the argument will successively enumerate
all natural numbers by backtracking. Now, define a predicate that tests whether a given
natural number is a solution to the division problem. Finally, define `divide`
using the generating predicate and the test.

## Lecture Thursday (24.11.2011)

The fifth lecture will start with a short introduction to constraint logic programming. Then we will continue with an introduction to Haskell. Here, we will focus on the basics of function declarations and pattern matching.

##### Topics

functional programming, function declarations, pattern matching

##### Reading

Lecture Notes in Blackboard (Course Documents)

## Lecture Friday (25.11.2011)

The sixth lecture will continue with the introduction to Haskell. After repeating guards, we will introduce pattern matching and learn about more advanced ways of declaring functions.

##### Topics

pattern matching, guards, local declarations, operators, expressions, patterns

##### Reading

Lecture Notes in Blackboard (Course Documents)