Prolog Flashcards
Define a a predicate append to append one list to another (i.e. append(L1, L2, L1&2))?
append([],L,L).
append([H|T], L2, [H|L3]) :-
append(T,L2,L3).
Write 2 predicates using the append/3 predicate to determine if L1 is a prefix of L2, and if L1 is a suffix of L2 (prefix(X,Y) and sufix(X,Y))?
sufix(X,Y) :- append(_, X, Y).
prefix(X,Y) :- append(X, _, Y).
What does the fail operator do?
The fail operator can never be satisfied and thus forces a fail. Can be used with cut operator for efficiency.
What does enter and ; allow when querying?
; gets the next answer
enter stops query?
What is the cut operator?
! // the cut operator
Prevents backtracing and instantiates the variables made up to this point, and only those will be considered.
Try to negate instead, it’s better practice.
Write a predicate that checks if an elem is in a list?
is_in( [ X , _] , X ).
is_in( [ _ , X ] , Y ) :-
is_in( X , Y ).
Define Prolog from a declarative pov.
The programmer specifies a goal to be achieved and the system works out how to achieve it.
What is a fact?
A basic assertion about the world:
likes(jasmine, pat).
What is a rule (predicate)?
An inference about the facts of the world.
friends(X,Y) :- likes(X,Y), likes(Y,X), + (X=Y).
What is an atom?
They are values which cannot change (lowercase).
What is a variable?
Uppercase strings which can be used to query.
What is an anonymous variable?
_
That is used when we don’t care about the value.
How do we use negation, or equals?
+ (X=Y)
X is not equal to Y
=(X=Y)
X is equal to Y
What is unification?
Finds the values that match both sides.
What is backtracing?
Incrementally build up candidates to a solution and abandons each partial candidates (backtracks) when it determines that the candidate does not satisfy the goal.