[tutor] Update assignment

This commit is contained in:
tolauwae 2025-03-30 21:56:24 +02:00
parent 39c3af4ba5
commit ca687c0136
No known key found for this signature in database
GPG key ID: 20E068EB5B132116
18 changed files with 438 additions and 14 deletions

View 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).

View 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
View 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.

View 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.

View 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
View 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.

View 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.

View 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.

View 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
View file

@ -0,0 +1 @@
:- initialization(main). main :- write('gpl zegt: '), groet(wereld), nl. groet(X) :- write(dag(X)).