Download ML Programming: Lecture 3b - While Loops, References, and Regular Expressions and more Slides Computer Science in PDF only on Docsity!
Lecture #3b, Jan. 22, 2007
- References
- While Loops
- Accumulating parameter functions
- Regular Expressions as programs
- RE to NFA
- Patterns for RE’s
Peeking inside a Library
- To see what is inside a Structure you can open it.
- This is somewhat of a hack, but it is useful.
Standard ML of New Jersey v110.57 [built: Mon Nov 21 21:46:28 2005]
- open Int;
opening Int
type int = ?.int
val precision : Int31.int option
val minInt : int option
val maxInt : int option
val toLarge : int -> IntInf.int
val fromLarge : IntInf.int -> int
val toInt : int -> Int31.int
val fromInt : Int31.int -> int
val div : int * int -> int
val mod : int * int -> int
val quot : int * int -> int
val rem : int * int -> int
val min : int * int -> int
val max : int * int -> int
References
- References allow one to write programs with mutable variables.
- The interface to assignment and update is slightly different from other languages - val r = (ref 5) Create a new reference that can be updated - !r Get the value stored in the reference - (ref n) => … Pattern match to get value - fun! (ref n) = n - r := 6 + z Update a reference with a new value
- Later today we will use the following: val next = ref 0; fun new () = let val ref n = next in (next := n+1; n) end; Alternatively fun new () = let val n = !next in (next := n+1; n) end;
While Loops
- While loops are similar to other languages, they usually require use of references (to get the condition to eventually change).
- Statements are inside ()’s and separated by “;” val n = ref 4; val w1 = while (!n > 0) do (print (Int.toString (!n) ^ "\n"); n := (!n) – 1 );
Semicolon to
separate
statements
Semicolon to
end the
“val w1 = …”
declaration
Accumulating parameters
- Many loops look like this { ans = init; While test do stuff ; Return ans}
- There is a pattern that mimics this in ML called functions with accumulating parameter.
- The pattern consists of a recursive function with two (or more) parameters. The first parameter drives the loop (usually by pattern matching), the second accumulates an answer (like ans in example above).
- We call the function with init as the value of the second argument to get started.
- We return the second argument when the function is done recurring.
Fact as an accumulating function
fun fact4 n = let fun loop 0 ans = ans | loop n ans = loop (n-1) (n*ans) in loop n 1 end; { ans = init; While test do stuff ; Return ans}
Regular Expressions
• Regular Languages and Regular expressions are used to
describe the patterns which describe lexemes.
• Regular expressions are composed of empty-string,
concatenation, union, and closure.
• Examples:
A(A | D)* where A is alphabetic and
D is a digit
(+ | - | ε ) D D*
closure
union
Empty-string
Concatenation is implicit
Meaning of Regular Expressions Let A,B be sets of strings: The empty string: "" ε= { "" } (sometimes ) Concatenation by juxtaposition: AB = a^b where a in A and b in B A = {"x", "qw"} and B = {"v", "A"} then AB = { "xv", "xA", "qwv", "qwA"}
Regular Expressions as a language
- We can treat regular expressions as a programming language.
- Each expression is a new program.
- Programs can be compiled.
- How do we represent the regular expression language? By using a datatype. datatype RE = Empty | Union of RE * RE | Concat of RE * RE | Star of RE | C of char;
Example RE program
(+ | - | ε ) D D*
val re1 =
Concat(Union(C #”+”,Union(C #”-
”,Empty))
,Concat(C #”D”,Star (C
#”D”)))
Rules
• “x”
• AB
• A|B
• A*
ε x A^ B A B ε ε ε ε A ε ε ε ε
Example: (a|b)*abb
ε ε ε ε 8 a b ε a b b 1
• Note the many ε transitions
• Loops caused by the *
• Non-Determinism, many paths out of a state on “a”
(^2 ) 6 4 5 7 (^10 ) 0 ε ε ε
fun nfa Empty = let val s = new() val f = new() in (s,f,[Edge(s,Epsilon,f)]):Nfa end | nfa (C x) = let val s = new() val f = new() in (s,f,[Edge(s,Char x,f)]) end | nfa (Union(x,y)) = let val (sx,fx,xes) = nfa x val (sy,fy,yes) = nfa y val s = new() val f = new() val newes = [Edge(s,Epsilon,sx) ,Edge(s,Epsilon,sy) ,Edge(fx,Epsilon,f) ,Edge(fy,Epsilon,f)] in (s,f,newes @ xes @ yes) end ε x A B ε ε ε ε
| nfa (Concat(x,y)) = let val (sx,fx,xes) = nfa x val (sy,fy,yes) = nfa y in (sx,fy,(Edge(fx,Epsilon,sy)):: (xes @ yes)) end | nfa (Star r) = let val (sr,fr,res) = nfa r val s = new() val f = new() val newes = [Edge(s,Epsilon,sr) ,Edge(fr,Epsilon,f) ,Edge(s,Epsilon,f) ,Edge(f,Epsilon,s)] in (s,f,newes @ res) end A ε ε ε ε A^ B
Docsity.com