Notes from 341 quiz section (9/30/97) ===================================== * admin - course web: http://www.cs.washington.edu/education/courses/cse341 - subscribe to mailing list - e-mail (put 341 somewhere in subject line, please!) * running ML (SML/NJ) - use xterms (e.g. orcas.cs.washington.edu) in Sieg 329 - add sml's path in .cshrc (or .mycshrc) set path = ($path "/cse/courses/misc_lang/axp/sml/bin") - type "sml" - exit with -d - loading files - use("myfile.sml"); (at SML prompt) - sml < "myfile.sml" (from command line) * basic types - integers (int) - reals (real) - booleans: true, false (bool) - strings (string) - characters (char) - new addition, NOT in ML book! - #"b" * declaring types - shown with `:' followed by type - "x : int" is a variable of type integer * arithmetic operators - usual for math: + - * - integer division: mod div - real division: / * lists - examples - [1, 2, 3] - [] = empty list, also written: nil - [["homer", "marge"], ["bart"]] - elements must be same type! - operators - append (@) - combines two lists - [1,2] @ [3,4] = [1,2,3,4] - functions - cons (::) - prepends an element onto a list - 1 :: [2,3,4] = [1,2,3,4] - head (hd) - return first element - hd([1,2,3,4]) = 1 (not [1]!) - tail (tl) - return everything but first element - tl([1,2,3,4]) = [2,3,4] - null - check for empty list - null([]) = true, null([5,6]) = false * strings and characters - concat (^) -- "ab" ^ "cd" = "abcd" - explode - turns a string into a list of characters - explode("hi") = [#"h", #"i"] - implode (inverse of explode) - ord - ascii value of a character -- ord(#"a") = 97 - chr (inverse of ord) * functions - expect lots of recursion - fun double(x) = 2 * x; - valid, but only works for integers - fun square(x) = x * x; - NOT valid -- can't infer whether x is integer or real - this would work: fun square(x:int) = x * x; * comments - "(*" at the beginning, "*)" at the end * ML quirks - unary negation written ~5, not -5 - boolean operators - "andalso" and "orelse", instead of "and" and "or" - beware: "and" *is* a keyword, but has another meaning - if-then-else - MUST have the "else" part - similar to C's construct: if a ? b : c; - parentheses in function calls are optional (e.g. "F x" means "F(x)") * reading - above notes taken from ch. 0-5 in blue ML book