Programming Languages Dan Grossman 2013 Datatype-Programming in Racket Without Structs.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions 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 17 Structs, Implementing Languages, Implementing Higher-Order Functions 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 =
Programming LanguagesSection 61 Programming Languages Section 6. Racket v.s. ML Xiaojuan Cai Spring 2015.
CSE341: Programming Languages Structs, Implementing Languages, Implementing Higher-Order Functions Alan Borning (slides “borrowed” from Dan Grossman) Fall.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
CSE 341 : Programming Languages Lecture 4 Records, Datatypes, Case Expressions Zach Tatlock Spring 2014.
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.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
Programming Languages Dan Grossman 2013 Optional: Dynamic Dispatch Manually in Racket.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Autumn 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
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 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs 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 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Preparing for MUPL! Justin Harjanto
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 4 Records, Datatypes, Case Expressions Dan Grossman Winter 2018.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
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 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
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 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Adapted from slides by Nicholas Shahan and Dan Grossman
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2016.
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
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 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
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
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

Programming Languages Dan Grossman 2013 Datatype-Programming in Racket Without Structs

Life without datatypes Racket has nothing like a datatype binding for one-of types No need in a dynamically typed language: –Can just mix values of different types and use primitives like number?, string?, pair?, etc. to “see what you have” –Can use cons cells to build up any kind of data This segment: Coding up datatypes with what we already know Next segment: Better approach for the same thing with structs –Contrast helps explain advantages of structs Jan-Mar 20132Dan Grossman, Programming Languages

Mixed collections In ML, cannot have a list of “ints or strings,” so use a datatype: In Racket, dynamic typing makes this natural without explicit tags –Instead, every value has a tag with primitives to check it –So just check car of list with number? or string? Jan-Mar 20133Dan Grossman, Programming Languages datatype int_or_string = I of int | S of string fun funny_sum xs = (* int_or_string list -> int *) case xs of [] => 0 | (I i)::xs’ => i + funny_sum xs’ | (S s)::xs’ => String.size s + funny_sum xs’

Recursive structures More interesting datatype-programming we know: Jan-Mar 20134Dan Grossman, Programming Languages datatype exp = Const of int | Negate of exp | Add of exp * exp | Multiply of exp * exp fun eval_exp e = case e of Constant i => i | Negate e2 => ~ (eval_exp e2) | Add(e1,e2) => (eval_exp e1) + (eval_exp e2) | Multiply(e1,e2)=>(eval_exp e1)*(eval_exp e2)

Change how we do this Previous version of eval_exp has type exp -> int From now on will write such functions with type exp -> exp Why? Because will be interpreting languages with multiple kinds of results (ints, pairs, functions, …) –Even though much more complicated for example so far How? See the ML code file: –Base case returns entire expression, e.g., (Const 17) –Recursive cases: Check variant (e.g., make sure a Const ) Extract data (e.g., the number under the Const ) Also return an exp (e.g., create a new Const ) Jan-Mar 20135Dan Grossman, Programming Languages

New way in Racket See the Racket code file for coding up the same new kind of “ exp -> exp ” interpreter –Using lists where car of list encodes “what kind of exp” Key points: Define our own constructor, test-variant, extract-data functions –Just better style than hard-to-read uses of car, cdr Same recursive structure without pattern-matching With no type system, no notion of “what is an exp” except in documentation –But if we use the helper functions correctly, then okay –Could add more explicit error-checking if desired Jan-Mar 20136Dan Grossman, Programming Languages

Optional: Symbols Will not focus on Racket symbols like 'foo, but in brief: –Syntactically start with quote character –Like strings, can be almost any character sequence –Unlike strings, compare two symbols with eq? which is fast Jan-Mar 20137Dan Grossman, Programming Languages