Functional Design and Programming Lecture 8: Imperative programming.
-
date post
22-Dec-2015 -
Category
Documents
-
view
230 -
download
1
Transcript of Functional Design and Programming Lecture 8: Imperative programming.
Literature
Paulson, chap. 8 Reference types (8.1-8.3) References in data structures (8.4-8.6) Input/Output (8.7-8.9)
Exercises
Paulson, chap. 8: 8.1-8.4, 8.6-8.9 8.10-8.11, 8.13-8.15, 8.26-8.32 (8.16-8.19, 8.21-8.25)
.
References
References model storage locations References make imperative programming possible
in SML Referential transparency: Evaluation of an
expression can be repeated without change in result Referential transparency is broken by the use of
references.
Environment
Environment: Bindings of values to identifiers.
Bindings are established by: declarations (val, fun, exception; type, structure,
signature, functor) parameter passing (actual argument bound to
formal parameter) pattern matching
Store
Store: Mapping of (storage) locations to values; locations are values.
The store is updated by reference operations.
Environment and store...
5
false
8.5
25
Store:Environment:
x:
cond:
iref:
rref:
l1
l2
l3
l4
l2
l4 l3
l1
Reference operations
Create a new reference: ref v Look up value of a reference: !r Update a reference: r := v Check if two references are equal: r = r’
Imperative Arrays
Imperative arrays have constant-time element lookup and update operations.
Example: val a = Array.array (10000, 0)
Array.update (a, 1800, 25);Array.sub (a, 1800)
String Processing
toString functions: Return string representation of argument.
Examples:Int.toString 5Bool.toString falseReal.toString 5.25
String Processing...
fromString functions: Scan input string for longest prefix that is representation of a given type.
Examples:Int.fromString “85”Int.fromString “85Xdyf”Int.fromString “Xdvydf”Real.fromString “5.25”Real.fromString “5.25odxx”Real.fromString “Xdfkdfj”
String Splitting...
String.tokens: Splitting strings into tokens, resp. fields
Example:
String.tokens Char.isSpace “ABC DE F G”
Substrings
Substring: Structure for operations on substrings of strings
Examples:Substring.splitl Char.isAlphaNum (Substring.all “a1B. dfjk “)
fun dateFromString...
Readers
Reader type: type (‘a, ‘b) reader = a -> (‘b * ‘a) option
‘a: source type ‘b: result type (`a, char) reader: character source
Example: List.getItem: ‘a list -> (‘a * ‘a list) option
Scanning functions
scan: Takes a character source and returns value denoted by longest prefix of a given type, together with remaining characters
Example:Real.scan List.getItem (explode “5.25”)
HTML
HTML: HyperText Markup Language Used to specify formatted documents Prevalent in WWW documents Standard scenarios:
‘static’ HTML page on WWW-server, requested by WWW-client (browser)
function (‘servlet’) on WWW-server, invoked by browser, produces HTML-page
rendered by browser (in both cases)
HTML as Output Format
Idea: Leave layout and rendering to existing tools; generate input document format for those tools
Examples: HTML PostScript
HTML: Example
<P><EM>Westmoreland</EM>. Of fighting men they have full three score thousand.<P><EM>Exeter</EM>. There’s five to one;besides, they all are fresh.<P><EM>Westmoreland.</EM> O that we nowhad here <BR>But one ten thousand of those men in England <BR>That do no work to-day!