λ
A Tour of Standard ML
↤
Table of Contents
0.1 - Setup
1.0 - Values
1.1 - Let expressions
1.2 - Basic data types
1.3 - Built-in data structures
1.4 - Functions
1.5 - Fun with Functions
1.6 - Modules
1.7 - Module Signatures
1.8 - Signatures, continued
1.9 - Functors
1.10 - Defining data types
1.11 - Recursive types
2.0 - Pattern matching
2.1 - Exhaustivity checking
2.2 - Value deconstruction
2.3 - Pattern matching in functions
2.4 - Conditional expressions
2.5 - Recursion
2.6 - Higher order functions
2.7 - Infinite loops
2.8 - Chaining expressions for side effects
2.9 - Mutable references
2.10 - While loops
3.0 - Concurrent ML Introduction
3.1 - Spawning a light-weight thread
3.2 - Channels
3.3 - Select
3.4 - Mailboxes
3.5 - IVars
3.6 - MVars
0.0 - Welcome
↦
3.3: Select
select
lets a thread wait on a list of communication events.
(* examples/select.sml *)
fun fib c q = let val x = ref 0 val y = ref 1 val nextFib = fn x' => let val tmp = !x in ( x := !y; y := tmp + (!y) ) end val break = ref false fun endRecvd () = !break = true in while not (endRecvd ()) do CML.select [ CML.wrap (CML.sendEvt (c, !x), nextFib ) , CML.wrap (CML.recvEvt q, fn _ => ( break := true; print "quit\n" )) ] end fun print_channel c q = let val i = ref 0 in ( while (!i) < 10 do ( print (Int.toString (CML.recv c)); print "\n"; i := (!i) + 1 ); CML.send (q, true) ) end fun main () = let val c : int CML.chan = CML.channel () val q : bool CML.chan = CML.channel () in ( CML.spawn (fn () => print_channel c q); fib c q ) end val _ = RunCML.doit(main, NONE)