CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture: Datatypes and Recursion.

Slides:



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

ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Functional Programming Element of Functional Programming.
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.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
CSE 341 : Programming Languages Lecture 2 Functions, Pairs, Lists Zach Tatlock Spring 2014.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CSE 341 : Programming Languages Lecture 4 Records, Datatypes, Case Expressions Zach Tatlock Spring 2014.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
CSE 341 : Programming Languages Lecture 5 More Datatypes and Pattern Matching Zach Tatlock Spring 2014.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2013.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2017.
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Doug Woos Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
Functions, Patterns and Datatypes
Class code for pythonroom.com cchsp2cs
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture: Datatypes and Recursion

What about more complex data ? We’ve seen some base types and values: –Integers, Reals, Bool, String etc. Some ways to build up types: –Products (tuples), records, “lists” –Functions Design Principle: Orthogonality –Don’t clutter core language with stuff –Few, powerful orthogonal building techniques –Put “derived” types, values, functions in libraries

Today: Building datatypes Three key ways to build complex types/values 1. “Each-of” types Value of T contains value of T1 and a value of T2 2. “One-of” types Value of T contains value of T1 or a value of T2 3. “Recursive” Value of T contains (sub)-value of same type T

Suppose I wanted … … a program that processed lists of attributes Name (string) Age (integer) DOB (int-int-int) Address (string) Height (real) Alive (boolean) Phone (int-int) (string) Many kinds of attributes: too many to put in a record can have multiple names, addresses, phones, s etc. Want to store them in a list. Can I ?

Constructing Datatypes t is a new datatype. A value of type t is either: a value of type t1 placed in a box labeled C1 Or a value of type t2 placed in a box labeled C2 Or … Or a value of type tn placed in a box labeled Cn datatype t = C1 of t1 | C2 of t2 | … | Cn of tn

Suppose I wanted … Attributes: Name (string) Age (integer) DOB (int-int-int) Address (string) Height (real) Alive (boolean) Phone (int-int) (string) datatype attrib = Name of string | Age of int | DOB of int*int*int | Address of string | Height of real | Alive of bool | Phone of int*int | of string;

Creating Values How to create values of type attrib ? datatype attrib = Name of string | Age of int | DOB of int*int*int | Address of string | Height of real | Alive of bool | Phone of int*int | of string; - val a1 = Name “Ranjit”; val x = Name “Ranjit” : attrib - val a2 = Height 5.83; val a2 = Height 5.83 : attrib - val year = 1977 ; val year = 1977 : int; - val a3 = DOB (9,8,year) ; val a3 = DOB (9,8,1977) : attrib - val a_l = [a1,a2,a3]; val a3 = … : attrib list

One-of types We’ve defined a “one-of” type named attrib Elements are one of: –String, –int, –int*int*int, –real, –bool … Can create uniform attrib lists Suppose I want a function to print attribs… datatype attrib = Name of string | Age of int | DOB of int*int*int | Address of string | Height of real | Alive of bool | Phone of int*int | of string;

How to tell whats in the box ? datatype attrib = Name of string | Age of int | DOB of int*int*int | Address of string | Height of real | Alive of bool | Phone of int*int | of string; case e of Name s => e1 | Age i => e2 | DOB (m,d,y) => e3 | Address addr => e4 | Height h => e5 | Alive b => e6 | Phone (a,n) => e7 | e => e8 Pattern-match expression: check if e is of the form … On match: – value in box bound to pattern variable – corresponding result expression is evaluated Simultaneously test and extract contents of box

Case-of is an Expression case e of Name s => e1 | Age i => e2 | DOB (d,m,y) => e3 | Address addr => e4 | Height h => e5 | Alive b => e6 | Phone (a,n) => e7 | e => e8 Pattern-match expression: check if e is of the form … On match: – value in box bound to pattern variable – corresponding result expression is evaluated Simultaneously test and extract contents of box

Case-of is an Expression Type rules ? e1, e2,…,en must have same type Which is type of whole expression case e of C1 x1 => e1 | C2 x2 => e2 | … | Cn xn => en

