Introduction to ML

21
Introduction to ML

description

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 - PowerPoint PPT Presentation

Transcript of Introduction to ML

Page 1: Introduction to ML

Introduction to ML

Page 2: 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

Page 3: Introduction to ML

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

Page 4: Introduction to ML

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

Page 5: Introduction to ML

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"

Page 6: Introduction to ML

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

Page 7: Introduction to ML

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)

Page 8: Introduction to ML

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

Page 9: Introduction to ML

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

Page 10: Introduction to ML

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

Page 11: Introduction to ML

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

Page 12: Introduction to ML

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

Page 13: Introduction to ML

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

Page 14: Introduction to ML

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 [ ] !

Page 15: Introduction to ML

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

Page 16: Introduction to ML

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?

Page 17: Introduction to ML

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

Page 18: Introduction to ML

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

Page 19: Introduction to ML

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.

Page 20: Introduction to ML

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

Page 21: Introduction to ML

The End