CSE 341 Section 5 Winter 2018.

Slides:



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

Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
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 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 12 Modules Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
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.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
Programming LanguagesSection 41 Programming Languages Section 4. SML Remaining Topics Xiaojuan Cai Spring 2015.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 341 : Programming Languages Lecture 12 ML Modules Zach Tatlock Spring 2014.
CSE 341 : Programming Languages Lecture 8 First Class Functions 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 10 ML Modules
CSE341: Programming Languages Lecture 7 First-Class Functions
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
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 7 First-Class Functions
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 10 ML Modules
CSE-321 Programming Languages Introduction to Functional Programming
CSE 341 Section 2 Nick Mooney Spring 2017
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
341 midterm review Xinrong Zhao Autumn 2018.
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
With thanks to Alexander Lent, Nick Mooney, Spencer Pearson
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 12 Equivalence
Higher-Order Functions and Closures
Presentation transcript:

CSE 341 Section 5 Winter 2018

CSE 341: Programming Languages Midterm Review! Variable Bindings, Shadowing, Let Expressions Boolean, Comparison and Arithmetic Operations Equality Types Types, Datatypes, Type synonyms Tuples, Records and Lists Case statement, Pattern Matching Functions, Anonymous Functions, Higher Order Functions Actually Taking in Tuples, Function Closures Tail Recursion Currying Filter, Map, Fold Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages Midterm Review! Lexical Scope vs Dynamic Scope Type Inference, Polymorphic Types and Type Generality Modules Equivalence Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages Variable Bindings SML evaluation creates bindings in the environments (static and dynamic) rather than change values stored in variables. Repeated Variable names? Shadowing Let Expression allows us to create bindings in a smaller Scope Winter 2018 CSE 341: Programming Languages

Boolean, Comparison and Arithmetic Operations Boolean Operators andalso, orelse evaluates for booleans only, they are not functions (you cannot do partial evaluation with them) not is a function - op not; val it = fn : bool -> bool - List.map not [true, true, false]; val it = [false,false,true] : bool list Winter 2018 CSE 341: Programming Languages

Boolean, Comparison and Arithmetic Operations Comparison and Arithmetic Operators =, <>, equality types >, <, >=, <=, +, -, *, must take the same type on both sides ‘div’ for integers, ‘/’ for reals You cannot divide on integer by a real or vice versa Because these operators are all functions! Winter 2018 CSE 341: Programming Languages

Types, Datatypes, Type synonyms Built-in types String, int, real, bool, records, lists What about tuples? They are just syntactic sugar for records datatype keyword Allows you to create types by yourself “one of type” and recursive type type keyword “each of type”, just renaming the existing types Winter 2018 CSE 341: Programming Languages

Case statement, Pattern Matching Values and variables form patterns SML is essentially creating variable bindings of the variable with the actual value in e0. It is not checking if the value stored in the variable equals to what’s in the current environment case e0 of p1 => e1 | p2 => e2 … | pn => en Winter 2018 CSE 341: Programming Languages

Functions, Anonymous Functions, Higher Order Functions Functions actually takes in a pattern, for example, (x : int, y : bool). By pattern matching, it creates bindings of variables and values. Then the environment is bound The bounded environment along with the code creates function closure. Winter 2018 CSE 341: Programming Languages

Functions, Anonymous Functions, Higher Order Functions Anonymous Functions use keyword fn rather than fun, which cannot be recursive Tail Recursion You are not doing any more operation after getting returned value from your recursive call Winter 2018 CSE 341: Programming Languages

Functions, Anonymous Functions, Higher Order Functions Currying is taking a function with “several arguments” and make it into nested functions, which takes one argument at a time Partial evaluation: since curried functions are just nested functions, we can pass in one argument at a time in order We can take in functions as arguments Higher order functions are just those functions that return or take in functions Winter 2018 CSE 341: Programming Languages

Functions, Anonymous Functions, Higher Order Functions Classic higher order functions List.filter List.map List.foldl List.foldr What do they do? Winter 2018 CSE 341: Programming Languages

Lexical Scope vs Dynamic Scope Lexical scope: use environment where function is defined Our Function Closure so far is in lexical scope Dynamic scope: use environment where function is called Winter 2018 CSE 341: Programming Languages

Type Inference, Polymorphic Types and Type Generality Polymorphic types means it can be any type So is more general than But not more general than Polymorphic type can be any type More general means you can substitute one type by another consistently ‘a list * ‘a list -> ‘a list int list * int list -> int list int list * string list -> int list Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages Modules You can hide a function by using signatures structure MyModule = struct bindings end signature SIGNAME = sig types-for-bindings end structure MyModule :> SIGNAME = struct bindings end Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages Modules Remember from lecture you can ensure constraints on values structure Rational3 = struct type rational = int * int exception BadFrac fun make_frac (x,y) = … fun Whole i = (i,1) (* needed for RATIONAL_C *) fun add ((a,b)(c,d)) = (a*d+b*c,b*d) fun toString r = … (* reduce at last minute *) end All fractions are always in reduced form in this module Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages Equivalence Given equivalent arguments, two equivalent pieces of code: Produce equivalent results Have the same (non-)termination behavior Mutate (non-local) memory in the same way Do the same input/output Raise the same exceptions Look for function closures, dynamic and static environments and side effects like print Winter 2018 CSE 341: Programming Languages

CSE 341: Programming Languages Good Luck! Winter 2018 CSE 341: Programming Languages