prolog Flashcards

(21 cards)

1
Q

Give an example of facts, rules and queries all having the same name and arity. In your own words describe why this can be useful when writing Prolog code.

A

% facts
parent(john, mary).
parent(mary, susan).

% rule (same name/arity)
parent(X, Y) :- ancestor(X, Y).

% query (same name/arity)
?- parent(john, mary).

  • Lets you create a single, unified interface.
  • Prolog automatically tries facts first, then rules, so you can extend behaviour without changing queries.
  • This makes code cleaner, more modular, and easier to maintain.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The closed world assumption (CWA) defines the way that Prolog works.

A

The Closed World Assumption (CWA) means that anything not known to be true is assumed false.

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

Explain how the CWA is used in Prolog, in particular describe the consequences to the code of it being used in Prolog.

A

Prolog’s reasoning is based on its database of facts and rules.
If a goal cannot be proven from them, Prolog treats it as false.

Consequences for code

Negation as failure - + succeeds when Prolog cannot prove something, treating “unknown” as “false”.

You must explicitly add all facts that should be considered true; missing facts become false by default.

Incomplete knowledge leads to incorrect “false” answers.

Programmers must ensure the database fully covers all needed cases, or Prolog may draw wrong conclusions.

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

Describe how lists are handled in Prolog. Use examples to illustrate your answer, showing the operations you can perform. How do these operations vary between Lisp and Prolog?

A

Lists in Prolog are recursive structures either defined as an empty list [] or a head tail pair [H|T].

Lists are processed in prolog using unification and pattern matching

?- [H|T] = [1,2,3] % head-tail decomposition
member(X, [a,b,c]). % membership
append([a,b],[1,2],R). % append
length([a,b,c],N). %Length
?- L = [1,2 | [33,4]].  %Constructing lists

Prolog is declarative, using unification and backtracking with pattern matching [H|T], whereas Lisp is functional, using explicit functions like car and cdr.

Prolog predicates are multi-directional, while Lisp list functions are one-way

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

What kind of elements can the elements of a list be? Are there any restrictions on the elements of any given list?

A

Anything Prolog allows as a term: atoms, numbers, variables, lists, or complex structures.

Examples:

[1, a, foo(bar), _X, [2,3]]

Restrictions?
No type restrictions: elements in the same list can be of different types and mixed freely. The only rule is that lists must be properly formed using [] and |.

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

Write a Prolog rule that takes a list as its argument. Each of the
elements of the list is itself a list containing integers. Your rule should
have a 2nd argument that is used to ‘return’ a list, whose elements are
the lists from the 1st argument but with their values doubled. You can
assume that all of the elements in the list are integers.
For example:

?- doubleLists([ [5, 4], [2, 3], [3, 1] ], Res).
Res = [[10, 8], [4, 6], [6, 2]]]
A
doubleLists([],[]).

doubleLists([H|T], [DoubledH|DoubledT]) :-
   doubleList(H, DoubledH),
   doubleLists(T, DoubledT).
	 
doubleList([], []).

doubleList([H|T],  [DoubledH|DoubledT] ) :-
  doubleList(T, DoubledT),
	DoubledH is H * 2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write code in Prolog that adds the value and the position of each
element of the list if the position is even. You can assume that all of the
elements in the list are numbers and the 1st element in the list is in
position1. For example:

?-add_List( [5, 4, 3, 2, 1], Res).
Res = [5, 6, 3, 6, 1] – eg [5, 4+2, 3, 2+4, 1]
A
add_List(L,Res) :-
    add_List(L, 1, Res).
		
add_List([],_,[]).

add_List([H|T], Pos, [R|RT]) :-
    ( Pos mod 2 =:= 0
		-> R is H + Pos
		; R = H
		),
		NewPos is Pos + 1,
		add_List(T,NewPos,RT).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a Prolog rule which take two lists as arguments. They should
create two new lists, the first of which contains all of the numbers less
than 10 from both lists and the 2nd list contains all of the other numbers
from both lists. For example:

?-oddEven([6, 11, 34, 2, 1] , [13, 5, 10, 21], Small, Large).
Small = [6, 2, 1, 5]
Large = [11, 34, 13, 10, 21]
A
oddEven(L1,L2, Small, Large) :-
		append(L1,L2, All),
		split(All, Small, Large).
		
	split([],[],[]).
		
split([H|T], [H|Small], Large) :-
		H < 10,
		split(T, Small, Large).

split([H|T], Small, [H|Large]) :-
		H >= 10,
		split(T, Small, Large).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a Prolog rule takes a list as its argument. Each of the elements of
the list is itself a list containing integers. Your rule should have a 2nd
argument that is used to ‘return’ a list, whose elements are the sums of
the lists in the original list. You can assume that all of the elements in
the list are integers. For example:

?- sumLists([ [1, 2], [3, 4], [5, 6] ], Res).
Res = [ 3, 7 11]
A
sumLists([], []).

sumLists([H|T], [R|RT]) :-
		sumList(H, R),
		sumLists(T,RT).
		
sumList([],0).

sumList([H|T], Sum) :-
		sumList(T,RestSum),
		Sum is H + RestSum.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write code in Prolog that multiplies the elements in a list by its position
in the list. You can assume that all of the elements in the list are
numbers and the 1st element in the list is in position1. For example:

?-mult_List( [2, 6, 8, 12, 11], Res).
Res = [2, 12, 24, 48, 55] – eg [2*1, 6*2, 8*3, 12*4, 11*5]
A
multi_List(L, Res) :-
	multi_List(L, 1, Res).
	
