(****************************************************************************** * SNU 4190.310 Programming Languages (Fall 2006) * * HW 1: nML exercise I ******************************************************************************) (****************************************************************************** * Exercise 1. * nML exercise I: merge ******************************************************************************) fun merge ([] : int list, r) = r | merge (l, []) = l | merge (l as (lh :: lt), r as (rh :: rt)) = if lh < rh then lh :: (merge (lt, r)) else rh :: (merge (l, rt)) (****************************************************************************** * Exercise 2. * nML exercise I: sigma ******************************************************************************) fun sigma (a, b, f) = if b < a then 0 else f a + sigma (a + 1, b, f) (****************************************************************************** * Exercise 3. * nML exercise I: vocalize ******************************************************************************) exception Error of string val vocalize : string -> string list list = let val invalid : string -> bool = fn s => not String.length(s)=7 val split : string -> int -> string list = fn s i => [(String.sub s 0 i),(String.sub s i ((String.length s)-i))] val vocalize_digit : string -> int -> string list = let fun vocalize_table_1 s = (case s of "1" => "ÀÏ" | "2" => "ÀÌ" | "3" => "»ï" | "4" => "»ç" | "5" => "¿À" | "6" => "À°" | "7" => "Ä¥" | "8" => "ÆÈ" | "9" => "±¸" | _ => raise Error "vocalize_digit : fist argument is not integer string") fun vocalize_table_2 i = (case i of 1 => "½Ê" | 2 => "¹é" | 3 => "õ" | 4 => "¸¸" | 5 => "½Ê¸¸" | 6 => "¹é¸¸" | 7 => "õ¸¸" | 8 => "¾ï" | _ => raise Error "vocalize_digit : second argument out of range(1-8)") in fn "0" _ => [] | s 0 => [(vocalize_table_1 s)] | s i => [(vocalize_table_1 s),(vocalize_table_2 i)] end val vocalize_string : string -> string list = let val rec vocalize_stringb : string -> string list = fn s => if String.length(s) = 0 then [] else (List.append (vocalize_digit (String.sub s 0 1) (String.length(s) - 1)) (vocalize_stringb (String.sub s 1 (String.length(s)-1)))) in fn s => if (vocalize_stringb s) = [] then ["¿µ"] else (vocalize_stringb s) end in fn s => if invalid(s) then raise Error "invalid input : length of first argument string must be 7" else (List.map vocalize_string (split s 3)) end (****************************************************************************** * Exercise 4. * nML exercise I: crazy2val ******************************************************************************) type crazy2 = NIL | ZERO of crazy2 | ONE of crazy2 | MONE of crazy2 val rec crazy2val : crazy2 -> int =fn NIL => 0 | ONE(c) => 1 + 2 * (crazy2val c) | MONE(c) => -1 + 2 * (crazy2val c) | ZERO(c) => 2 * (crazy2val c) (****************************************************************************** * Exercise 5. * nML exercise I: crazy2add ******************************************************************************) fun merge ([] : int list, r) = r type crazy2 = NIL | ZERO of crazy2 | ONE of crazy2 | MONE of crazy2 val rec crazy2val : crazy2 -> int =fn NIL => 0 | ONE(c) => 1 + 2 * (crazy2val c) | MONE(c) => -1 + 2 * (crazy2val c) | ZERO(c) => 2 * (crazy2val c) val rec crazy2add : crazy2 * crazy2 -> crazy2 =fn (NIL, c) => c | (c, NIL) => c | (ONE(c1), ONE(c2)) => ZERO(crazy2add(crazy2add(c1, c2),ONE(NIL))) | (ONE(c1), ZERO(c2)) => ONE(crazy2add (c1, c2)) | (ONE(c1), MONE(c2)) => ZERO(crazy2add(c1,c2)) | (ZERO(c1), ZERO(c2)) => ZERO(crazy2add(c1,c2)) | (ZERO(c1), MONE(c2)) => MONE(crazy2add(c1,c2)) | (MONE(c1), MONE(c2)) => ZERO(crazy2add(crazy2add(c1,c2),MONE(NIL))) | (c1,c2) => crazy2add(c2,c1)