Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
teaching
AI
Commits
6a7f22be
Commit
6a7f22be
authored
Oct 31, 2019
by
Leon Schmidtchen
Browse files
Added slides and prolog example for tut1
parent
6fcb7c77
Changes
2
Hide whitespace changes
Inline
Sidebyside
Leon/tut1/examples.pl
0 → 100644
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:
% [HeadTail], 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 Prologlike 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).
Leon/tut1/presentationhandout.pdf
0 → 100644
View file @
6a7f22be
File added
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment