Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu.

Similar presentations


Presentation on theme: "CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu."— Presentation transcript:

1 CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu

2 Review: Function as argument and output  h(x) = f(x) + g(x)  let combine f g = let h x = f x + g x in h  let combine f g = fun x -> f x + g x  # let h = combine (fun x -> x + 1) (fun y -> y - 1) in h 0;;  - : int = 0 –2–2

3 Review: Tail-recursive function  Every recursive function can be transformed to a tail-recursive function.  let sum n =  let rec sum_reverse i s = if i = n then s else sum_reverse (i + 1) (s + i + 1)  in  sum_reverse 1 1;;  sum 5 -> sum_reverse 1 1 -> 2 3 -> 3 6 -> 4 10 -> 5 15 -> 15 –3–3

4 Review: Tail-recursive function  We can use multiple arguments to store intermediate results in a tail-recursive function  E.g., fibonacci function  let fib n =  let rec fib’ i p q = if i = n then p else fib’ (i + 1) (p + q) p  in  if n = 1 then 1  else fib’ 2 1 1;; –4–4

5 How types are implemented  Machine understand only 0 and 1, i.e., a bit.  An integer is typically implemented using 32bits  A float is also implemented using 32bits  CPU usually can compute only integers or floats.  A bool is implemented using integer.  A tuple of two integers is implemented using two integers  A character is implemented using 8bits or a byte  A string is implemented using multiple characters. –5–5

6 More types  Types are created not for machine but for human.  One can create more types like…  Color = Red, Green, Blue, … (or 0, 1, 2, …)  Date = Monday, Tuesday, … Sunday (or 1,.., 7)  Integer comparison type = Less, Equal, Greater (or -1, 0. 1) –6–6

7 Sum type  Sum type definition syntax  type = | |…;;  E.g.,  type color = Red | Green | Blue;;  type day = Sunday | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday;;  type order = Less | Equal | Greater;;  # Red;; - : color = Red  # (Red, Sunday, Less);; - : color * day * order = (Red, Sunday, Less) –7–7

8 Sum type comparison  Use “=“ or “<>” for comparing two values of the same type  # Red = Green;; - : bool = false  # Red <> Green;; - : bool = true  # Red = Sunday;; This expression has type day but is here used with type color –8–8

9 Pattern matching expression  match e with  | ->  …  E.g.,  match x with | Red -> 0 | Green -> 1 | Blue -> 2 –9–9

10 Pattern matching expression  # let convert x =  match x with | Red -> 0 | Green -> 1;;  Warning: this pattern-matching is not exhaustive.  Here is an example of a value that is not matched:  Blue  val convert : color -> int = –10

11 Pattern matching expression  let is_red x =  match x with | Red -> true | Green -> false | Blue -> false  Or, use a variable like ‘_’  let is_red x =  match x with | Red -> true | _ -> false –11

12 Pattern matching expression  let convert_red x =  match x with | Red -> Green | y -> y  Or, use ‘_’  let convert_red x =  match x with | Red -> Green | _ -> x –12

13 Sum type with arguments  type icolor = Red0 | Green0 | Blue0 | Red1 | Green1 | Blue1 | · · · | Red255 | Green255 | Blue255;;  type icolor = Red of int | Green of int | Blue of int  # Red 0;;  - : icolor = Red 0  # Green 10;;  - : icolor = Green 10  # Blue 0;;  - : icolor = Blue 0 –13

14 Sum type with arguments  type acolor = Rgb of int * int * int | Gray of int | Black  # Rgb (100, 100, 100);;  - : acolor = Rgb (100, 100, 100)  # Gray 100;;  - : acolor = Gray 100  # Black;;  - : acolor = Black –14

15 Sum type with arguments  let rgb_of_acolor x =  match x with | Rgb (r, g, b) -> (r, g, b) | Gray g -> (g, g, g) | Black -> (0, 0, 0)  let is_gray x =  match x with | Rgb (r, g, b) -> r = g && g = b | _ -> true –15

16 Polymorphic sum type  Define a type sset (“simple set”)  type int_sset = Empty | Singleton of int | Pair of int * int;;  type float_sset = Empty | Singleton of float | Pair of float * float;;  type ’a sset = Empty | Singleton of ’a | Pair of ’a * ’a;;  # Empty;;  - : ’a sset = Empty  # Singleton 1;;  - : int_sset = Singleton 1  # Singleton 1.0;;  - : float_sset = Singleton 1. –16 –# Pair (1, 2);; –- : int_sset = Pair (1, 2) –# Pair (1.0, 2.0);; –- : float_sset = Pair (1., 2.) –# Pair (0, 1.0);; –This expression has type float but is here used with type int

17 Recursive sum type  type nat = Zero | Succ of nat;;  # Zero;;  - : nat = Zero  # Succ Zero;;  - : nat = Succ Zero  # Succ (Succ Zero);;  - : nat = Succ (Succ Zero) –17

18 Recursive sum type  let is_zero n =  match n with  | Zero -> true  | Succ -> false  let rec add m n =  match m with  | Zero -> n  | Succ m’ -> Succ (add m’ n) –18

19 Recursive sum type  let rec equal m n =  match m with  | Zero -> ( match n with | Zero -> true | _ -> false )  | Succ m’ -> ( match n with | Zero -> false | Succ n’ -> equal m’ n’ ) –19

20 Recursive sum type  let rec equal m n =  match (m, n) with  | (Zero, Zero) -> true  | (Succ m’, Succ n’) -> equal m’ n’  | (_, _) -> false –20

21 Recursive sum type  type ’a mylist = Nil | Cons of ’a * ’a mylist;;  Cons (1, Cons (2, Cons (3, Nil))) –21


Download ppt "CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu."

Similar presentations


Ads by Google