Lecture HPC 7 Programming Paradigms
Lecture HPC 7 Programming Paradigms
1. Imperative.
2. Structured.
3. Procedural.
4. Object-Oriented.
5. Functional.
1
Paradigms II
• Idiomatic programming.
2
Imperative, structured, and
procedural
Imperative
• Oldest approach.
• Closest to the actual mechanical behavior of a computer⇒ original imperative languages were
abstractions of assembly language.
• A program is a list of instructions that change a memory state until the desired end state is achieved.
• Difficult to scale.
3
Structured
• Structured program theorem (Böhm-Jacopini): sequencing, selection, and iteration are sufficient to
express any computable function.
4
Procedural
• Divide the code into procedures: routines, subroutines, modules, methods, or functions.
• Advantages:
1. Division of work.
3. Maintenance.
4. Reusability.
5
OOP
Object-oriented programming I
• Predecesors in the late 1950s and 1960s in the LISP and Simula communities.
• Partial support in several languages: structures in C (and structs in older versions of Matlab).
6
Object-oriented programming II
• Object receives messages, processes data, and sends messages to other objects.
7
Object-orientated programming: basic properties
• Encapsulation.
• Inheritance.
• Polymorphis.
• Overloading.
• Abstraction penalty.
8
Example in Matlab
• Class household.
9
Functional Programming
Functional programming
• Created by John McCarthy with LISP (list processing) in the late 1950s.
• Always admired in academia but with little practical use (except in Artificial Intelligence).
10
11
Theoretical foundation
• Lambda Calculus is Turing Complete: we can write a solution to any problem that a computer can
solve.
12
Why functional programming?
1. Easier to read.
3. Easier to parallelize.
• Useful features:
2. Lazy evaluation.
3. Closures.
13
Main idea
• All computations are implemented through functions: functions are first-class citizens.
1. Immutability: no variable gets changed (no side effects). In some sense, there are no variables.
2. Recursions.
3. Curried functions.
14
Interactions
1. Pure functional languages (like Haskell): only limited side changes allowed (for example, I/O) and
tightly enforced to prevent leakage.
2. Impure functional languages (like OCalm or F#): side changes are allowed at the discretion of the
programmer.
• We can implement many insights from functional programming even in standard languages such as
C++ or Matlab.
15
Functional languages
• Main languages:
1. Mathematica.
2. Common Lisp/Scheme/Clojure.
3. Standard ML/Calm/OCalm/F#.
4. Haskell.
5. Erlang/Elixir.
6. Scala.
16