Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
teaching
AI
Commits
6a7f22be
Commit
6a7f22be
authored
Oct 31, 2019
by
Leon Schmidtchen
Browse files
Options
Downloads
Patches
Plain Diff
Added slides and prolog example for tut1
parent
6fcb7c77
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Leon/tut1/examples.pl
+96
-0
96 additions, 0 deletions
Leon/tut1/examples.pl
Leon/tut1/presentation-handout.pdf
+0
-0
0 additions, 0 deletions
Leon/tut1/presentation-handout.pdf
with
96 additions
and
0 deletions
Leon/tut1/examples.pl
0 → 100644
+
96
−
0
View file @
6a7f22be
peter
.
%?- peter.
likes
(
peter
,
mary
).
likes
(
peter
,
sophie
).
cool
(
X
)
:-
likes
(
peter
,
X
).
%?- trace, cool(sophie).
%?- trace, cool(X).
has_wheels
(
mybike
,
2
).
has_wheels
(
mytricycle
,
3
).
has_wheels
(
mytoycar
,
4
).
has_wheels
(
mycar
,
4
).
has_motor
(
mybike
).
has_motor
(
mycar
).
car
(
X
)
:-
has_wheels
(
X
,
4
),
has_motor
(
X
).
%?- trace, car(Y).
% Cool cool cool, but how can we code with this?
% The answers is for the most part...RECURSION!
nat
(
zero
).
nat
(
s
(
X
))
:-
nat
(
X
).
% add(X, Y, Z) -> X + Y = Z
add
(
X
,
zero
,
X
).
add
(
X
,
s
(
Y
),
s
(
Z
))
:-
add
(
X
,
Y
,
Z
).
% We see, there is no "return" in Prolog, only predicates.
% For every predicate please state in a comment which parameters
% are input and which are output.
%% List comprehension:
% list e.g. [1,2,3] or [jamie,cersei], empty list = []
% pattern matching with lists:
% [Head|Tail], where Head is an element and Tail is a List!
% Also works with first 2 elements [X1, X2 | T]
append
([],
L2
,
L2
).
append
([
H
|
T
],
L2
,[
H
|
Res
]):-
append
(
T
,
L2
,
Res
).
% A practical example
% Signature for our problem:
% countN(N : int, L : List[int]) -> int
% counts the occurrences of N in L and returns the result.
% With Prolog-like lists: countN(N, [H | T]) -> int
% L = [2, 3, 5, 2, 7, 1, 2, 2, 5, 9]
% countN(3, L) -> 1
% countN(2, L) -> 4
% countN(8, L) -> 0
% countN(5, L) -> 2
% Delcarative Approach
% countN(N, L) -> res
% res = 0
% for e in L:
% if e == N:
% res += 1
% return res
% Functional / Recursive Approach
% Base Cases:
% countN(N, []) -> return 0
% Recursive Case:
% countN(N, [H | T]) ->
% if N == H
% return countN(N, [T]) + 1
% else
% return countN(N, [T])
% ... in prolog we can't "return", so we need another variable in the predicate
% countN(N, L, RES) the number of occurrences of N in list L
% will be "returned/safed" in RES (Will be in relation with RES)
% Base Cases:
countN
(
_
,
[],
0
).
%?- trace, countN(2, [], X).
% Recursive Case:
countN
(
N
,
[
N
|
T
],
RES
)
:-
countN
(
N
,
T
,
OLDRES
),
RES
is
OLDRES
+
1
.
countN
(
N
,
[
M
|
T
],
RES
)
:-
not
(
N
=
M
),
countN
(
N
,
T
,
RES
).
% Point out: "N" in first line list compr. "is" can be very useful
%?- trace, countN(2, [2], X).
This diff is collapsed.
Click to expand it.
Leon/tut1/presentation-handout.pdf
0 → 100644
+
0
−
0
View file @
6a7f22be
File added
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment