220 likes | 326 Views
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
E N D
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.
Example val x = 5 val cond = false val iref = ref 25 val rref = ref (ref (ref 8.5))
Environment and store Environment: Store: x: 5 8.5 cond: false 25 iref: rref:
Environment and store... Environment: Store: x: l1 5 8.5 cond: false l2 25 iref: l3 l1 l2 l3 rref: l4 l4
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 5 Bool.toString false Real.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”)
TextIO • See SML/NJ Basis library: Imperative I/O and Text I/O
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 now had here <BR>But one ten thousand of those men in England <BR>That do no work to-day!
Translating to HTML • Inddata: Westmoreland. Of fighting men they have full three score thousand. Exeter. There’s five to one; besides, they all are fresh. Westmoreland. O that we now had here But one ten thousand of those men in England That do no work to-day!