1 / 21

Introduction to ML

Introduction to ML. General comments. ML is a purely functional language--there are (almost) no side effects There are two basic dialects of ML: Standard ML, and CAML ML is interactive ML is case-sensitive ML is strongly typed. Using Harlequin ML. Use MLWorks 2.0 + Basis

vivi
Download Presentation

Introduction to ML

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

  2. General comments • ML is a purely functional language--there are (almost) no side effects • There are two basic dialects of ML: Standard ML, and CAML • ML is interactive • ML is case-sensitive • ML is strongly typed

  3. Using Harlequin ML • Use MLWorks 2.0 + Basis • When you run MLWorks you get a Listener window • Expressions can be entered at the prompt • To load a file: use "A:\\myfile.sml"; • Notice the double quotes, \\, .sml, ; • The MLWorks User Guide is quite good

  4. Comments • Comments are enclosed in (* and *) • Comments can be nested • Nested comments are useful for commenting out a block of code that may itself contain comments

  5. Primitive types • int 0, 5, 42, ~17, 0x00FF • Notice that ~ is the unary minus • real 0.0, ~5.3, 1.7e14, 1e~10 • Cannot start or end with a decimal point • bool true, false • string "", "One\nTwo\nThree" • char #"a", #"\n"

  6. Kinds of identifiers • Alphanumeric identifiers, e.g. foo_bar • Composed of letters, digits, underscores, primes • "prime" = "apostrophe" = "single quote" = "tick" • Only "type variables" can start with a prime • An underscore all by itself, _, is a wildcard • Symbolic identifiers, e.g. >>> • Composed of punctuation, except:. , " ( ) [ ] { } • Best used for infix operators

  7. Trying things at the prompt • MLWorks> 2+2; • val it : int = 4 • MLWorks> val five = 5; • val five : int = 5 • MLWorks> 2 + five; • val it : int = 7 • The identifier it holds the last top-level value • ML shows you the type (because it's important)

  8. No mixed-mode arithmetic • 5 + 3.0; • Function applied to argument of wrong typeNear: 5 + 3.0Required argument type: (int * int)Actual argument type: (int * real) Type clash between real and int

  9. Numeric coercions • MLWorks> real 5; • val it : real = 5.0 • MLWorks> round 7.5; • val it : int = 8 • MLWorks> trunc 7.5; • val it : int = 7

  10. Operations on bools • Comparisons give a bool result • < <= = <> >= > • Standard operators are not, andalso, orelse • MLWorks> (1 < 3) andalso (3 <= 5); • val it : bool = true • andalso and orelse are short-circuit operators • There is an and, but it's something else entirely

  11. Operations on numbers • + - * are as usual • but remember, no mixed mode! • / is for reals, div and mod are for ints • ~ is the unary minus • Parentheses and precedence are as usual

  12. Operations on strings and chars • ^ is string concatenation • size returns the size of a string • Comparisions < <= = <> >= > can be applied to strings • The same comparisons can be applied to chars

  13. Lists • Lists are comma-separated and enclosed in brackets • MLWorks> [3,5,7]; • val it : int list = [3, 5, 7] • All elements of a list must be the same type • Note that the type above is given as int list

  14. The empty list • The empty list is represented by [ ] • The empty list has a type • When ML doesn't know the type, it represents it with a type variable'a, 'b, 'c, ... • MLWorks> []; • val it : 'a list = [] • But sometimes ML does know the type of [ ] !

  15. Operations on lists • hd returns the head (CAR) of a list • tl returns the tail (CDR) of a list • :: adds an element to a list (CONS) • Example: 1 :: [3, 5, 7] gives [1, 3, 5, 7] • @ appends two lists (APPEND) • implode converts a list of chars to a string • explode converts a string to a list of chars

  16. Tuples • Tuples are comma-separated and enclosed in parentheses • MLWorks> ("pi", 3.1416, [1,2,3]); • val it : (string * real * int list) = ("pi", 3.1416, [1, 2, 3]) • Notice the type: (string * real * int list) • [(3, 5.0), (3.0, 5)] isn't legal--why not?

  17. Tuples of different sizes • Notice the types in the following: • MLWorks> (1,2,3); • val it : (int * int * int) = (1, 2, 3) • MLWorks> (1,2); • val it : (int * int) = (1, 2) • MLWorks> (1); (* What type is this? *) • val it : int = 1 (* Why is this an int? *) • For x of any type, (x) is the same as x

  18. Operations on tuples • #1 is the first element of a tuple,#2 is the second element of a tuple,#3 is the third element of a tuple,and so on. • This only works for constants; #x is not legal • That's about it for operations on tuples

  19. The unit • The empty tuple is represented as ( ) • The empty tuple is called the "unit" • There is (obviously) only one value of this type, but it is a value • There are a (very) few places where we use the unit because we need to provide a value but don't care what it is.

  20. Defining a simple function • MLWorks> fun add (x, y) = x + y; • val add : (int * int) -> int = fn • MLWorks> add(3,5); • val it : int = 8 • Notice the type of add: (int * int) -> int = fn • The -> indicates that this is a function • The value (function body) is abbreviated to fn

  21. The End

More Related