Prolog Flashcards

1
Q

Define a a predicate append to append one list to another (i.e. append(L1, L2, L1&2))?

A

append([],L,L).
append([H|T], L2, [H|L3]) :-
append(T,L2,L3).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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))?

A

sufix(X,Y) :- append(_, X, Y).

prefix(X,Y) :- append(X, _, Y).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the fail operator do?

A

The fail operator can never be satisfied and thus forces a fail. Can be used with cut operator for efficiency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does enter and ; allow when querying?

A

; gets the next answer

enter stops query?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the cut operator?

A

! // 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a predicate that checks if an elem is in a list?

A

is_in( [ X , _] , X ).
is_in( [ _ , X ] , Y ) :-
is_in( X , Y ).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define Prolog from a declarative pov.

A

The programmer specifies a goal to be achieved and the system works out how to achieve it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a fact?

A

A basic assertion about the world:

likes(jasmine, pat).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a rule (predicate)?

A

An inference about the facts of the world.

friends(X,Y) :- likes(X,Y), likes(Y,X), + (X=Y).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is an atom?

A

They are values which cannot change (lowercase).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a variable?

A

Uppercase strings which can be used to query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an anonymous variable?

A

_

That is used when we don’t care about the value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do we use negation, or equals?

A

+ (X=Y)

X is not equal to Y

=(X=Y)
X is equal to Y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is unification?

A

Finds the values that match both sides.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is backtracing?

A

Incrementally build up candidates to a solution and abandons each partial candidates (backtracks) when it determines that the candidate does not satisfy the goal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Lists

A

[1,2,3]

Can be deconstructed:
[ Head | Tail ]

17
Q

Arithmetic example?

A

pop(usa, 313)
area(usa, 826)

density(X,Y) :-
pop(X, Pop) , area(X, Area), Y is Pop / Area.

18
Q

Predicate to find duplicate consecutive elements in a list

A

find_dup([X, X|], X).
find_dup([
|Tail], X) :-
find_dup(Tail, X).

duplicate([1,2,3,3,4,5,6,6], M) => M = 3.

19
Q

Using greater / Lesser than?

A

smart_invention(X) :-
invented(Y,X),
iq(Y,Z),
Z >= 160.