1(* This is a comment *) 2 3abstype abstype_name 4 5datatype suit = Spades | Hearts | Diamonds | Clubs 6 7exception Change 8 9fun dist (x:real, y:real):real = sqrt (x*x + y*y) 10 11fun checked_factorial n = 12 if n < 0 then 13 raise Factorial 14 else if n=0 then 15 1 16 else 17 n * checked_factorial (n-1) 18 19exception Factorial 20 21local 22 fun fact 0 = 1 23 | fact n = n * fact (n-1) 24in 25 fun checked_factorial n = 26 if n >= 0 then 27 fact n 28 else 29 raise Factorial 30end 31 32functor Matcher (structure RegExp : REGEXP) :> MATCHER = struct 33 34 structure RegExp = RegExp 35 36 open RegExp 37 38 fun match_is Zero cs k = false 39 | match_is One cs k = k cs 40 | match_is (Char c) nil _ = false 41 | match_is (Char c) (c'::cs) k = (c=c') andalso (k cs) 42 | match_is (Plus (r1, r2)) cs k = 43 (match_is r1 cs k) orelse (match_is r2 cs k) 44 | match_is (Times (r1, r2)) cs k = 45 match_is r1 cs (fn cs' => match_is r2 cs' k) 46 | match_is (r as Star r1) cs k = 47 (k cs) orelse match_is r1 cs (fn cs' => match_is r cs' k) 48 49 fun match regexp string = 50 match_is regexp (String.explode string) 51 (fn nil => true | _ => false) 52 53end 54 55signature SUSP = sig 56 type 'a susp 57 val force : 'a susp -> 'a 58 val delay : (unit -> 'a) -> 'a susp 59end 60 61structure Susp :> SUSP = struct 62 type 'a susp = unit -> 'a 63 fun force t = t () 64 fun delay (t : 'a susp) = 65 let 66 exception Impossible 67 val memo : 'a susp ref = ref (fn () => raise Impossible) 68 fun t' () = 69 let val r = t () in memo := (fn () => r); r end 70 in 71 memo := t'; 72 fn () => (!memo)() 73 end 74 fun loopback f = 75 let 76 exception Circular 77 val r = ref (fn () => raise Circular) 78 fun t () = force (!r) 79 in 80 r := f t ; t 81 end 82end 83 84type hyperlink = { protocol : string, address : string, display : string } 85 86val dist : real * real -> real = fn (x:real, y:real) => sqrt (x*x + y*y) 87