Introduction to ML

Download Report

Transcript Introduction to ML

Introduction to ML – Part 1
Kenny Zhu
Assignment 2


http://www.cs.princeton.edu/courses/ar
chive/fall07/cos441/assignments/a2.ht
m
Due next Monday (Oct 1st)
Introduction to ML



This lecture: some basics on the SML
language and how to interact with the
SML/NJ run time system
Next lecture: implement more complex
system using SML
Resources:



Robert Harper’s “Programming in Standard ML”
Peter Lee’s “Notes on using SML/NJ systems”
SML/NJ Basis Library


http://www.standardml.org/Basis/index.html
See course webpage for pointers and info about how to
get the software
Standard ML



Standard ML is a general-purpose functional
programming language with type checking
and type inference
Emphasizes applications of functions, not
state changes and mutable data
Support for




Complex data structures
Memory management like Java
Large projects with many modules
Advanced type system for error detection
ML Highlights

Interactive Language




Type in expressions
Evaluate and print result
Compile into binary as well
Strongly-typed language




Every expression has a type
Functions as values
Certain errors cannot occur
Polymorphic types provide flexibility
ML Highlights

High-level programming features





Data types
Pattern matching
Exceptions
Mutable data discouraged
Modules and functors
Basic types and expressions






Int: 3+5, 2*6, 100 div 2, 2-5, ~3
Real: 3.1415, 100.0/3.0, ~1.5
Char: #”!”
String “abc”, “hello” ^ “ “ ^ “world”
Bool: true, false, (5>6), (3<>4), a
andalso b, c orelse d
Unit: ()
Data structures



Tuple: (“george”, 35)
Record: {name = “george”, age = 35}
List:






1::(2::(3::(4::nil))))
1::2::3::4::nil
1::2::3::4::[]
(1::2::nil)@(3::4::nil)
[1,2,3,4]
1::2::[3,4]
Declaration








val x = 5
let val a = 4
val b = 2
in a + b
end
fun succ x = x + 1
exception SomeException <of string>
type complex_num = (real * real)
val n = (3.0, 5.5)
datatype number = Int of int | Real of real
val n1 = Real ~8.0
Functions





fun f (x, y) = x * y
fun f x y = x * y
(curried form)
fn (x:int, y:int) :int => x * y
val f = fn x y => x * y
fun f pat_1 = exp_1
| f pat_2 = exp_2
| f pat_3 = exp_2
| f _ = exp_default
Other Useful Expressions





if x>0 then x-1 else x+1
case num of
Int x -> x div 2
| Real y -> y / 2.0
(expr_1; expr_2; …; expr_n)
raise SomeException “Fatal Error”
some_expr handle SomeExpection s =>
print s
When your program grows…
Interactive mode is a good way to start
learning and to debug programs, but…
 Type in a series of declarations into a
“.sml” file
- use “foo.sml”
[opening foo.sml]
list of declarations
…
with their types

Larger Projects


SML has its own built in interactive
“make”
Pros:



It automatically does the dependency
analysis for you
No crazy makefile syntax to learn
Cons:

May be more difficult to interact with other
languages or tools
Compilation Manager
sources.cm
Group is
a.sig
b.sml
c.sml
a.sig
b.sml
c.sml
% sml
- OS.FileSys.chDir “~/courses/510/a2”;
- CM.make();
looks for “sources.cm”, analyzes dependencies
[compiling…]
compiles files in group
[wrote…]
saves binaries in ./CM/
- CM.make’ “myproj/”();
specify directory
SML/NJ Demo