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

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

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.

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

View Full Document
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.
Prolog – Arithmetic Example : we can also use arithmetic in compound statements. ?- X is 5, Y is 2 * X. X = 5 Y = 10

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

View Full Document
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
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/03/2011 for the course CSC 301 taught by Professor Hamel during the Spring '08 term at Rhode Island.

### 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
Ask a homework question - tutors are online