ln025 - Prolog Arithmetic P r o lo g is a p r o g r a m m...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Prolog – Arithmetic Prolog is a programming language, therefore, arithmetic is implemented as expected. The only difference to other programming languages is that assignment is done via the predicate is rather than the equal sign, since the equal sign has been used for the unification operator. Examples: ?- X is 10 + 5; X = 15 ?- X is 10 + 5 * 6 / 3; X = 20 Precedence and associativity of operators are respected.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Prolog – Arithmetic Example : write a predicate definition for length/2 that takes a list in its first argument and returns the length of the list in its second argument. length([ ], 0). length(L, N) :- L = [H|T], length(T,NT), N is NT + 1.
Background image of page 2
Prolog – Arithmetic Example : we can also use arithmetic in compound statements. ?- X is 5, Y is 2 * X. X = 5 Y = 10
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Prolog – I/O write(term) is true if term is a Prolog term, writes term to the terminal. read(X) is true if the user types a term followed by a period, X becomes unified to the term. nl
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

ln025 - Prolog Arithmetic P r o lo g is a p r o g r a m m...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online