Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Outline - temporal
Logic Languages
2
Outline - exemplar
Here we give an example algorithm that we will code in several dierent languages. Seive of Eratostenes - Aim to nd the rst primes
Given {1..n}, starting at 2 going up remove from the set successive multiples of each number in the set, what remains are the primes.
Optimisations:
1. only need to go up to
Pascal Version
PROGRAM seive(output); CONST maxlim=100; TYPE range= 1..maxlim; intset=SET OF range; VAR primes:intset; i,k,j:integer; BEGIN primes:=[2..maxlim]; k:=1; FOR i IN primes DO BEGIN j:=i*(k+1);k:=i; WHILE j<= maxlim DO BEGIN primes:=primes-[j]; j:=j+i; END; END; primes:=primes+[1]; FOR i IN primes DO WRITELN(i);
END.
Time/conceptual grid Imperative assemblers Fortran, Algol60 Simula Algol68 Pascal,C ADA C++, Fortran90 Java C# Interpreter autocode Array < 1958 1960 1964 1967 1970 1980 1985 1990 1995 2002
Lisp
A+ J Matlab ZPL
Note that in the last period array and object oriented concepts become common
1. The automatic translation of algebraic expressions to machine code. FORTRAN stood for FORmula TRANslator.
2. The use of functions or subroutines. (a) Recursion - provided in algol60 but not FORTRAN. (b) Block structure - algol60.
1. Strong types
3. Records or structures
4. Typed modular compilation - Fortran had provided separate compilation but with no type checking.
2. Introduction of facilities for parallel processing Fortran90 and F, these are borrowed from array processing languages.
Array Languages Iversons notation used as a mathematical notation in the 50s prior to use in programming A Programming Language (APL) introduced 1962 Arrays as built in type All operations extend over arrays Interpretive/interactive Special mathematical typeface used
APL continued Very compact notation Widely used for interactive calculations prior to the introduction of spreadsheets Type face hindered further use
10
Second Generation Array Languages A+, J, K All of these allow ascii keywords or operators either exclusively or as alternative to special symbols. We will look at the problem of nding primes in A+ rst program to nd all prime numbers <= specied integer as an introduction initially to the special notation and then give ascii versions.
11
PRIMES n:{(~R,R.R)/R
2 n}
This one line function does not use eratosthenes, we will get that later. Notes:
1. APL processes expressions from right to left, except when the order of evaluation is changed by using parentheses.
2. Example assumes "0-origin" for generating "all integers up to R" (iota function).
3.
n returns the sequence of integers from 0 to n 1 thus 7 gives (0,1,2,3,4,5,6) 2 n which drops the rst two elements from the list so 2 7 gives
12
4. R is assigned
R=(2,3,4,5,6)
6. This is then turned into a one dimensional array using the , operator in ,R.R giving (4,6,8,10,12,6,9,12,15,18,...,24,30,36)
is then
used to test each member of R for its presence in this vector this returns the boolean vector (0,0,1,0,1), which is then negated with the ~ operator to return (a) (1, 1, 0, 1, 0) = (2, 3, 4, 5, 6) (4, 6, 8, 10, 12, 6..., 30, 36)
8. The / operator means select from, so (1,1,0,1,0)/(2,3,4,5,6) selects (2,3,5) which is the answer. Only those elements on the right whose corresponding left element is true are selected.
The same algorithm can be written in ASCII as PRIMES n:{(~R in ,R*.R)/R:=2 drop iota n}
Eratosthenes version
R holds a remainder after division by next ^/1mask removes the head of mask and returns true if all elements in the list are true , concatenates two lists & stands for this function during recursion
13
Call with
14
14-1