CSE 341 Section 5. Winter 2018

Similar documents
CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSci 4223 Principles of Programming Languages

CMSC 330, Fall 2013, Practice Problems 3

CSE341, Fall 2011, Lecture 12 Summary

CSE 341: Programming Languages

Begin at the beginning

CSE341 Spring 2017, Midterm Examination April 28, 2017

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

A Brief Introduction to Standard ML

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Datatype declarations

Exercises on ML. Programming Languages. Chanseok Oh

Programming Languages

A quick introduction to SML

SML A F unctional Functional Language Language Lecture 19

CSE 341 : Programming Languages Midterm, Spring 2015

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Programming Languages

CSE3322 Programming Languages and Implementation

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programming Languages and Compilers (CS 421)

Part I: Written Problems

Fall Lecture 3 September 4. Stephen Brookes

List Functions, and Higher-Order Functions

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Lists. Michael P. Fourman. February 2, 2010

Recap: Functions as first-class values

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Australian researchers develop typeface they say can boost memory, that could help students cramming for exams.

Introduction to OCaml

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Introduc)on To Standard ML

CSE-321 Programming Languages 2010 Midterm

CSE 341 : Programming Languages

The type checker will complain that the two branches have different types, one is string and the other is int

Pattern Matching and Abstract Data Types

Introduction to SML Getting Started

Lecture 2: The Basis of SML

Programming Languages. Example 5. Example 4. CSE 130 : Fall type, can reuse code for all types! let rec cat l = match l with

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

ITT8060: Advanced Programming (in F#)

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

CSE-321 Programming Languages 2012 Midterm

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

CSCI-GA Scripting Languages

Programming Languages

News. CSE 130: Programming Languages. Environments & Closures. Functions are first-class values. Recap: Functions as first-class values

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Higher-Order Functions

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Functional programming Primer I

Function definitions. CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Example, extended. Some gotchas. Zach Tatlock Winter 2018

CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists. Zach Tatlock Winter 2018

CSE 341, Autumn 2005, Assignment 3 ML - MiniML Interpreter

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

02157 Functional Programming Lecture 2: Functions, Basic Types and Tuples

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring SOLUTIONS

Bindex: Naming, Free Variables, and Environments

CSE 341: Programming Languages

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

UNIVERSITETET I OSLO

Functional Paradigm II

Programming Languages

CSCI-GA Final Exam

CSE 341: Programming Languages

Bindex: Naming, Free Variables, and Environments SOLUTIONS

CSCC24 Functional Programming Typing, Scope, Exceptions ML

A Concepts-Focused Introduction to Functional Programming Using Standard ML Sample Exam #1 Draft of August 12, 2010

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

SML Style Guide. Last Revised: 31st August 2011

Functional Programming. Pure Functional Programming

Inductive Data Types

CSE341: Programming Languages Winter 2018 Unit 3 Summary Zach Tatlock, University of Washington

Specification and Verification in Higher Order Logic

F28PL1 Programming Languages. Lecture 14: Standard ML 4

CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

List Processing in SML

CS3110 Spring 2016 Lecture 6: Modules

Sample Exam; Solutions

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

List Processing in SML

Programming Languages

Transcription:

CSE 341 Section 5 Winter 2018

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 2

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

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

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 5

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 6

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 7

Case statement, Pattern Matching case e0 of p1 => e1 p2 => e2 pn => en 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 Winter 2018 CSE 341: Programming Languages 8

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 9

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 10

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 11

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 12

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 13

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

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 15

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 Winter 2018 CSE 341: Programming Languages 16

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 17

Good Luck! Winter 2018 CSE 341: Programming Languages 18