1 / 15

Introduction to ML – Part 1

Introduction to ML – Part 1. Kenny Zhu. Assignment 2. http://www.cs.princeton.edu/courses/archive/fall07/cos441/assignments/a2.htm 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

clem
Download Presentation

Introduction to ML – Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to ML – Part 1 Kenny Zhu

  2. Assignment 2 • http://www.cs.princeton.edu/courses/archive/fall07/cos441/assignments/a2.htm • Due next Monday (Oct 1st)

  3. 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

  4. 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

  5. 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

  6. ML Highlights • High-level programming features • Data types • Pattern matching • Exceptions • Mutable data discouraged • Modules and functors

  7. 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: ()

  8. 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]

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Compilation Manager sources.cm a.sig b.sml c.sml Group is 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

  15. SML/NJ Demo

More Related