Arithmetic and Lists
- Due:
- 5:00am, Wednesday November 6, 2024
Description
Write a Prolog file named p3.pl
that defines the following predicates:
factorial(N, Result)
– compute the factorial of a numberN
and place it inResult
. You can assume thatN
is a non-negative number. For example:?- factorial(0, Result). Result = 1 ; false. ?- factorial(5, Result). Result = 120 ; false. ?- factorial(5, 120). true ; false.
assoc(Property, List, Value)
–List
is an association list of property value pairs. If theProperty
is found as the first one of the pairs inList
, thenValue
is assigned the second of that pair. For example,?- assoc(age,[],V). false. ?- assoc(two,[[one,1],[two,2]],V). V = 2 ; false. ?- assoc(two,[[one,1],[two,2],[one,ichi]],V). V = 2 ; false. ?- assoc(one,[[one,1],[two,2],[one,ichi]],V). V = 1 ; V = ichi ; false. ?- assoc(P,[[one,1],[two,2],[one,ichi]],ichi). P = one ; false.
nth(N, List, Elt)
– place theN
th element ofList
inElt
. For example,?- nth(1, [a,b,c], Elt). Elt = b ; false. ?- nth(5, [a,b,c], Elt). false. ?- nth(2, [a,b,c], b). false. ?- nth(2, [a,b,c], c). true ; false.
Note that nth starts at zero. You may assume that the first argument is a non-negative integer, the second argument is a possibly empty list, and the third argument is an atom or variable.
Additionally, there is a program named escape_room.pl
in the starter code.
Augment this program with a new predicate escape(State, Actions)
where
Actions
will be instantiated with the list of actions to achieve the goal.
For example
?- escape( state(atdoor, onfloor, middle, trapped), A).
A = [walk(atdoor, middle), push(middle, atwindow), climb, climb]
Turning in the Assignment
To submit your assignment, create a zip file named p3.zip
of a
DIRECTORY named p3
containing the following files:
p3.pl
escape_room.pl
Then submit that file to the appropriate folder on D2L.