Download presentation
Presentation is loading. Please wait.
1
Functional Design and Programming Lecture 1: Functional modeling, design and programming
2
Literature Paulson, chap. 1 (excl. 1.7, 1.8) Paulson, chap. 2: Value declarations (2.1-2.3) Numbers, characters, strings (2.4-2.6) Pairs, tuples, records (2.7-2.10) The evaluation of expressions (2.11-2.16)
3
Programming paradigms Imperative programming Procedural Object-oriented Functional programming (Logic programming)
4
Imperative Programming Basic machine model: Machine has a state (memory, state of devices) State is acted upon by commands (e.g. “move a byte to some location in memory’’) Programs describe sequences of state changes by providing simple commands (“assign some value to a variable”) and constructs for composing those.
5
Procedural programming Central notion: procedure. Purpose: Code factoring, reuse. Procedure is a named piece of code (sequence of commands), parameterized by formal arguments. Procedures can be called (invoked) by referring to their name and providing actual arguments (values) that are assigned to the corresponding formal arguments.
6
Object-oriented programming Central concept: object. Purpose: Code factoring, abstraction (safety), reuse. Encapsulation of data and the operations (procedures) that operate on those data.
7
Functional programming Basic model: Machine consists of input ports and output ports. A program describes a function which computes outputs for given inputs. Central notions: value (incl. function), referential transparency, compositionality.
8
Referential transparency Expression: describes a computation and its result (“value”) Referential transparency: any computation can be replaced by its value in any context, without affecting the observable results.
9
Example function gcd(m,n: integer): integer; var prevm: integer; begin while m <> 0 do begin prevm := m; m := n mod m; n := prevm end; gcd := n end; fun gcd (m, n) = if m = 0 then n else gcd (n mod m, m);
10
Functional programming: Characteristics Powerful ‘bulk’ data types, e.g. lists, trees. Higher-order functions: functions as values (can be passed as parameters to and returned from other functions, stored in data structures) Recursion: Recursively defined functions and types. Pattern matching: convenient access to components of data structures
11
Functional programming: Characteristics... Polymorphism: Flexible and safe reuse of functions with many types of arguments Safe composition: Support for modular composition. High-level programming: automatic memory management no low-level data types
12
Functional Programming: Principles Orthogonality: No ad-hoc restrictions on how language constructs can be combined. Rigor: Languages are well-defined; support for rigorous reasoning about behavior of programs Safety: Static and dynamic checking of safety of operations; careful design of basic types and their operations.
13
Names, functions and types Values: Values have no identity, no birth date, no expiration date, and they can’t change (ever hear of a 5 turning into a 6?); they are unchanging and immortal. E.g., the string “Susanne”, the number denoted by the Arabic numeral 5, the list of natural numbers from 0 to 5: [0,1,2,3,4,5], etc.
14
Names, functions and types... Names: Names refer to things (values, people, etc.) Names can be bound to values in constructs called declarations. In this fashion we can refer to values by their names. (Q: How many names can a value have?) Some values have predefined names (constants) that cannot be bound to other values; e.g. 60 is the standard name (“numeral”) for a particular natural number. NB: Names are not values. Susanne as a name is not the same as the string “Susanne”, which is a value.
15
Identifiers in SML Identifiers: Those names that can be bound to values in declarations Alphabetic names ( length ) Symbolic names ( @) Long identifiers ( String.sub) Some identifiers are predefined; that is, they are bound to a value at program startup, but can be bound in a declaration to a new value.
16
Strings and characters String constants: ”Susanne” Character constants: #”a”
17
Truth values (“Booleans”) and conditional expressions Boolean constants: true, false Conditional expressions: if... then... else... E.g.: fun sign(n) = if n>0 then 1 else if n=0 then 0 else (* n<0 *) ~1
18
Pairs, tuples, records Pairs (2-tuples): e.g., (5, 8), (“Susanne”, 30) Tuples (n-tuples): e.g., (5, 8, 9), (“Susanne”, 30, #”a”) Records (named tuples): e.g., { firstName = “Susanne”, age = 30, empCategory = #”a” }
19
Pairs, tuples, records... Record selection: val tup1 = (5, 8); val tup2 = (“Susanne”, 30, #”a”); val empRec = {...}; #1 tup1; #3 tup2; #firstName empRec;
20
Record types Tuple types: int * int, string * int * char Record types: {firstName: string, age: int, empCategory: char}
21
Type declarations Type declaration: Binding of type (expression) to identifier. E.g., type empRecType = {firstName: string, age: int, empCategory: char}
22
Expression evaluation General idea: Compute the values of the subexpressions (in SML typically from left- to-right). Then perform the resulting operation on the values.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.