Chapter 7 - Working with lists in Prolog Flashcards

1
Q

Lists

A

symbolic structures; sequence of objects that are called elements

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

Lists can help to..

A

write clauses for predicates that are very similar except for the number of arguments they take

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

Valid lists examples in Prolog

A

[anna,karenina]
[john,paul,george,ringo,zeppo]
[1,2,3,3,2,1]
[]

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

Can lists contain other lists aswell?

A

Yes:
[[john,23],[mary,14],[hello]]
[8,john,[’199y’,john]]
[[]]

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

Is a one element list different from the element itself?

A

Yes:

[anna] is different from anna ;
[[]]is different from[].

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

Head

A

First element of a non-empty list. Can be anything.

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

Tail

A

list that is formed by removing the first element from a list
[a,b,c,d] has head a and tail[b,c,d];
[[a],b,[c]] has head [a] and tail [b,[c]];
[a,[b,c]] has head a and tail [[b,c]];

A tail MUST be a list

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

Prolog term

A

Constant, variable, number and LIST

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

Two ways for Prolog to write lists:

A
  1. A left square parenthesis, followed by a sequence of terms separated by commas and terminated by a right square parenthesis.
  2. A left square parenthesis, followed by a nonempty sequence of terms separated by commas, followed by a vertical bar, followed by a term denoting a list, and terminated by a right square parenthesis.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

List –> [X|Y, Z]

A

list who head is X and whose tail is the list with head Y and tail Z.

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

Are [1 , X , 3 | [ ] ] and [1,X,3] the same list?

A

Yes

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

Unification with lists

A
  1. Two lists without variables are considered to unify when they are identical, element for element.
  2. Two lists with distinct variables are considered to unify when the variables can be given values that make the two lists identical, element for element.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Not-unification with lists

A

Lists will not unify if they have different numbers of elements or if at least one corresponding element does not unify.

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

Unification with lists take-away

A

X matches anything, including any list.
[X] matches any list with exactly one element.
[X|Y] matches any list with at least one element.
[X,Y] matches any list with exactly two elements.

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

What will the following query output?

member(X,[a,b,c]).

A

X=a ;
X=b ;
X=c ;
No

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

What will the following query output?

member(3,L), 1=2.

A

ERROR: Out of global stack

each time the subquery 1=2, fails, the program backtracks to try to find a new value for L. Since there will always be another value to consider (that is, a bigger list), the process runs until L gets so large that it causes an error.

17
Q

Can variables that are not instantiated be used in lists in Prolog?

A

Yes, unlike numbers

18
Q

Examples of queries that can be solved with the “append” predicate

A
  1. What pairs of lists when joined give [a,b,c]?
    append(X,Y,[a,b,c]).
  2. [a,b] joined to what list L gives [a,b,c,d,e,f]?
    append([a,b],L,[a,b,c,d,e,f]).
  3. Solve for the variables X, Y, and L.
    append([X,b],[d|L],[a,_,Y,e,f]).
19
Q

front(L1,L2).

A

holds if the listL1is the start of listL2:

front(L1,L2) :- append(L1,_,L2).

20
Q

last(E,L).

A

holds if E is the last element of list L :

last(E,L) :- append(_,[E],L).

21
Q

another version of the member predicate using the append predicate

A

elem2(E,L) :- append(,[E|],L).

Explanation: is an element of L if L is a list that has some number of elements, then E, then some number of other elements.

22
Q

Will this version of the before predicate run?

before(X,Y,L) :- append(,[X|],Z), append(Z,[Y|_],L).

A

No, because there are infinitely many lists Z that contain X, and it may turn out that none of them satisfy the second query.

23
Q

Will this version of the before predicate run?

before(X,Y,L) :- append(Z,[Y|],L), append(,[X|_],Z).

A

Yes, follow this logic:

  1. join Z to a list whose head is Y, results in a list L;
  2. there is a list Z such that X appears somewhere in Z
    Since 1. specifies that Z comes AFTER Y, then automatically whatever is inside Z (X in this case), also comes after Y.