1 / 22

Functional Design and Programming

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

walker
Download Presentation

Functional Design and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional Design and Programming Lecture 8: Imperative programming

  2. Literature • Paulson, chap. 8 • Reference types (8.1-8.3) • References in data structures (8.4-8.6) • Input/Output (8.7-8.9)

  3. 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) .

  4. 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.

  5. 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

  6. Store • Store: Mapping of (storage) locations to values; locations are values. • The store is updated by reference operations.

  7. Example val x = 5 val cond = false val iref = ref 25 val rref = ref (ref (ref 8.5))

  8. Environment and store Environment: Store: x: 5 8.5 cond: false 25 iref: rref:

  9. Environment and store... Environment: Store: x: l1 5 8.5 cond: false l2 25 iref: l3 l1 l2 l3 rref: l4 l4

  10. 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’

  11. 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)

  12. String Processing • toString functions: Return string representation of argument. • Examples: Int.toString 5 Bool.toString false Real.toString 5.25

  13. 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”

  14. String Splitting... • String.tokens: Splitting strings into tokens, resp. fields • Example: String.tokens Char.isSpace “ABC DE F G”

  15. Substrings • Substring: Structure for operations on substrings of strings • Examples: Substring.splitl Char.isAlphaNum (Substring.all “a1B. dfjk “) fun dateFromString...

  16. 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

  17. 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”)

  18. TextIO • See SML/NJ Basis library: Imperative I/O and Text I/O

  19. 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)

  20. HTML as Output Format • Idea: Leave layout and rendering to existing tools; generate input document format for those tools • Examples: • HTML • PostScript

  21. 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!

  22. 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!

More Related