Benefits of case-of 1. Simultaneous test-extract-bind 2. Compile-time checks for: missed cases: ML warns if you miss a t value redundant cases: ML warns if a case never matches datatype t = C1 of t1 | C2 of t2 | … | Cn of tn case e of C1 x1 => e1 | C2 x2 => e2 | … | Cn xn => en

What about “Recursive” types ? Think about this! What are values of int_list ? datatype int_list = Nil | Cons of int * int_list NilCons(3,Nil) Cons(2,Cons(3,Nil)) Cons(1,Cons(2,Cons(3,Nil))) Nil Cons 3, Cons 2, Cons 1,

Lists aren’t built-in ! Lists are a “derived” type, built using elegant core! 1. Each-of 2. One-of 3. Recursive :: is just a pretty way to say “Cons” [] is just a pretty way to say “Nil” datatype int_list = Nil | Cons of int * int_list

Some functions on Lists : Length fun len l = case l of Nil => 0 | Cons(h,t) => 1 + (len t) Base Expression Inductive Expression Base pattern Ind pattern fun len l = case l of Nil => 0 | Cons(_,t) => 1 + (len t) fun len l = case l of Cons(_,t) => 1 + (len t) | _ => 0 Matches everything, no bindingPattern-matching in order - Must match with Nil

Some functions on Lists : Append fun append (l1,l2) = Base Expression Inductive Expression Base pattern Ind pattern Find the right induction strategy –Base case: pattern + expression –Induction case: pattern + expression Well designed datatype gives strategy

null, hd, tl are all functions … Bad ML style: More than aesthetics ! Pattern-matching better than test-extract: ML checks all cases covered ML checks no redundant cases …at compile-time: –fewer errors (crashes) during execution –get the bugs out ASAP!

Another Example: Calculator We want an arithmetic calculator to evaluate expressions like: – 5.92 ( ) * ( ) Whats a ML datatype for such expressions ? = 6.9 = =

Another Example: Calculator We want an arithmetic calculator to evaluate expressions like: – 5.92 ( ) * ( ) Whats a ML function for evaluating such expressions ? = 6.9 = =

Random Art from Expressions PA #1. Build more funky expressions, evaluate them, to produce:

Functions Expressions Values Types Functioning recursively in ML

Example : Factorial fun fac n = if n=0 then 1 else n * fac (n-1); Base Expression Inductive Expression Induction Condition

Example : Clone fun clone (x,n) = if n=0 then [] else x :: clone(x,n-1); Base Expression Inductive Expression Induction Condition

Example : interval fun interval (i,j) = if i > j then [] else i :: interval(i+1,j); Base Expression Inductive Expression Induction Condition

Example : List Maximum fun max (x,y) = if x > y then x else y ; fun listMax l = let fun helper (cur,l) = if (null l) then [] else helper (max(cur,hd l),tl l) in helper (0, l) end; Find maximum element in +ve int list Base Expression Inductive Expression Induction Condition

Example : List Maximum fun max (x,y) = if x > y then x else y fun listMax l = let fun helper (_,[]) = [] | helper (cur,h::t) = helper(max(cur,h),t) in helper (0, l) end; Find maximum element in +ve int list … in a more ML-ish way Base Expression Inductive Expression Base “pattern” Ind. “pattern”

Example : List Append fun append ([],l) = l | append (h::t,l) = h::(append (t,l)) ; Roll our Base Expression Inductive Expression Base “pattern” Ind. “pattern”

Example : List Filter funfilter (f,[]) = [] |filter (f,h::t) = let val t’ = filter (f,t) in if (f h) then h::t’ else t’ end; Base Expression Base “pattern” Inductive Expression Ind. “pattern”

Some functions on Lists : Append fun append (l1,l2) = case l1 of Nil => Nil | Cons(h,t) => Cons(h,Append (t,l2)) Base Expression Inductive Expression Base pattern Ind pattern fun len l = case l of Nil => 0 | Cons(_,t) => 1 + (len t) fun len l = case l of Cons(_,t) => 1 + (len t) | _ => 0 Matches everything, no bindingPattern-matching in order - Must match with Nil case l1 of Nil => Nil | Cons(h,t) => Cons(h,append (t,l2))