Chapter 29 Declarative Programming Flashcards

Have Prolog downloaded

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the difference between imperative and declarative languages?

A

In imperative programming languages the programmer writes a sequence of statements that reflect how to solve the problem while is declarative the programmer writes a sequence of statements that reflect what the problem is and it is up to the logic programming system to work out how to get the answer.

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

Which are the three basic constructs of Prolog? Give an example to each one of them.

A

Facts: capitalCities(budapest).
Rules: grandparent(A, B) :- parent(A, P), parent(P, B).
Queries: citieInCountry(budapest).

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

What are clauses?

A

Clauses are facts and rules.

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

What data type does a prolog have?

A

Prolog has a single data type, called ‘term’.

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

What can a term be?

A

A term can be an atom, a general-purpose name with no inherent meaning that alway starts with a lower case letter
A term can be a number, integer or float (real)
A term can be a variable, denotated by an identifier that starts with a capital letter or an underscore (_)
A term can be a compound term, a predicate, consisting of an atom and a number of arguments in parantheses

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

How to test the fact capitalCity(paris).?

A

capitalCity(paris).
You write this first in the Prolog environment
Then you write this in the SWI-Prolog
The output should be true.

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

Where is the variable in cityInCountry(berlin, Country).?

A

The Country is the variable right after berlin,.

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

What happens if you had the program cityInCountry(berlin, germany). in the Prolog environment and wrote cityInCountry(berlin, Country). in the SWI Prolog.

A

The output would be

Country = germany

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

Give an example of anonymus variable

A
Prolog environment:
ingredient(tagine, tomato, 100).
SWI Prolog:
ingredient(tagine, Ingredient, _).
Output
Ingredient = tomato 
'The amount won't be outputed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Give an example of a rule.

A
parent(fred, jack).
parent(fred, alia).
parent(fred, paul).
parent(dave, fred).
grandparent(A, B) :- parent(A, P), parent(P, B).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you debug your code in Prolog environment?

A

To debug the code you go to debug, graphical debugger than write trace. the part of the code you want and than just push the space button in the debugger program to see the program unfold

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

Where does recursion occur?

A

Recursion occurs where a rule is defined by itself or more precisely a rule uses itself as a sub-goal

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

What is the head and tail in a list?

A

The head is the first item and the tail is the rest of the list.

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

Write or say an example of a list.

A

Prolog environment
showHeadAndTail([fred, jack, emma], head, tail).
SWI Prolog
?- showHeadAndTail([H|T], head, tail).
H = fred,
T = [jack, emma].

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