lec2 A crash course in ML

lec2 A crash course in ML - " \$" \$"" 0 1 2...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ! "# \$ % % " \$ () * " % , - .* " 0 ,, 1 ( 2 ) 2 3 / ! 4 8 % 4 & % / \$ ' ! +% 9% 05 6 2 :% /7 "/ \$ ; '1 1 3 1 \$< 1 % 3 5 Expression ; ; > @ 1 % ? \$8 & 8 % < ; < ; < ; ) 8 4+ A 1 % ! 3 \$ & % =% & & \$ ;% 4& C % D4 \$ ;% 4E B /;% " & ; " & ; " & ; =% & ;% =% 4% \$ 1 , 4 , 1; 1 1 E > + \$+ \$ let x = e; @7 \$ E! \$ ! \$ = B & & , =% + e xA # let x = 2+2;; val x : int = 4 1 > 2 3 + \$ 1 1 # let val x # let val y # let val z # x : y : z : \$+ \$ \$ =% @+ \$A & > 1 \$, + \$& \$+ \$ + + \$ D x & \$ 1 C < <! = 2+2;; int = 4 = x * x * x;; int = 64 = [x;y;x+y];; int list = [4;64;68] & 11 % \$ F # let p = a + 1; Characters 8-9: let p = a + 1 ;; ^ Unbound value a 1 ;+ \$ ;% 1 E, + \$ =% @ % ;A & + 7 \$ +; ;+ \$ & 83 & 1 + let tempVar = x + 2 * y in tempVar * tempVar ;; G9 9 • tempVar + in … ;; , " & + [email protected] \$ ; \$ \$ =% + \$; # let val x val y val z (x,y,z) = (2+3,“a”^“b”, 1::[2]);; : int = 5 : string = “ab” : int list = [1;2] 1 % AD 7 \$ 7 ! , +; 83 1 = , 1 4+ + > E # let h::t = [1,2,3];; Warning P: this pattern-matching not exhaustive. val h : int = 1 val t : int list = [2,3] Expression ) ; ! B # let h::t = ; Exception: Match_failure # let l = [1;2;3]; val l = [1;2;3]: list - val h::t = l; Warning: Binding not exhaustive val h = 1 : int val t = [2,3] : int /;% & ; & ; & ; =% & ;% ', 1 & F ? ' ;+ , ;! % ;C 1 , FD 2 % = ;% C, D 7 \$; =% 1 F ' C, D + 7 \$; =% , 8H ( ! 1 [email protected] %% 1 AD & \$ < & < 7 \$, & < & @7 \$; =% A fun x -> x+1;; 8H # let inc = fun x -> x+1 ; val inc : int -> int = fn # inc 0; val it : int = 1 # inc 10; val it : int = 11 , fun x -> x+1;; ( ! 1 [email protected] %% 1 AD & \$ < & < 7 \$, & < & @7 \$; =% A , 1 % ; B & ' C, D 7 \$; =% 7 \$ ' C, D 7 \$; =% , fun x => fn y => x<y; fun (x,y) -> x<y; C I D 8H + , 8H C 8H + D ) ( ! 1 [email protected] %% 1 AD & \$ < & < 7 \$, & < & @7 \$; =% A F' , 1 1 , 1 , 1 % ; B & # let lt = fun x -> fn y -> x < y ; val lt : int -> int -> bool = fn # let is5Lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false; ' \$ C, D ! 7 \$; =% + E , ' \$, , 1 + \$ # let neg = fun f -> fun x -> not (f x); … # let neg f x = not (f x); val neg : int -> int -> bool = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn; # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; fun f -> fun x -> not(f x); C5 8H+ D 8H C5 8H + D ', 1 1 6 , 1 # let neg = fun f -> fun x -> not (f x); val lt : int -> int -> bool = fn # let is5gte = neg is5lt; val is5gte : int -> bool = fn # is5gte 10; val it : bool = false; # is5gte 2; val it : bool = true; (*…odd, even …*) 3 @, ? , @ % 1 A E E 7 \$; =% A, 1 @% A, 1 - let rec filter f l = match l with -> | (h::t)-> if f h then h::(filter f t) else (filter f t);; val filter : (‘a->bool)->‘a list->‘a lisi) = fn # let list1 = [1,31,12,4,7,2,10];; # filter is5lt list1 ;; val it : int list = [31,12,7,10] # filter is5gte list1;; val it : int list = [1,2,10] # filter even list1;; val it : int list = [12,4,2,10] # let partition f l = (filter f l, filter (neg f) l); val partition :(‘a->bool)->‘a list->‘a lisi * ‘a list = fn # let list1 = [1,31,12,4,7,2,10]; - … # partition is5lt list1 ; val it : (int list * int list) = ([31,12,7,10],[1,2,10] # partition even list1; val it : (int list * int list) = ([12,4,2,10],[1,31,7]) ' 16 E # 2 <= 3;; … val it : bool = true # “ba” <= “ab”;; val it : bool = false # let lt = (<) ;; val it : ‘a -> ‘a -> bool = fn # lt 2 3;; val it : bool = true; # lt “ba” “ab” ;; val it : bool = false; @J 16 A, 1 let rec sort l = match l with -> | (h::t) -> let (l,r) = partition ((<) h) t in (sort lt l)@(h::(sort lt r)) ;; # let is5Lt = lt 5; val is5lt : int -> bool = fn; # is5lt 10; val it : bool = true; # is5lt 2; val it : bool = false; !4 + + E 7 Expressions C ; % @ =D 8 1A + = 18 @ ; 1A > E C 1 D 7 /;% /;% < < 3 " =% 1 & 0, 16 , =% % 1 , =% @! 43 , 8 ;% \$A , \$ ;% , J =% ;% , < 3 " =% @ ;% A , \$ 1 % % & 4 7 2; 2+3; 7-4; /;% i i: e1: e2 e1+e2 e1: e2 e1-e2 e1: e2 e1 * e2 i e1 e2 e1+e2 v1+v2 e1 e2 e1-e2 e1 e2 e1*e2 7 2.0 /;% < :< , i e1 + e2 i: e1: e2 e1 +.e2 e1: e2 e1-.e2 e1: e2 e1/.e2 i e1 e2 e1+.e2 e1 e2 e1-.e2 e1 e2 e1/.e2 : e1 + e2 2.0 +. 3.0 e1 - e2 7.0 –. 4.0 < e1 - e2 (2+3)*(7-4); : e1 * e2 (2.0+3.0) /. (7.0-4.0) < << e1 / e2 =% /;% > 1 1 + , % \$, % \$, +8 =% ;% , +8 =% & , +8 =% =% /;% > 1 1 + , % \$, % \$, +8 =% ;% , +8 =% & , +8 =% 7 “ab” /;% @ +A 7 s s: s true /;% , + b b: e1: e2 e1=e2 e1: e2 e1<e2 e : not e e1: e2 e1&&e2 b e1 e1=e2 e1 e1<e2 e not e e1 e1&&e2 e2 e2 e1 = e2 (“ab”=“cd”) “ab” ^ “cd” @ +1\$A e1^e2 e1: e2 e1^e2 e1 e1^e2 e2 2 < 3 e1 < e2 e2 not(2<3) , not e =% /;% > 1 1 + , % \$, % \$, +8 =% ;% , +8 =% & , +8 =% not (2<3) && (“ab”=“cd”) , e1 && e2 7 /;% + e1: e e1=e e1 e1=e2 e2 /;% “pq” ^ 9; e1: e1^e e e1: e e1 + e (“ab”=“cd”) , e1 = e2 (2 + “a”); " C? D J 2 , 2 + 1 ; =% % & \$ =% + 4& 8 4 ;% & ;% " E =1 % , 2 , 1 B E! ;B " =% " /;% " ? , =% + 1 % +8 =% , \$, ! +8 =% ;% , +8 =% ! 8 ;% \$ 8 ;% \$ ! 0 * (2 + “a”); 5 % = ;% (2+2 , 7>8); / % " C94, D % = ;% + , ; ,= \$ C I / % . C 4 @ +1\$A4 , D (9-3,“ab”^“cd”,7>8) I+ I+ D e1: e2 (e1,e2,…,enD e1: (e1,e D e2 e1 e2 (e1,e2) e1 e2 (e1,e2,…,en) en " "/ % 1 1 + & \$ ,, \$ ;% % % = ;% " " 1 \$ ;% * ;% ; 1L M N 1 \$ + \$ , % ! \$ N% + O NN % = ;% [1+1;2+2;3+3;4+4]; P N9N NKQ [“a”;“b”; “c”^“d”]; [email protected] [email protected]+AN @1\$AQ 1 \$ \$ E [(1,“a”^“b”);(3+4,“c”)]; PC [email protected] [email protected] C I D {name=“ranjit”; age=27; pass=false} {name=“ranjit”;age=28;pass=false}.age K [[1];[2;3];[4;5;6]]; PP QNP N QNP9N:N QQN C D " " A + \$ \$ & . , ; C << , D {age=29;name=“ranjit”;pass=false}.name @ % = ;% [e1;e2;e3;…] : e1: e2 e3 [e1;e2;e3;…] % = ;% e1 e2 [e1;e2;…] e3 e1: e2 e1::e2 <<1 1::[2;3] P N N Q 1 @ % A e1 e2 e1::e2 ' & [1; “pq”]; ;% 1::[“b”; “cd”]; ; @1 A , ;% 6 % = ;% '%% [email protected] % A E1 1 % = ;% \$ " /! @ % P N N N9N:Q E\$ 1 , A hd C \$D \$ tl C 1 D P N N9N:Q [1;2]@[3;4] P N N N9Q e1: e2 [email protected] e1 e2 [email protected] hd [1;2;3;4;5] tl [1;2;3;4;5] [email protected][“b”; “cd”]; [1]@[“b”; “cd”]; ; %% \$ , ;% [email protected] [email protected]+AN @1\$AQ hd [“a”;“b”;“cd”] @ A tl [“a”;“b”;“cd”] [email protected]+AN @1\$AQ PC [email protected] [email protected] hd [(1,“a”);(7,“c”)] tl [(1,“a”);(7,“c”)] C I PCG4 @1AQ D PPQNP N N QNP9N:QQ hd [;[1;2;3];4;5] tl [;[1;2;3];4;5] P N N9N:Q ( ( \$ \$ \$/ e : hd e e hd e / e : null e % e null e e null e !" / e : tl e e tl e null [“a”;“b”;“c”] null , (hd [;[1;2;3]]) = (hd [;[“a”]]) e1: e e1=e null 0 1 % = 18 @ ; Expressions C ; % @ =D 8 1A 1A > C 1 D 1 % " ? " , " 7 " " / % @! , =% 43 , % =% + -* +. -. *. = < && || not R 4 = \$ /;% < < 3 " =% 1 & 0, 1 \$ + .fieldname ,& 4 , \$ ,, ;% 16 , =% % 1 8 ;% \$A , \$ ;% , J =% 2 ;% , < 3 " =% @ ;% A , \$ 1 % % & " 2 + 4S 4 \$4 4 \$ \$ + ,& 4 , ;% 7 ? ,8 8 =% if (1 < 2) then 5 else 10 : [email protected] [email protected]\$AQ ? ,8 8 =% if (1 < 2) then [1;2] else 5 if false then [1;2] else 5 if (1 < 2) then [“ab”;“cd”] else [“x”] ? ,8 8 ; =% =% 4 F + 1 " / 8 + =%4 1 ;% 8 + =% , =% & ;% F 2 E! if e1 then e2 else e3 e1 e1 : if e1 then e2 else e3 e1 e2 if e1 then e2 else e3 : if e1 then e2 else e3 e1 e2 if e1 then e2 else e3 ? ,8 8 e1 =% : if e1 then e2 else e3 = > + \$7 \$ " / 2 J 8 + =%4 ;% , 8 + =% =% & ;% F if 1>2 then [1;2] else PQ if 1<2 then else [“a”] PQ (if 1>2 then [1;2] else )=(if 1<2 then else [“a”]) 8 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online