Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
teaching
AI
Commits
d5d5c12c
Commit
d5d5c12c
authored
Nov 11, 2019
by
jfschaefer
Browse files
added today's prolog file
parent
1e8c9d71
Changes
2
Hide whitespace changes
Inline
Side-by-side
Frederik/README.md
View file @
d5d5c12c
...
...
@@ -7,3 +7,4 @@ AI 1 - 2019/2020
*
2019-10-28:
`prolog/2019-10-28-tutorial.pl`
(simple prolog exercises)
*
2019-10-31 (substition for Katja):
`prolog/2019-10-31-tutorial.pl`
(simple prolog exercises)
*
2019-11-04:
`prolog/2019-11-04-trees.pl`
(some simple tree exercises)
*
2019-11-11:
`2019-11-11-nary-trees.pl`
(tree exercise in preparation for tree-search homework)
Frederik/prolog/2019-11-11-nary-trees.pl
0 → 100644
View file @
d5d5c12c
% The tree definition from the homework assignment.
istree
(
tree
(
Value
,
Children
))
:-
string
(
Value
),
subtrees
(
Children
).
subtrees
([]).
subtrees
([(
Cost
,
T
)|
Rest
])
:-
number
(
Cost
),
istree
(
T
),
subtrees
(
Rest
).
% An example tree.
% If you want to call e.g. addweights with this tree, you can do it with the following trick:
% tree1(X), addweights(X, Sum).
tree1
(
tree
(
"A"
,
[(
2
,
tree
(
"B"
,
[(
1
,
tree
(
"E"
,
[])),
(
2
,
tree
(
"F"
,
[]))])),
(
5
,
tree
(
"C"
,
[(
2
,
tree
(
"G"
,
[]))])),
(
2
,
tree
(
"D"
,
[(
1
,
tree
(
"X"
,
[(
1
,
tree
(
"Y"
,
[]))]))]))])).
% Add up all the weights of the edges in the tree.
% (we called it cw in the tutorial)
addweights
(
tree
(
_
,
[]),
0
).
addweights
(
tree
(
_
,
[(
X
,
T
)|
Rest
]),
S
)
:-
addweights
(
T
,
S1
),
addweights
(
tree
(
"asdf"
,
Rest
),
S2
),
S
is
X
+
S1
+
S2
.
% Also adds up all the weights, but in a different way:
% In the previous attempt, we always expected a tree as argument and constructed trees on demand.
% In this approach we'll take a list of (Cost, Tree) pairs instead.
% This is not as pretty, but a useful approach for some tree search algorithms.
addweights2
([],
0
).
addweights2
([(
X
,
tree
(
_
,
Children
))|
Rest
],
S
)
:-
addweights2
(
Rest
,
S1
),
addweights2
(
Children
,
S2
),
S
is
S1
+
S2
+
X
.
addweights2
(
T
,
S
)
:-
istree
(
T
),
addweights2
([(
0
,
T
)],
S
).
Write
Preview
Supports
Markdown
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