120 likes | 267 Views
Standard ML of New Jersey. Li-Wen Hsu <lwhsu@csie.nctu.edu.tw>. Standard ML of New Jersey. Compiler and programming environment for the Standard ML programming language. http://www.smlnj.org/ Download http://www.smlnj.org/dist/working/ 110.59 110. 60 Standard ML of NJ User's Guide
E N D
Standard ML of New Jersey Li-Wen Hsu <lwhsu@csie.nctu.edu.tw>
Standard ML of New Jersey • Compiler and programming environment for the Standard ML programming language. • http://www.smlnj.org/ • Download • http://www.smlnj.org/dist/working/ • 110.59 • 110.60 • Standard ML of NJ User's Guide • http://www.smlnj.org/doc/index.html
Connect to Workstation • PuTTY • http://www.chiark.greenend.org.uk/~sgtatham/putty • Workstations • bsd1.cs.nctu.edu.tw • bsd2.cs.nctu.edu.tw • linux1.cs.nctu.edu.tw
Standard ML of New Jersey Interactive System • $ sml • http://www.smlnj.org/doc/interact.html
factorial.sml fun factorial(0) = 1 | factorial(n:int) = n * factorial(n-1);
fib.sml fun fib(n) = if n < 2 then 1 else fib(n-1)+fib(n-2);
Base Types • int • Values: 3, ~2, 9999, ... • Operations: +,-,*,/, mod, div, =, <, ... • real • Values: 3.14, ~2.17, 0.1E6, ... • Operations: +,-, *,/, =, <, ... • char • Values: #"a", #"b", ... • Operations: ord, chr, =, <, ... • string • Values: "abc", "1234", ... • Operations: ^, size, =, <, ... • bool • Values: true, false • Operations: if exp then exp1 else exp2
hello.sml (* print hello world *) print "hello world!\n"
List • [1, 2, 3] • [] • nil • 0 :: [1, 2, 3] • fun length nil = 0 | length (_::t) = 1 + length t • [~3, ~2, ~1] @ [0, 1, 2, 3] • fun rev nil = nil | rev (h::t) = rev t @ [h]
Reference • Programming in Standard ML • http://www.cs.cmu.edu/People/rwh/introsml/ • Core Language, Programming Techniques • SML/NJ FAQ • http://www.smlnj.org/doc/FAQ/index.html • SML/NJ Literature • http://www.smlnj.org/doc/literature.html • The Standard ML Basis Library • http://www.smlnj.org/basis/
Quiz • square(n:int) • gcd(m:int, n:int)
Quiz • square(n:int) • gcd(m:int, n:int) fun factorial(0) = 1 | factorial(n:int) = n * factorial(n-1); fun fib(n) = if n < 2 then 1 else fib(n-1)+fib(n-2);