chore: Demo & examples

This commit is contained in:
Tibo De Peuter 2025-05-18 23:27:35 +02:00
parent 61178cbeac
commit b294467587
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
9 changed files with 228 additions and 23 deletions

View file

@ -1,15 +0,0 @@
accumulator :-
Sum = 0,
shift(Number),
NewSum is Sum + Number,
write("Result is: "), writeln(NewSum).
main :-
reset(accumulator, Number, Cont),
between(1, 5, Number),
forall()
call(Cont),
writeln("End of calculation").
:- initialization(main).

View file

@ -4,7 +4,7 @@ printer :-
shift(Name),
write(Name), write(", ").
main :-
ceremony_main :-
writeln("/Ceremony starts/"),
reset(printer, Name, Cont),
\+ \+ ( Name = "John", call(Cont) ),
@ -12,4 +12,4 @@ main :-
writeln("and my parents!"),
writeln("/Ceremony ends/").
:- initialization(main).
:- initialization(ceremony_main).

View file

@ -12,11 +12,11 @@ test_ :-
X is 1 + (2 * Y),
write("In test X = "), write(X), writeln("; done").
main :-
continuations_main :-
test(Cont, Term),
\+ \+ ( writeln("Calling Cont(2)"),
Term = 2, call(Cont)),
\+ \+ ( writeln("Calling Cont(4)"),
Term = 4, call(Cont)).
:- initialization(main).
:- initialization(continuations_main).

15
examples/meta/ground.pl Normal file
View file

@ -0,0 +1,15 @@
ground(T) :-
nonvar(T), atomic(T),
!.
ground(T) :-
nonvar(T), compound(T),
functor(T, _, N),
ground(T, N).
ground(T, N) :-
N \== 0,
arg(N, T, A),
ground(A),
M is N - 1,
ground(T, M).
ground(T, 0).

View file

@ -0,0 +1,27 @@
interpreter :-
interpreter_prompt,
read(Goal),
interpreter(Goal).
interpreter_prompt :- write('Next Command ?-- ').
interpreter(exit) :- !.
interpreter(Goal) :-
ground(Goal), !,
solve_ground(Goal),
interpreter.
interpreter(Goal) :-
solve(Goal),
interpreter.
solve_ground(Goal) :-
call(Goal),
!,
writeln('True :)').
solve_ground(_) :- writeln('False :(').
solve(Goal) :-
call(Goal),
writeln(Goal),
fail.
solve(_) :- writeln('No more solutions :/').

View file

@ -40,9 +40,9 @@ example3 :-
mib(f, e, Result2, Result3),
write(Result3), nl.
main :-
mib_main :-
example1,
example2,
example3.
:- initialization(main).
:- initialization(mib_main).

14
examples/meta/my_list.pl Normal file
View file

@ -0,0 +1,14 @@
% my_member(Element, List) - checks if Element is in List
my_member(Element, Element) :- atomic(Element).
my_member(Element, Compound) :-
compound(Compound),
Compound =.. [Head, Tail],
(Element = Head ; my_member(Element, Tail)).
% my_length(List, Length) - finds the my_length of a list
my_length(Atom, 1) :- atomic(Atom).
my_length(Compound, N) :-
compound(Compound),
Compound =.. [_, Tail],
my_length(Tail, M),
N is M + 1.