Post on 01-Jan-2016
description
The Variety of Programming Languages
D GoforthCOSC 3127
D GoforthCOSC 3127
1. Grouping Languages by Paradigm imperative functional logical object.oriented scripting??
Imperative Languages (Procedural) since 1940’s, high level since 1950’s statement-oriented closest to underlying machine model e.g. Fortran, COBOL, Algol, Pascal, c
ALGOL 60 example: (see p. 60, Sebesta)(part of program, read in array and find sum)comment input integers into array and find sum;begin integer array intlist[1:99]; integer listlen, counter, sum; sum = 0; readint(listlen); if (listlen>0) ^ (listlen < 100) then begin comment loop to read values; for counter := 1 step 1 until listlen do begin readint(intlist[counter]); sum := sum + intlist[counter] end; printint(sum) endend
Functional Languages (Applicative) since 1950’s based on mathematical concept of function data storage is a ‘side-effect’! Command-line interpreter model e.g. LISP, APL, Maple, unix, DOS BASIC is an imperative/functional hybrid
LISP example: (in a pseudo-lisp dialect)
(function to add all numbers in a list ‘lis’; lis may not be a list, or, when it is a list, may contain items others than numbers that can be added
? (define (sumnum lis)
(if ((not (list? lis))
( 0 ))
((number? (firstOf lis))
(plus (firstOf lis)(sumnum (restOf lis))))
( true (sumnum (restOf lis)))))
sumnum
? (sumnum ‘( 4 w (u n 66) 5 3))
12
Logic Languages
since 1970’s based on logical implication expressions (=>) programs determine what combinations of
data make expressions true e.g. Prolog
Prolog example:
(program to find the greatest common divisor of two integers U, V; result is value of W)
gcd(U, 0, U).
gcd(U, V, W) :- not(V=0),
R is U mod V,
gcd(V, R, W).
?- gcd(10, 15, W).
W = 5
?- gcd(20,30,7).
no.
Object-oriented Languages
since 1970’s abstract data types manipulated by
messages invoking methods e.g. Smalltalk, Actor C++, java are imperative/object-oriented
hybrids
Smalltalk example: (see p. 481, Sebesta)
(method to replace two arrays of objects with longer arrays;
the arrays ‘names’ and ‘codes’ are instance variables)
grow | oldNames oldCodes|
oldNames <- names.
oldCodes <- codes.
names <- Array new: names size + 1.
codes <- Array new: codes size + 1.
names replaceFrom: 1 to: oldNames size with: oldNames
codes replaceFrom: 1 to: oldCodes size with: oldCodes
Scripting Languages
since 1950’s based on large libraries of routines e.g. JCL, AWK, Perl
2. Grouping Languages by Kinship Sebesta, p. 39 languages are related to others developed
before many languages mix programming paradigms