.
c) Is your cut in part b) green or red?
Please define a predicate count(A,T,L), which is fulfilled if
term A appears N times in list L.
Examples:
?- count(a, [a,b,b,a], N).
N = 2
?- count(a, [a,X,a,b,Z, N).
N = 2
?- count(X, [a,X,Y,b], N).
N = 1
?- count([a], [a,[a],X,[a,b]], N).
N = 1
Suppose given are a list of properties, for example [animate, male, canine, feline], and a list of clauses such as
animate(fred).
animate(alice).
male(fred).
canine(alice).
feline(fred).
Please define a predicate checkprops(P,L), where P is a person and L
a list of properties. checkprops(A,L) is fulfilled, if P has all properties from L.
Examples:
?- checkprops(fred, [animate, male]).
yes
?- checkprops(alice, [animate, feline]).
no
Please define a predicate filter(P,L1,L2), which is fulfilled if list L2 contains all
elements from list L1 fulfilling predicate P.
Example:
pos(X) :- X > 0.
neg(X) :- X < 0.
?- filter(pos,[1,2,0,5,-5,-6,8],L).
L = [1,2,5,8]
?- filter(neg,[1,2,0,5,-5,-6,8],L).
L = [-5,-6]
Please define a predicate variables(T,L), which is fulfilled if L
is the list of variables appearing in term T.
Please define a predicate count(A,T,N), which is fulfilled if
term A appears N times in term T.
Examples:
?- count(a, f(a), N).
N = 1
?- count(a, f(a, g(b,a)), N).
N = 2
?- count(a, f(a,X)), N).
N = 1
?- count(f(a), f(a,g(f(a),f(a))), N).
N = 2
Please define a predicate powerset(L1,L2) using bagof.
The following predicate gives the N-th Fibonacci number.
fib(0,1).
fib(1,1).
fib(N,F) :- N >1, N1 is N-1, N2 is N-2, fib(N1,F1), fib(N2,F2), F is F1 + F2.
Please improve this predicate using assert.
Please implement counters in Prolog:
A counter has a name and a value and should provide
operations init/2, getvalue/2, incr/1, decr/1, and del/1.
Example:
?- init(z,3).
true
?- getvalue(z,V).
V = 3
?- incr(z), decr(z), decr(z).
true
?- getvalue(z,V).
V = 2
?- del(z).
true
?- getvalue(z,V).
no