Add ch04, start working on ch05.

This commit is contained in:
2018-01-18 16:30:58 -08:00
parent 50dbc5f0c0
commit 5f9633349c
11 changed files with 318 additions and 3 deletions

21
lpn/ch05/arith.pl Normal file
View File

@@ -0,0 +1,21 @@
len([], 0).
len([_|T], N) :-
len(T, X),
N is X+1.
alen_([], A, A).
alen_([_|H], A, L) :-
A2 is A+1,
alen_(H, A2, L).
alen(X, L) :- alen_(X, 0, L).
max([], N, N).
max([H|T], N, M) :-
H > N,
max(T, H, M).
max([H|T], N, M) :-
H =< N,
max(T, N, M).
max([H|T], N) :-
max(T, H, N).

24
lpn/ch05/exercises.pl Normal file
View File

@@ -0,0 +1,24 @@
%% Exercise 5.2
%%
%% 1. Define a 2-place predicate increment that holds only when its second
%% argument is an integer one larger than its first argument. For example,
%% increment(4,5) should hold, but increment(4,6) should not.
increment(A, B) :- B is A+1.
%% 2. Define a 3-place predicate sum that holds only when its third argument is
%% the sum of the first two arguments. For example, sum(4,5,9) should hold, but
%% sum(4,6,12) should not.
sum(X, Y, Z) :- Z is X+Y.
%% Exercise 5.3
%%
%% Write a predicate addone/2 whose first argument is a list of integers, and
%% whose second argument is the list of integers obtained by adding 1 to each
%% integer in the first list. For example, the query
%% ?- addone([1,2,7,2],X).
%% should give
%% X = [2,3,8,3].
addone([], []).
addone([X|TX], [Y|TY]) :-
Y is X+1,
addone(TX, TY).

74
lpn/ch05/notes.md Normal file
View File

@@ -0,0 +1,74 @@
## Chapter 5: Arithmetic in Prolog
Prolog provides basic arithmetic operators.
Ex.
```
?- 8 is 6+2.
yes
?- 12 is 6*2.
yes
?- -2 is 6-8.
yes
?- 3 is 6/2.
yes
?- 1 is mod(7,2).
yes
?- X is 12/4.
X = 3.
```
The operators don't actually do arithmetic:
```
?- X = 2 + 3.
X = 2+3.
```
The default is just to do unification; `is` must be used. The arithmetic
expression must be on the RHS. This part of Prolog is a black box that
handles this, and isn't part of the normal KB and unification parts.
### Arithmetic and lists
A recursive list length calculator:
```
len([], 0).
len([_|T], N) :-
len(T, X),
N is X+1.
```
A tail-recursive length calculator:
```
alen_([], A, A).
alen_([_|H], A, L) :-
A2 is A+1,
alen_(H, A2, L).
alen(X, L) :- alen_(X, 0, L).
```
Standard notes about tail recursion efficiency apply here.
### Comparing integers
* *x < y* &rarr; `X < Y.`
* *x ≤ y* &rarr; `X =< Y.`
* *x = y* &rarr; `X =:= Y.`
* *x ≠ y* &rarr; `X =\= Y.`
* *x ≥ y* &rarr; `X >= Y`
* *x > y* &rarr; `X > Y`
Note the difference between `=` and `=:=`.
Let's write a `max` function:
```

54
lpn/ch05/practical.pl Normal file
View File

@@ -0,0 +1,54 @@
%% Chapter 5 practical session
%%
%% The purpose of Practical Session 5 is to help you get familiar with Prologs
%% arithmetic capabilities, and to give you some further practice in list
%% manipulation. To this end, we suggest the following programming exercises:
%%
%% 1. In the text we discussed the 3-place predicate accMax which returned the
%% maximum of a list of integers. By changing the code slightly, turn this into
%% a 3-place predicate accMin which returns the minimum of a list of integers.
min([], N, N).
min([H|T], N, M) :-
H < N,
min(T, H, M).
min([H|T], N, M) :-
H >= N,
min(T, N, M).
min([H|T], N) :-
min(T, H, N).
%% 2. In mathematics, an n-dimensional vector is a list of numbers of length n.
%% For example, [2,5,12] is a 3-dimensional vector, and [45,27,3,-4,6] is a
%% 5-dimensional vector. One of the basic operations on vectors is scalar
%% multiplication . In this operation, every element of a vector is multiplied
%% by some number. For example, if we scalar multiply the 3-dimensional vector
%% [2,7,4] by 3 the result is the 3-dimensional vector [6,21,12] .
%%
%% Write a 3-place predicate scalarMult whose first argument is an integer,
%% whose second argument is a list of integers, and whose third argument is the
%% result of scalar multiplying the second argument by the first. For example,
%% the query
%% ?- scalarMult(3,[2,7,4],Result).
%% should yield
%% Result = [6,21,12]
scalarMult(_, [], []).
scalarMult(K, [H|T], R) :-
V is K * H,
scalarMult(K, T, [R|V]).
%% 3. Another fundamental operation on vectors is the dot product. This
%% operation combines two vectors of the same dimension and yields a
%% number as a result. The operation is carried out as follows: the
%% corresponding elements of the two vectors are multiplied, and the
%% results added. For example, the dot product of [2,5,6] and [3,4,1] is
%% 6+20+6 , that is, 32 . Write a 3-place predicate dot whose first
%% argument is a list of integers, whose second argument is a list of
%% integers of the same length as the first, and whose third argument is
%% the dot product of the first argument with the second. For example,
%% the query
%% ?- dot([2,5,6],[3,4,1],Result).
%% should yield
%% Result = 32