Lecture 3 --- Administrative 1. check the class bulletin bboard regularly for announcements, and Q&As about assignments. - 110.07 version of SML is fine - logon to mlb.acad.ece.udel.edu to use sml - email the intructor to get your class id 2. any feedback so for? --- ML (continued) > Define your own datatype e.g., - datatype IntOrBool = Int of int | Bool of bool - [Int 5, Bool true] val it = ... : IntOrBool list e.g., - datatype RegExp = Empty | Char of char | Alt of RegExp * RegExp | Seq of RegExp * RegExp | Repeat of RegExp Naming conventions: Capitalize value constructors to distinguish them from value variables. Write a function "sample" to take a regular expression exp and return a string matching that regular exp - fun sample Empty = "" | sample (Char c) = str c | sample (Alt (r1,r2)) = sample r1 | sample (Seq (r1,r2)) = sample r1 ^ sample r2 | sample (Repeat r) = ""; or using wildcard | sample (Repeat _) = ""; Note: pattern matching is used extensively on datatype An application of function "sample": - sample (Seq(Char #"a", Char #"b")) val it = "ab" Datatypes are recursive by default. e.g., datatype 'a tree = Lf | Br of 'a * 'a tree * 'a tree; > Exception - fun f m n = 1 + m div n handle Div => 0 | Overflow => 1 > How to define your own Exception? An exception name in SML is a constructor of the built-in type exn. e.g., exception Foo Contructors can also be functions: e.g., exception Badvalue of int; Whenever possible, declare exceptions at top level. > Raising exceptions - fun f m n = (1 + m div n) handle Div => raise Foo 1 + raise Foo any raise Exception is treated as 'a when ML does type checking. Infinite loop e.g., - fun f x = f x 'a -> 'b > Standard exceptions Overflow Div Size Fail > Handling exceptions E handle P1 => E1 | ... | Pn => En - fun f m n = 1 + m div n handle Div => 0 | Overflow => 1