Compare and Contrast

advertisement
Compare and Contrast
What types of languages do we have?
1. Imperative (e.g. FORTRAN, C, Pascal, Java
imperative statements, etc.)
 Computations are performed by assigning
values to variables
 Prevalent features are variables, assignment
statements, and iteration
o Variable: memory cell
o Assignment statement: piping
o Iteration: efficient repeat
 Based on Van Newman architecture:
o CPU, memory, bus in between
o CPU has address of next line of code, but
actual program code and data are held in
memory, piped over the bus, and executed
in CPU
o Bottleneck: bus and memory speeds are
much lower than CPU speed
 CPU -> bus -> RAM -> .. -> disk
o Fetch-execute cycle:
 CPU requests next line of code from
the memory (i.e. fetches the next
instruction) and increments program
counter;
 decodes the instruction;
 executes the instruction (e.g. requests
data from memory, computes, and
puts the results back into memory)
2. Object oriented (e.g. C++, Java, etc.)
 Grew out of imperative languages
 Encapsulate data and operations in objects
 Have inheritance and dynamic type binding
3. Functional (e.g. Lisp, Haskel, ML)
 Computations are performed by applying
functions to parameters
4. Logic (e.g. Prolog)
 Use mathematical logic.
 A program consists of rules, in no specific
order. Usually, each rule is a predicate
(something to evaluate to true/false, e.g.
Boolean or if-else statement). Execution of
program means finding the rules that apply,
i.e. there is inference procedure.
5. Scripting (e.g. shell scripts, Perl, Scheme, JavaScript)
 Based on regular expression and pattern
matching.
Languages can be:
 (strongly) typed
 Not typed
Some other criteria to evaluate languages:
 Readability – easy to understand?
 Writeability – easy to write?
 Reliability – always works?
 Safety – error catching, etc.
 Simplicity –e.g. how large is the language,
operator overloading, different ways to do
the same thing, …
 Orthogonality
 Cost – to write, compile, debug, maintain,
upgrade, execute, …

Orthogonality: use of some language features does not
affect other features.
 For example, + is overloaded to add numbers
as well as strings.
 Orthogonality improves readability and
writeability, can increase simplicity, but can
lead to confusion
Languages can be implemented as:
 Compiled: source code gets transformed
into executable file, errors are reported
o Fast! once the executable is produced…
 Interpreted: source code is taken line by line
by the interpreter
o Fast! if there are no loops and the code
is short…
 Hybrid: both
o E.g. Java is compiled into byte code,
then interpreted
A View of Computer System
Compiled language implementation
8 phases of compiling: the above plus symbol table
processing and error detection and reporting
Lexical analyzer: basic typo errors, i.e. detecting words/tokens
that are not present in this particular language
Syntax analyzer: statements not following the rules of this
language
Semantic analyzer: statements that make no sense in this
language
Interpreted language implementation
Hybrid implementation
Examples
Imperative (and typed) language:
int x
x =2+3
Scripting language:
$x = 2+3
@x = 2+3
Functional language:
(setf x (+ 2 3))
Logic language:
cannot assign x=2+3, but can check that 5=2+3 is true.
Assigning
to
Java,
C/C++
LISP
Perl
Python
variable
x=10
(setf x 10)
$x=10
X=10
Array
element
a[0]=10
(setf (aref a 0) 10)
$a[0]=10
a[0]=10
(setf (gethash ‘key
hash) 10)
$hash{‘key’}
= 10
hash[‘key’]=
10
$o->{‘field’}
= 10
o.field = 10
Hash table N/A
entry
Field in
object
o.field = (setf (field o) 10)
10
Factorial example in different languages:
http://www.codecodex.com/wiki/index.php?title=Calcul
ate_the_factorial_of_a_number#C.2FC.2B.2B
Download