c03 - C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on ML: Page 1 of 20 Types primitive type : int, real, bool, unit, string, char, ..., list type, record type, tuple type, function type; type abbreviation ; datatype definition ; in ML, the type for each expression is inferred and checked for consistency at compile time ! if 1 < 2 then 3 else 4.0; type king = {name : string, born : int, crowned : int, died : int, quote : string} fun lifetime(k : king ) = #died k - #born k fun fac n = if n = 0 then 1 else n * (fac(n-1)) C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on ML: Page 2 of 20 Polymorphic Functions polymorphic functions can be applied to arguments of different types, polymorphic functions usually do something simple !- fun ident x = x val ident = fn : a -> a- fun pairself x = (x,x) val pairself = fn : a -> a * a- fun pairint (x : int) = (x,x) val pairint = fn : int -> int * int- fun fst (x,y) = x val fst = fn : a * b -> a - fun snd (x,y) = y val snd = fn : a * b -> b- val foo = pairself 4.0; val foo = (4.0,4.0) : real * real- val bar = pairself hello; val bar = (hello,hello) : string * string- fst(foo); val it = 4.0 : real- pairint(4.0); std_in:13.1-13.12 Error: operator and operand dont agree (tycon mismatch) operator domain: int, operand: real, in expression: pairint (4.0) C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on ML: Page 3 of 20 Polymorphic Data Structures infixr 5 :: datatype a list = nil | :: of a * a list fun rev nil = nil | rev (a::r) = (rev r)@[a] val rev = fn : a list -> a list datatype a tree = LEAF of a | NODE of a * a tree * a tree datatype a tree con LEAF : a -> a tree con NODE : a * a tree * a tree -> a tree fun depth(LEAF _) = 1 | depth(NODE(_,left,right) = 1+max(depth(left),depth(right)) val depth = fn : a tree -> int val t = NODE(0, LEAF 1, LEAF 2) val t = NODE (0,LEAF 1,LEAF 2) : int tree- depth t; val it = 2 : int C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University More on ML: Page 4 of 20 More on Pattern Matching nested pattern --- use the as idiom (* example : merging two sorted list of integers *) fun merge(x : int list, ) = x | merge(, y) = y | merge(x as (a::r), y as (b::z)) = if (a > b) then (b :: (merge(x, z))) else if (a < b) then (a :: (merge(r, y))) else (a::(merge(r, z))) partial record pattern --- must fully specify the record type !...
View Full Document

This document was uploaded on 02/20/2012.

Page1 / 5

c03 - C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online