multi_List([], _, []).

multi_List([H|T], Pos, [R|RT]) :-
		R is Pos * H,
		NewPos is Pos + 1,
		multi_List(T, NewPos, RT
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write Prolog rules which take two lists as arguments. They should
create two new lists, the first of which contains all of the even numbers
from both lists and the 2nd list contains all of the odd numbers from both
lists. For example:

?-oddEven([1, 2, 3, 4, 5, 6] , [7, 8, 9, 10, 11], Even, Odd).
Even = [10, 8, 6, 4, 2]
Odd = [11, 9, 7, 5, 3, 1]
A
oddEven(L1, L2 , Even, Odd) :-
		append(L1,L2,All),
		oddEvenSingle(All, Even, Odd).
		

oddEvenSingle([],[],[]).

oddEvenSingle([H|T], [H|Even], Odd) :-
		H mod 2 =:= 0,
		oddEvenSingle(T, Even, Odd).
		
oddEvenSingle([H|T], Even, [H|Odd]) :-
		H mod 2 =:= 1,
		oddEvenSingle(T, Even, Odd).
		
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a Prolog rule that finds the sum of all of numbers in even
positions in a list and the sum of all of the numbers in odd positions in
the list. You can assume that all of the elements in the list are numbers
greater than 0. For example:

?- findSums([2, 5, 4, 6, 3], SumOdd, SumEven).
SumOdd = 9 (2 + 4 + 3)
SumEven = 11 (5 + 6)
A
findSums([],0,0).

findSums([H], H, 0).

findSums([H1,H2| T], SumOdd, SumEven) :-
		findSums(T, RestSumOdd, RestSumEven),
		SumOdd is H1 + RestSumOdd,
		SumEven is H2 + RestSumEven. 
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write code in Prolog that, for every element in a list adds an element’s
position to that element. You can assume that all of the elements in the
list are numbers. For example:

?-increase( [2, 5, 3, 1], X).
X = [3, 7, 6, 5] - eg (2+1, 5+2, 3+3, 1+4)
A
increase(L, X) :-
		increase(L, 1, X).
		
increase([],_,[]).

increase([H|T], Pos, [HX|TX]) :-
		HX is Pos + H,
		NewPos is Pos + 1,
		increase(T, NewPos, TX).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write Prolog rules which take two lists as arguments. They should
create a third list which contains elements that are the sum of the
elements at each corresponding position from both lists. If the lists are
of different length then the new list should just contain the remaining
elements from the longer list. You can assume that all of the elements
in the lists are numbers. For example:

?-combineLists([9, 5, 2, 8] , [1, 2, 3, 4, 5, 6], Summed).
Summed = [10, 7, 5, 12, 5, 6] - eg (9+1, 5+2, 2+3, 8+4, 5, 6)
A
combineLists([],[],[]).
		
combineLists([],L, L).
combineLists(L, [], L).
		
combineLists([H1|T1], [H2|T2], [R|RT]) :-
			R is H1 + H2,
			combineLists(T1, T2, RT).
		
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a Prolog rule that finds the sum of all of the odd numbers in a list
and the sum of all of the even numbers in the list. You can assume that
all of the elements in the list are numbers greater than 0. For example:

?- findSums([1, 2, 3, 4, 5, 6, 7, 8], SumOdd, SumEven).
Sum = 16
SumEven = 20
A
findSums([],0,0).

findSums([H|T], SumOdd, SumEven) :-
		findSums(T, RestOdd, RestEven),
		( H mod 2 =:= 0
		-> SumEven is H + RestEven,
				SumOdd is RestOdd
		; SumOdd is H = RestOdd,
			SumEven is RestEven
		).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write code in Prolog that doubles and then adds 1 to every number in
the list. You can assume that all of the elements in the list are numbers.
For example:

?-double_plus_one( [1, 2, 3, 4, 5, 6, 7], X).
X = [3,5,7,9,11,13,15]
17
Q

Write Prolog rules which take two lists as arguments. They should
create a third list which contains all of the even numbers from the 1st list
followed by the odd numbers from the 2nd list. For example:

?-combineLists([4,5,6,7,8,9,3] , [1,2,3,4,5,6,7,8,9,10], EvenOdd).
EvenOdd = [4,6,8,1,3,5,7,9]
18
Q

Write code in Prolog that doubles every even number in a list – the odd
numbers are left alone. You can assume that all of the elements in the list are
numbers. For example:

?-double_even( [1, 2, 3, 4, 5, 6, 7], X).
X = [1 4 3 8 5 12 7]
19
Q

Write a Prolog rule that finds the sum of all of the odd numbers in a list. You
can assume that all of the elements in the list are numbers greater than 0. For
example:

?- findSumOdd([1, 2, 3, 4, 5, 6, 7, 8], SumOdd).
SumOdd = 16
20
Q

Write code in Prolog that doubles every even number in a list – the odd
numbers are left alone. You can assume that all of the elements in the list are
numbers. For example:

?-double_even( [1, 2, 3, 4, 5, 6, 7], X).
X = [1 4 3 8 5 12 7]
21
Q

Write Prolog rules which take two lists as arguments. They should count the
number of elements in the 2nd list that are equal to the last element in the 1st
list. For example:

?-countEqualLast([4,5,6,7,8,9,3], [1,2,3,4,3,5,3,6,7,3,3], Count).
Count = 5
As 3 is the last element in the first list and there are 5 3’s in the 2nd list.