Programming Languages Dan Grossman 2013

Slides:



Advertisements
Similar presentations
Homework What we have learnt Variable binding: val x = e; Conditionals: if e1 then e2 else e3 Function binding:fun x0 (x1 : t1,..., xn :
Advertisements

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.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CSE341: Programming Languages Lecture 12 Equivalence Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 3 Local bindings, Options, Benefits of No Mutation Dan Grossman Fall 2011.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSE 341 : Programming Languages Lecture 5 More Datatypes and Pattern Matching Zach Tatlock Spring 2014.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 11 Type Inference
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2017.
Programming Languages Dan Grossman 2013
ML Again ( Chapter 7) Patterns Local variable definitions
Programming Languages Dan Grossman 2013
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 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 12 Equivalence
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 11 Type Inference
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.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Winter 2018.
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 5 More Datatypes and Pattern-Matching Doug Woos Winter 2018.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 3 Local Bindings; Options; Benefits of No Mutation Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
Programming Languages Dan Grossman 2013
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
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
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Programming Languages Dan Grossman 2013 Options

Dan Grossman, Programming Languages Motivating Options Having max return 0 for the empty list is really awful Could raise an exception (future topic) Could return a zero-element or one-element list That works but is poor style because the built-in support for options expresses this situation directly Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Options t option is a type for any type t (much like t list, but a different type, not a list) Building: NONE has type 'a option (much like [] has type 'a list) SOME e has type t option if e has type t (much like e::[]) Accessing: isSome has type 'a option -> bool valOf has type 'a option -> 'a (exception if given NONE) Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Example fun better_max (xs : int list) = if null xs then NONE else let val tl_ans = better_max(tl xs) in if isSome tl_ans andalso valOf tl_ans > hd xs then tl_ans else SOME (hd xs) end val better_max = fn : int list -> int option Nothing wrong with this, but as a matter of style might prefer not to do so much useless “valOf” in the recursion Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Example variation fun better_max2 (xs : int list) = if null xs then NONE else let (* ok to assume xs nonempty b/c local *) fun max_nonempty (xs : int list) = if null (tl xs) then hd xs else let val tl_ans = max_nonempty(tl xs) in if hd xs > tl_ans else tl_ans end SOME (max_nonempty xs) Jan-Mar 2013 Dan Grossman, Programming Languages