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

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
0 PROGRAMMING IN HASKELL Chapter 10 - Declaring Types and Classes.
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
IAT 800 Foundations of Computational Art and Design Lecture 2.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Function tax and buy_one double tax (double price, double tr) { return price*tr; } double buy_one() { double p; cout > p;
Saturday May 02 PST 4 PM. Saturday May 02 PST 10:00 PM.
Types A type consists of –a set of values –a set of operations on those values Types can be –primitive (atomic, non-decomposable) –composite (includes.
Section 2 - More Basics. The char Data Type Data type of a single character Example char letter; letter = 'C';
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ (4)
Operators, Functions and Modules1 Pattern Matching & Recursion.
Week 9 - Friday.  What did we talk about last time?  typedef  Linked lists.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
1 Functional Programming Lecture 6 - Algebraic Data Types.
Code Grammar. Syntax A set of rules that defines the combination of symbols and expressions.
Com Functional Programming Regular Expressions and Abstract Data Types Marian Gheorghe Lecture 14 Module homepage Mole &
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Overview of the Haskell 98 Programming Language
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CS1372: HELPING TO PUT THE COMPUTING IN ECE CS1372 Some Basics.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
LCC 6310 Computation as an Expressive Medium Lecture 2.
Computer Science I Storing data. Binary numbers. Classwork/homework: Catch up. Do analysis of image types.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Chapter SevenModern Programming Languages1 A Second Look At ML.
Selection Executing Statements Selectively. Review We’ve seen that the C++ if statement permits a Statement to be executed selectively: if (Expression)
CSED101 INTRODUCTION TO COMPUTING FUNCTION ( 함수 ) 유환조 Hwanjo Yu.
More Selection Executing Statements Selectively Chap. 7 (Read § & Part of Picture: Boolean Logic and Digital Design) 1.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
More Data Types CSCE 314 Spring CSCE 314 – Programming Studio Defining New Data Types Three ways to define types: 1.type – Define a synonym for.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
7/7/20161 Programming Languages and Compilers (CS 421) Dennis Griffith 0207 SC, UIUC Based in part on slides by Mattox.
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
A lightening tour in 45 minutes
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
Types and Classes in Haskell
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
IAT 800 Foundations of Computational Art and Design
LCC 6310 Computation as an Expressive Medium
Functions, Patterns and Datatypes
Review Previously User-defined data types: records, variants
Presentation transcript:

CSED101 INTRODUCTION TO COMPUTING SUM TYPE 유환조 Hwanjo Yu

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

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 -> > > 15 –3–3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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