in tro duction to f unctional programming lecture 2 1 in
play

In tro duction to F unctional Programming: Lecture 2 1 In - PDF document

In tro duction to F unctional Programming: Lecture 2 1 In tro duction to F unctional Programming John Harrison Univ ersit y of Cam bridge Lecture 2 Basics of ML T opics co v ered: V ersions of ML


  1. In tro duction to F unctional Programming: Lecture 2 1 In tro duction to F unctional Programming John Harrison Univ ersit y of Cam bridge Lecture 2 Basics of ML T opics co v ered: � V ersions of ML � Running ML on Thor � In teracting with ML � Higher order functions and currying � Ev aluation order John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  2. In tro duction to F unctional Programming: Lecture 2 2 The ML family ML is not a single language. Apart from Standard ML, whic h w e use here, there are man y descendan ts of the original ML used as the metalanguage of Edin burgh LCF, e.g. � CAML Ligh t | an excellen t ligh t w eigh t system dev elop ed at INRIA. � Ob jectiv e CAML | a new v ersion of CAML Ligh t including ob ject-orien ted features. � Lazy ML | a v ersion from Gothen burg using lazy evaluation . � Standard ML | an agreed `standard v ersion' Standard ML has t w o parts: the Cor e language and the Mo dules . W e will not co v er the mo dule system at all. John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  3. In tro duction to F unctional Programming: Lecture 2 3 Implemen tations of Standard ML There are sev eral implemen tations of (something similar to) Standard ML: � Standard ML of New Jersey | free but v ery resource-h ungry . � Mosco w ML | free but do esn't ha v e the Mo dules. � P oly ML | go o d, but a commercial pro duct, and do esn't run under Lin ux � Harlequin ML | a new er commercial system with in tegrated dev elopmen t en vironmen t. W e'll use Mosco w ML. This is a go o d ligh t w eigh t implemen tation based on CAML Ligh t, written b y Sergei Romanenk o (Mosco w) and P eter Sestoft (Cop enhagen). The features w e use are general, and can easily b e translated to other dialects. John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  4. In tro duction to F unctional Programming: Lecture 2 4 Starting up ML Mosco w ML is already installed on Thor. In order to start it up, t yp e: /group/clteach/jr h/m os ml/ `a rch `/ bin /m osm l This will in v ok e the appropriate v ersion, dep ending on the mac hine arc hitecture. The system should start up and presen t its prompt. $ /group/clteach/jr h/m os ml/ `a rch `/ bin /m osm l Moscow ML version 1.42 (July 1997) Enter `quit();' to quit. - If y ou w an t to install Mosco w ML on y our o wn computer, see the W eb page: http://www.dina. kv l.d k/ ~se st oft /m osm l. htm l John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  5. In tro duction to F unctional Programming: Lecture 2 5 In teracting with ML W e will run ML as an in terpreter, rather lik e a sophisticated calculator. Y ou just t yp e an expression in to it, terminated b y a semicolon, and it prin ts the result of ev aluating it, e.g. - 10 + 5; > val it = 15 : int ML not only prin ts the result, but also infers the typ e of the expression, namely int . If ML cannot assign a t yp e to an expression then it is rejected: - 1 + true; ! .... Type clash: .... ML kno ws the t yp e of the built-in op erator + , and that is ho w it mak es its t yp e inference. W e'll treat the t yp e system more systematically next time; for the momen t, it should b e in tuitiv ely clear. John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  6. In tro duction to F unctional Programming: Lecture 2 6 Loading from �les W e ha v e just b een t yping things in to ML and thinking ab out the results. Ho w ev er one do esn't write real programs in this w a y . T ypically , one writes the expressions and declarations in a �le. T o try them out as y ou go, these can b e inserted in the ML windo w using cut and paste. Y ou can cut and paste using X-windo ws and similar systems, or an editor lik e Emacs with m ultiple bu�ers. F or larger programs, it's con v enien t simply to load them from �le in to the ML session. This can b e done using the use command in ML, e.g: use "myprog.ml"; Programs can also include commen ts written b et w een (* and *) . These are ignored b y ML, but are useful for p eople reading the co de. John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  7. In tro duction to F unctional Programming: Lecture 2 7 Using functions Since ML is a functional language, expressions are allo w ed to ha v e function t yp e. The ML syn tax for a function taking x to t[x] is fn x => t[x] . F or example w e can de�ne the successor function: - fn x => x + 1; > val it = fn : int -> int Again, the t yp e of the expression, this time int -> int , is inferred and displa y ed. Ho w ev er the function itself is not prin ted; the system merely writes fn . F unctions are applied b y juxtap osition, with or without brac k eting: - (fn x => x + 1) 4; > val it = 5 : int - (fn x => x + 1)(3); > val it = 4 : int John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  8. In tro duction to F unctional Programming: Lecture 2 8 Curried functions (1) Ev ery function in ML tak es a single argumen t. One w a y to get the e�ect of m ultiple argumen ts is to use a p air for the argumen t | w e'll discuss this next time. Another w a y , often more p o w erful, is to use Currying , making the function tak e its argumen ts one at a time, e.g. - fn x => (fn y => x + y); > val it = fn : int -> (int -> int) This function tak es the �rst argumen t and giv es a new function. F or example: - (fn x => (fn y => x + y)) 1; > val it = fn : int -> int is just the successor function again, e.g: - ((fn x => (fn y => x + y)) 1) 6; > val it = 7 : int John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  9. In tro duction to F unctional Programming: Lecture 2 9 Curried functions (2) Because curried functions are so common in functional programming, ML �xes the rules of asso ciation to a v oid the need for man y paren theses. When one writes E E E , ML asso ciates it as 1 2 3 ( E E ) E . F or example, all the follo wing are 1 2 3 equiv alen t: - ((fn x => (fn y => x + y))(1))(6); > val it = 7 : int - ((fn x => (fn y => x + y)) 1) 6; > val it = 7 : int - (fn x => (fn y => x + y)) 1 6; > val it = 7 : int - (fn x => fn y => x + y) 1 6; > val it = 7 : int John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  10. In tro duction to F unctional Programming: Lecture 2 10 Bindings (1) It is not necessary to ev aluate an expresion all in one piece. Y ou can bind an expression to a name using val : - val successor = fn x => x + 1; > val successor = fn : int -> int - successor(successo r( suc ce sso r 0)); > val it = 3 : int Note that this isn't an assignmen t statemen t, merely an abbreviation. But bindings can b e recursiv e: just add rec : - val rec fact = fn n => if n = 0 then 1 else n * fact(n - 1); > val fact = fn : int -> int - fact 3; > val it = 6 : int John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  11. In tro duction to F unctional Programming: Lecture 2 11 Bindings (2) When binding functions, one can also use fun. The follo wing are equiv alen t: - val successor = fn x => x + 1; - fun successor x = x + 1; and - val rec fact = fn n => if n = 0 then 1 else n * fact(n - 1); - fun fact n = if n = 0 then 1 else n * fact(n - 1); Note that bindings with fun are always recursiv e. John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

  12. In tro duction to F unctional Programming: Lecture 2 12 Higher order functions Curried functions are an example of a function that giv es another function as a result. W e can also de�ne functions that tak e other functions as argumen ts. This one tak es a p ositiv e n in teger n and a function f and returns f , i.e. f � � � � � f ( n times): - fun funpow n f x = if n = 0 then x else funpow (n - 1) f (f x); > val funpow = fn : int -> ('a -> 'a) -> 'a -> 'a - funpow 20 (fn x => 2 * x) 1; > val it = 1048576 : int John Harrison Univ ersit y of Cam bridge, 16 Jan uary 1998

Recommend


More recommend