Add ch04, start working on ch05.
This commit is contained in:
60
lpn/ch04/exercises.pl
Normal file
60
lpn/ch04/exercises.pl
Normal file
@@ -0,0 +1,60 @@
|
||||
%% Exercises from chapter 4
|
||||
%%
|
||||
%% Exercise 4.3 Write a predicate second(X,List) which checks whether X is the
|
||||
%% second element of List .
|
||||
|
||||
second(X, [_, X|_]).
|
||||
|
||||
%% Exercise 4.4 Write a predicate swap12(List1,List2) which checks whether
|
||||
%% List1 is identical to List2 , except that the first two elements are
|
||||
%% exchanged.
|
||||
|
||||
swap12([X,Y|T], [Y,X|T]).
|
||||
|
||||
%% Exercise 4.5 Suppose we are given a knowledge base with the following
|
||||
%% facts:
|
||||
|
||||
tran(eins,one).
|
||||
tran(zwei,two).
|
||||
tran(drei,three).
|
||||
tran(vier,four).
|
||||
tran(fuenf,five).
|
||||
tran(sechs,six).
|
||||
tran(sieben,seven).
|
||||
tran(acht,eight).
|
||||
tran(neun,nine).
|
||||
|
||||
%% Write a predicate listtran(G,E) which translates a list of German number
|
||||
%% words to the corresponding list of English number words. For example:
|
||||
%% listtran([eins,neun,zwei],X).
|
||||
%% should give:
|
||||
%% X = [one,nine,two].
|
||||
%% Your program should also work in the other direction. For example, if you give it the query
|
||||
%% listtran(X,[one,seven,six,two]).
|
||||
%% it should return:
|
||||
%% X = [eins,sieben,sechs,zwei].
|
||||
%% (Hint: to answer this question, first ask yourself “How do I translate the
|
||||
%% empty list of number words?”. That’s the base case. For non-empty lists, first
|
||||
%% translate the head of the list, then use recursion to translate the tail.)
|
||||
listtran([], []).
|
||||
listtran([X|TX], [Y|TY]) :-
|
||||
tran(X, Y),
|
||||
listtran(TX, TY).
|
||||
|
||||
%% Exercise 4.6 Write a predicate twice(In,Out) whose left argument is a list,
|
||||
%% and whose right argument is a list consisting of every element in the left
|
||||
%% list written twice. For example, the query
|
||||
%% twice([a,4,buggle],X).
|
||||
%% should return
|
||||
%% X = [a,a,4,4,buggle,buggle]).
|
||||
%% And the query
|
||||
%% twice([1,2,1,1],X).
|
||||
%% should return
|
||||
%% X = [1,1,2,2,1,1,1,1].
|
||||
%% (Hint: to answer this question, first ask yourself “What should happen when
|
||||
%% the first argument is the empty list?”. That’s the base case. For non-empty
|
||||
%% lists, think about what you should do with the head, and use recursion to
|
||||
%% handle the tail.)
|
||||
twice([], []).
|
||||
twice([X|T1], [X,X|T2]) :-
|
||||
twice(T1, T2).
|
||||
13
lpn/ch04/list.pl
Normal file
13
lpn/ch04/list.pl
Normal file
@@ -0,0 +1,13 @@
|
||||
member(X, [X|_]).
|
||||
member(X, [_|T]) :- member(X, T).
|
||||
|
||||
memberr(X, X).
|
||||
memberr(X, [X|_]).
|
||||
memberr(X, [[H|T1]|T2]) :-
|
||||
memberr(X, H);
|
||||
memberr(X, T1);
|
||||
memberr(X, T2).
|
||||
memberr(X, [_|T]) :- member(X, T).
|
||||
|
||||
sameLen([], []).
|
||||
sameLen([_|TA], [_|TB]) :- sameLen(TA, TB).
|
||||
30
lpn/ch04/notes.md
Normal file
30
lpn/ch04/notes.md
Normal file
@@ -0,0 +1,30 @@
|
||||
## Chapter 4: Lists
|
||||
|
||||
Lists are enclosed in square brackets, and are finite sequences of elements.
|
||||
Elements can be anything: `[mia, robber(yolanda), X, 2, mia]` or `[mia,
|
||||
[vincent, jules], [butch, girlfriend(butch)]]` --- a list can contain other
|
||||
lists.
|
||||
|
||||
Prolog lists use the standard head/tail vocabulary, and the decomposition operator is `|`:
|
||||
|
||||
```
|
||||
[Head|Tail] = [mia, [vincent, jules], [butch, girlfriend(butch)]]
|
||||
```
|
||||
|
||||
Note that the empty list behaves as one would think.
|
||||
|
||||
```
|
||||
?- [X|Y] = [].
|
||||
|
||||
no
|
||||
````
|
||||
|
||||
Arguments can be chained, such as `[X, Y | Z]`.
|
||||
|
||||
### The `member` function
|
||||
|
||||
```
|
||||
member(X, [X|_]).
|
||||
member(X, [_|T]) :- member(X, T).
|
||||
```
|
||||
|
||||
39
lpn/ch04/practical.pl
Normal file
39
lpn/ch04/practical.pl
Normal file
@@ -0,0 +1,39 @@
|
||||
%% 1. Write a 3-place predicate combine1 which takes three lists as arguments
|
||||
%% and combines the elements of the first two lists into the third as follows:
|
||||
%%
|
||||
%% ?- combine1([a,b,c],[1,2,3],X).
|
||||
%%
|
||||
%% X = [a,1,b,2,c,3]
|
||||
%%
|
||||
%% ?- combine1([f,b,yip,yup],[glu,gla,gli,glo],Result).
|
||||
%%
|
||||
%% Result = [f,glu,b,gla,yip,gli,yup,glo]
|
||||
combine1([], [], []).
|
||||
combine1([X|TX], [Y|TY], [X,Y|TXY]) :- combine1(TX, TY, TXY).
|
||||
|
||||
%% Now write a 3-place predicate combine2 which takes three lists as arguments
|
||||
%% and combines the elements of the first two lists into the third as follows:
|
||||
%%
|
||||
%% ?- combine2([a,b,c],[1,2,3],X).
|
||||
%%
|
||||
%% X = [[a,1],[b,2],[c,3]]
|
||||
%%
|
||||
%% ?- combine2([f,b,yip,yup],[glu,gla,gli,glo],Result).
|
||||
%%
|
||||
%% Result = [[f,glu],[b,gla],[yip,gli],[yup,glo]]
|
||||
combine2([], [], []).
|
||||
combine2([X|TX], [Y|TY], [[X, Y]|TXY]) :- combine2(TX, TY, TXY).
|
||||
|
||||
%% Finally, write a 3-place predicate combine3 which takes three lists as
|
||||
%% arguments and combines the elements of the first two lists into the third
|
||||
%% as follows:
|
||||
%%
|
||||
%% ?- combine3([a,b,c],[1,2,3],X).
|
||||
%%
|
||||
%% X = [j(a,1),j(b,2),j(c,3)]
|
||||
%%
|
||||
%% ?- combine3([f,b,yip,yup],[glu,gla,gli,glo],R).
|
||||
%%
|
||||
%% R = [j(f,glu),j(b,gla),j(yip,gli),j(yup,glo)]
|
||||
combine3([], [], []).
|
||||
combine3([X|TX], [Y|TY], [j(X,Y)|TXY]) :- combine3(TX, TY, TXY).
|
||||
Reference in New Issue
Block a user