Chapter 29 Declarative Programming Flashcards
Have Prolog downloaded
What is the difference between imperative and declarative languages?
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.
Which are the three basic constructs of Prolog? Give an example to each one of them.
Facts: capitalCities(budapest).
Rules: grandparent(A, B) :- parent(A, P), parent(P, B).
Queries: citieInCountry(budapest).
What are clauses?
Clauses are facts and rules.
What data type does a prolog have?
Prolog has a single data type, called ‘term’.
What can a term be?
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 to test the fact capitalCity(paris).?
capitalCity(paris).
You write this first in the Prolog environment
Then you write this in the SWI-Prolog
The output should be true.
Where is the variable in cityInCountry(berlin, Country).?
The Country is the variable right after berlin,.
What happens if you had the program cityInCountry(berlin, germany). in the Prolog environment and wrote cityInCountry(berlin, Country). in the SWI Prolog.
The output would be
Country = germany
Give an example of anonymus variable
Prolog environment: ingredient(tagine, tomato, 100). SWI Prolog: ingredient(tagine, Ingredient, _). Output Ingredient = tomato 'The amount won't be outputed
Give an example of a rule.
parent(fred, jack). parent(fred, alia). parent(fred, paul). parent(dave, fred). grandparent(A, B) :- parent(A, P), parent(P, B).
How do you debug your code in Prolog environment?
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
Where does recursion occur?
Recursion occurs where a rule is defined by itself or more precisely a rule uses itself as a sub-goal
What is the head and tail in a list?
The head is the first item and the tail is the rest of the list.
Write or say an example of a list.
Prolog environment
showHeadAndTail([fred, jack, emma], head, tail).
SWI Prolog
?- showHeadAndTail([H|T], head, tail).
H = fred,
T = [jack, emma].