Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.

Similar presentations


Presentation on theme: "Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to."— Presentation transcript:

1 Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to ML” See course webpage for pointers and info about how to get the software

2 Intro to ML Highlights Functional Language Functions are pervasive: First-class values Storable, arguments, results, nested Strongly-typed language Every expression has a type Certain errors cannot occur Polymorphic types provide flexibility Flexible Module System Abstract Types Higher-order modules (functors)

3 Interactive Language Type in expressions Evaluate and print type and result Statically Scoped Language Compile-time resolution of values Call-by-value language f(e) High-level programming features Data types Pattern matching Exceptions Mutable data discouraged

4 Preliminaries Read – Eval – Print – Loop - 3 + 2; > 5: int - it + 7; > 12 : int - it – 3; > 9 : int - 4 + true; Type clash in : 3 + true Looking for a : intType I have found a : boolError - 3 div 0 Failure : Div- run-time error

5 Basic Values - (); > () : unit=> like “void” in c (sort of) => the uninteresting value/type - true; > : bool - False; > : bool - if it then 3+2 else 7;always necessary > 7 : int - false and also loop_Forever; > False : booland also, or else short-circuit eval

6 Basic Values Integers -3 + 2j > 5:int - 3 + (if not true then 5 else 7); > 10 : intNo division between expressions and statements Strings - “Dave” ^ “ “ ^ “Walker”; > “Dave Walker”:string - size “foo”; > 3 : int Reals - 3.14; > 3.14 : real

7 Structured Values Tuples - (true, 17, “Stuff”); > (true, 17, “Stuff”) : bool * int * string - (if 3 < 2 then “x” else “y”, false); >(“y”, false) : string * bool Records -{name = “Dave”, ssn = 332177} >_____________ : {name = string, ssn : int} -#name it ; >“Dave” : string -#1 (true, False); -true : bool

8 Parametric Polymorphism Functions like compose work on objects of many different types In ML, we write compose: (‘a -> ‘b) -> (‘c -> ‘a) -> type variables are written with ‘ (‘c -> ‘b) compose not : compose not not : compose (op + 1) not : compose (fn x => x) :

9 What is the type of compose? Fun compose = Fn f => (fn g => Fn x => f (g x)) T3 Compose : T2 -> (T2 -> (T5 -> T6) : T2 -> (( T7 -> T8) -> (T5 -> T6)) : (T9 -> TA) -> (T7 -> T9) -> (T7 -> TA) : (‘a -> ‘b) -> (‘c -> ‘a) -> (‘a -> ‘c) Compose (fn x => x + 1) (fn y => y * 3)7;

10 Declarations -val x = 4 * 5;Declare new name x and bind it to value >val x = 20 : int -val y = x + 1; >val y = 21 : int -val x = x + y;reuse name >val x = 41 : int -x; >41 : int static scoping -y; local declaration >21 : intscope -(let val x = 10 in (x + x, y + x) and, x); >((20, 31), 41) : (int * int) * int

11 Functions (the good stuff) - not;functions are values >Not = fn : bool -> bool we can’t write out a function so ML just says this -not true;function application is a space >False : bool -fun twice x = 2 * x >Val twice = fn : int -> int -fun fact x = if x = 0 then 1 else (fact (x-1)) * x -fact (twice (twice 3));

12 functions - fn s => x ^ “!”;an anonymous function like writing >Fn : string -> string-3 -val exclaim = rather than - val x = 3; fn : => s ^ “!” >… -val compose = fn f => fn g => fn x => f(g x); >… - ((compose twice) twice) 3; >12 -four = compose twice twice >… -four 7; >…


Download ppt "Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to."

Similar presentations


Ads by Google