[tutor] Update assignment
This commit is contained in:
parent
39c3af4ba5
commit
ca687c0136
18 changed files with 438 additions and 14 deletions
30
examples/basics/arithmetics.pl
Normal file
30
examples/basics/arithmetics.pl
Normal file
|
@ -0,0 +1,30 @@
|
|||
name( character(Name, _, _, _), Name).
|
||||
class( character( _, Class, _, _), Class).
|
||||
level( character( _, _, Level, _), Level).
|
||||
hitpoints(character( _, _, _, HP), HP).
|
||||
|
||||
levelup(character(Name, Class, Level, HP), character(Name, Class, Next, HP)) :-
|
||||
succ(Level, Next).
|
||||
|
||||
aid(character(Name, Class, Level, HP), character(Name, Class, Level, T)) :-
|
||||
T is HP+5.
|
||||
|
||||
hit(character(Name, Class, Level, HP), character(Name, Class, Level, T)) :-
|
||||
T is HP-5.
|
||||
|
||||
status(character(Name, Class, Level, HP)) :-
|
||||
write(Name), write(' is a level '), write(Level), write(' '), write(Class), write(' with '), write(HP), write(' hitpoints.'), nl.
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
Gimli = character(gimli, fighter, 4, 35), status(Gimli),
|
||||
Legolas = character(legolas, ranger, 5, 30), status(Legolas),
|
||||
Gandalf = character(gandalf, wizard, 10, 25), status(Gandalf),
|
||||
Frodo = character(frodo, rogue, 2, 20), status(Frodo),
|
||||
write('legolas threw gimli, and gimli took 5 damage.'), nl,
|
||||
hit(Gimli, Thrown), status(Thrown),
|
||||
write('gandalf casts aid.'), nl,
|
||||
aid(Thrown, Aided), status(Aided),
|
||||
write('legolas leveled up.'), nl,
|
||||
levelup(Legolas, Leveled), status(Leveled).
|
9
examples/basics/backtracking.pl
Normal file
9
examples/basics/backtracking.pl
Normal file
|
@ -0,0 +1,9 @@
|
|||
leq(0, _).
|
||||
leq(s(X), s(Y)) :- leq(X, Y).
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
leq(X, s(s(s(0)))),
|
||||
write(X), nl,
|
||||
fail.
|
9
examples/basics/cut.pl
Normal file
9
examples/basics/cut.pl
Normal file
|
@ -0,0 +1,9 @@
|
|||
leq(0, _).
|
||||
leq(s(X), s(Y)) :- leq(X, Y).
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
leq(X, s(s(s(0)))), !,
|
||||
write(X), nl,
|
||||
fail.
|
15
examples/basics/disjunction.pl
Normal file
15
examples/basics/disjunction.pl
Normal file
|
@ -0,0 +1,15 @@
|
|||
likes(alice, pizza).
|
||||
likes(alice, pasta).
|
||||
likes(bob, pasta).
|
||||
|
||||
likes_italian_food(Person) :-
|
||||
likes(Person, pizza) ;
|
||||
likes(Person, pasta).
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
likes_italian_food(alice),
|
||||
write('Alice likes Italian food.'), nl,
|
||||
likes_italian_food(bob),
|
||||
write('Bob likes Italian food.'), nl.
|
20
examples/basics/equality.pl
Normal file
20
examples/basics/equality.pl
Normal file
|
@ -0,0 +1,20 @@
|
|||
check_equal(X, Y) :-
|
||||
X = Y,
|
||||
write('X = Y succeeded'), nl.
|
||||
|
||||
check_identical(X, Y) :-
|
||||
X == Y, !,
|
||||
write('X == Y succeeded'), nl.
|
||||
|
||||
check_identical(_, _) :-
|
||||
write('X == Y failed'), nl.
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
check_identical(A, 13),
|
||||
check_equal(A, 13),
|
||||
check_identical(A, 13),
|
||||
check_equal(42, 42),
|
||||
check_identical(42, 42).
|
||||
|
11
examples/basics/forall.pl
Normal file
11
examples/basics/forall.pl
Normal file
|
@ -0,0 +1,11 @@
|
|||
likes(alice, pizza).
|
||||
likes(alice, pasta).
|
||||
likes(bob, pasta).
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
forall(likes(X, pizza), X = alice),
|
||||
write('Only alice likes pizza.'), nl,
|
||||
forall(likes(X, pizza), X = bob),
|
||||
write('Bob should not like pizza.'), nl.
|
17
examples/basics/fraternity.pl
Normal file
17
examples/basics/fraternity.pl
Normal file
|
@ -0,0 +1,17 @@
|
|||
age(robespierre, 25).
|
||||
age(danton, 29).
|
||||
age(marat, 35).
|
||||
age(camus, 22).
|
||||
age(desmoulins, 19).
|
||||
|
||||
eligible_for_event(Person) :-
|
||||
age(Person, Age),
|
||||
between(20, 30, Age).
|
||||
|
||||
:- initialization(list_eligible_members).
|
||||
|
||||
list_eligible_members :-
|
||||
eligible_for_event(Person),
|
||||
write('Citizen '), write(Person), write(' is eligible for the event.'), nl,
|
||||
fail.
|
||||
|
31
examples/basics/liberty.pl
Normal file
31
examples/basics/liberty.pl
Normal file
|
@ -0,0 +1,31 @@
|
|||
:- dynamic declaration/1.
|
||||
|
||||
add_declaration_first(NewDecl) :-
|
||||
asserta(declaration(NewDecl)).
|
||||
|
||||
add_declaration_last(NewDecl) :-
|
||||
assertz(declaration(NewDecl)).
|
||||
|
||||
database :-
|
||||
add_declaration_first('Man is born free, and everywhere he is in chains.'),
|
||||
retract(declaration(_)),
|
||||
add_declaration_last('The revolution devours its own children.'),
|
||||
add_declaration_first('I disapprove of what you say, but I will defend to the death your right to say it.'),
|
||||
add_declaration_first('Give me Liberty, or give me Death!'),
|
||||
add_declaration_last('So this is how liberty dies, with thunderous applause.').
|
||||
|
||||
show_declarations :-
|
||||
declaration(Decl),
|
||||
write(Decl), nl,
|
||||
fail.
|
||||
|
||||
show_declarations.
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
database,
|
||||
show_declarations,
|
||||
retractall(declaration(_)),
|
||||
show_declarations.
|
||||
|
22
examples/basics/unification.pl
Normal file
22
examples/basics/unification.pl
Normal file
|
@ -0,0 +1,22 @@
|
|||
grade(alice, a).
|
||||
grade(bob, b).
|
||||
grade(carol, a).
|
||||
grade(dave, c).
|
||||
|
||||
got_an_a(Student) :-
|
||||
grade(Student, Grade),
|
||||
Grade = a.
|
||||
|
||||
did_not_get_an_a(Student) :-
|
||||
grade(Student, Grade),
|
||||
Grade \= a.
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
write("While "),
|
||||
got_an_a(X),
|
||||
write(X), write(" got an A, "), fail;
|
||||
write("but "),
|
||||
did_not_get_an_a(Y),
|
||||
write(Y), write(" did not get an A, "), fail; write("unfortunately."), nl.
|
1
examples/basics/write.pl
Normal file
1
examples/basics/write.pl
Normal file
|
@ -0,0 +1 @@
|
|||
:- initialization(main). main :- write('gpl zegt: '), groet(wereld), nl. groet(X) :- write(dag(X)).
|
10
examples/dcg/palimdrome.pl
Normal file
10
examples/dcg/palimdrome.pl
Normal file
|
@ -0,0 +1,10 @@
|
|||
palindrome --> [].
|
||||
palindrome --> [_].
|
||||
palindrome --> [X], palindrome, [X].
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
string_chars("hellolleh", Chars),
|
||||
phrase(palindrome, Chars).
|
||||
|
10
examples/dcg/tree.pl
Normal file
10
examples/dcg/tree.pl
Normal file
|
@ -0,0 +1,10 @@
|
|||
inorder(nil) --> [].
|
||||
inorder(node(L, N, R)) --> inorder(L), [N], inorder(R).
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
main :-
|
||||
T = node(node(nil, b, node(nil, c, nil)), a, node(nil, d, nil)),
|
||||
phrase(inorder(T), Names),
|
||||
write(Names), nl.
|
||||
|
53
examples/lists/merlin.pl
Normal file
53
examples/lists/merlin.pl
Normal file
|
@ -0,0 +1,53 @@
|
|||
% Merlin's Potion lab
|
||||
|
||||
:- dynamic ingredient/1.
|
||||
|
||||
% Rules of the game
|
||||
|
||||
% Add a new ingredient if it's not already present
|
||||
add_ingredient(Item) :-
|
||||
\+ ingredient(Item),
|
||||
assertz(ingredient(Item)),
|
||||
write('Added ingredient: '), write(Item), nl.
|
||||
|
||||
% Use an ingredient (remove it from memory)
|
||||
use_ingredient(Item) :-
|
||||
ingredient(Item),
|
||||
retract(ingredient(Item)),
|
||||
write('Used ingredient: '), write(Item), nl.
|
||||
|
||||
% Make a potion from two ingredients
|
||||
make_potion(Item1, Item2) :-
|
||||
ingredient(Item1),
|
||||
ingredient(Item2),
|
||||
Item1 \= Item2,
|
||||
retract(ingredient(Item1)),
|
||||
retract(ingredient(Item2)),
|
||||
write('You mixed '), write(Item1), write(' and '), write(Item2),
|
||||
write(' to create a potion!'), nl.
|
||||
|
||||
% Show what's left
|
||||
show_ingredients :-
|
||||
findall(X, ingredient(X), List),
|
||||
write('Remaining ingredients: '), write(List), nl.
|
||||
|
||||
% Reset lab
|
||||
reset_lab :-
|
||||
retractall(ingredient(_)),
|
||||
write('Potion lab reset.'), nl.
|
||||
|
||||
% Merlin's recipes
|
||||
|
||||
stock :-
|
||||
add_ingredient(wolfsbane),
|
||||
add_ingredient(mandrake),
|
||||
add_ingredient(unicorn_blood),
|
||||
add_ingredient(phoenix_feather),
|
||||
add_ingredient(dittany).
|
||||
|
||||
brew :-
|
||||
stock,
|
||||
make_potion(wolfsbane, mandrake),
|
||||
show_ingredients.
|
||||
|
||||
:- initialization(brew).
|
74
examples/lists/zebra.pl
Normal file
74
examples/lists/zebra.pl
Normal file
|
@ -0,0 +1,74 @@
|
|||
%% houses(-Solution)
|
||||
% @param Solution is a list of houses that satisfy all constraints.
|
||||
% @author Folklore attributes this puzzle to Einstein
|
||||
% @see http://en.wikipedia.org/wiki/Zebra_Puzzle
|
||||
|
||||
/* Houses logical puzzle: who owns the zebra and who drinks water?
|
||||
|
||||
1) Five colored houses in a row, each with an owner, a pet, cigarettes, and a drink.
|
||||
2) The English lives in the red house.
|
||||
3) The Spanish has a dog.
|
||||
4) They drink coffee in the green house.
|
||||
5) The Ukrainian drinks tea.
|
||||
6) The green house is next to the white house.
|
||||
7) The Winston smoker has a serpent.
|
||||
8) In the yellow house they smoke Kool.
|
||||
9) In the middle house they drink milk.
|
||||
10) The Norwegian lives in the first house from the left.
|
||||
11) The Chesterfield smoker lives near the man with the fox.
|
||||
12) In the house near the house with the horse they smoke Kool.
|
||||
13) The Lucky Strike smoker drinks juice.
|
||||
14) The Japanese smokes Kent.
|
||||
15) The Norwegian lives near the blue house.
|
||||
|
||||
Who owns the zebra and who drinks water?
|
||||
*/
|
||||
|
||||
zebra_owner(Owner) :-
|
||||
houses(Hs),
|
||||
member(h(Owner,zebra,_,_,_), Hs).
|
||||
|
||||
water_drinker(Drinker) :-
|
||||
houses(Hs),
|
||||
member(h(Drinker,_,_,water,_), Hs).
|
||||
|
||||
houses(Hs) :-
|
||||
% each house in the list Hs of houses is represented as:
|
||||
% h(Nationality, Pet, Cigarette, Drink, Color)
|
||||
length(Hs, 5), % 1
|
||||
member(h(english,_,_,_,red), Hs), % 2
|
||||
member(h(spanish,dog,_,_,_), Hs), % 3
|
||||
member(h(_,_,_,coffee,green), Hs), % 4
|
||||
member(h(ukrainian,_,_,tea,_), Hs), % 5
|
||||
next(h(_,_,_,_,green), h(_,_,_,_,white), Hs), % 6
|
||||
member(h(_,snake,winston,_,_), Hs), % 7
|
||||
member(h(_,_,kool,_,yellow), Hs), % 8
|
||||
Hs = [_,_,h(_,_,_,milk,_),_,_], % 9
|
||||
Hs = [h(norwegian,_,_,_,_)|_], % 10
|
||||
next(h(_,fox,_,_,_), h(_,_,chesterfield,_,_), Hs), % 11
|
||||
next(h(_,_,kool,_,_), h(_,horse,_,_,_), Hs), % 12
|
||||
member(h(_,_,lucky,juice,_), Hs), % 13
|
||||
member(h(japanese,_,kent,_,_), Hs), % 14
|
||||
next(h(norwegian,_,_,_,_), h(_,_,_,_,blue), Hs), % 15
|
||||
member(h(_,_,_,water,_), Hs), % one of them drinks water
|
||||
member(h(_,zebra,_,_,_), Hs). % one of them owns a zebra
|
||||
|
||||
next(A, B, Ls) :- append(_, [A,B|_], Ls).
|
||||
next(A, B, Ls) :- append(_, [B,A|_], Ls).
|
||||
|
||||
/** <examples>
|
||||
|
||||
?- zebra_owner(Owner).
|
||||
|
||||
?- water_drinker(Drinker).
|
||||
|
||||
?- houses(Houses).
|
||||
|
||||
*/
|
||||
|
||||
main :- findall(Houses, houses(Houses), Results), write(Results), nl.
|
||||
|
||||
:- initialization(main).
|
||||
|
||||
/* source: SWISH https://swish.swi-prolog.org/example/houses_puzzle.pl */
|
||||
|
28
examples/meta/mib.pl
Normal file
28
examples/meta/mib.pl
Normal file
|
@ -0,0 +1,28 @@
|
|||
% Old mag steeds door New vervangen worden
|
||||
mib(Old, New, Old, New).
|
||||
|
||||
% Een term blijft enkel behouden als het een attoom is
|
||||
% (dat is, niet compound), en als het niet Old is
|
||||
mib(Old, _, Term, Term) :-
|
||||
atomic(Term),
|
||||
Term \= Old.
|
||||
|
||||
% Voor een samengestelde Term
|
||||
mib(Old, New, Pre, Post) :-
|
||||
compound(Pre),
|
||||
functor(Pre, F, N), % Pre heeft naam F en arriteit N
|
||||
functor(Post, F, N), % Post is term met zelfde naam (F) en arriteit (N)
|
||||
mib(N, Old, New, Pre, Post).
|
||||
|
||||
% Extra predicaat om de argumenten te vervangen van een samengestelde term
|
||||
%
|
||||
% N = het nr van het argument (strikt positief)
|
||||
mib(0, _, _, _, _) :- !. % Argument 0 bestaat niet, als we hier komen zijn we klaar.
|
||||
|
||||
mib(N, Old, New, Pre, Post) :-
|
||||
arg(N, Pre, ArgPre), % neem het N-de argument
|
||||
mib(Old, New, ArgPre, ArgPost), % vertaal het
|
||||
arg(N, Post, ArgPost), % unificeer het met het N-de argument van output
|
||||
N1 is N-1,
|
||||
mib(N1, Old, New, Pre, Post). % Herhaal voor argument N-1
|
||||
|
5
examples/program.pl
Normal file
5
examples/program.pl
Normal file
|
@ -0,0 +1,5 @@
|
|||
:- initialization(main).
|
||||
|
||||
main :- write(10),
|
||||
nl,
|
||||
write(hello(world)).
|
Reference in a new issue