Nicholas Shahan Spring 2016

Slides:



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

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.
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 =
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 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures 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 7 First-Class Functions
ML: a quasi-functional language with strong typing
CSE 341 Section 9 Nick Mooney Spring 2017
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CMSC 330: Organization of Programming Languages
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
Nicholas Shahan Spring 2016
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
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
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 9 Function-Closure Idioms
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
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
Spencer Pearson Spring 2017
CSE 341 Section 2 Nick Mooney Spring 2017
Adapted from slides by Nicholas Shahan and Dan Grossman
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
Nicholas Shahan Spring 2016
CSE-321 Programming Languages Introduction to Functional Programming
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 12 Equivalence
Nicholas Shahan Spring 2016
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 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
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.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 12 Equivalence
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

Nicholas Shahan Spring 2016 CSE 341 Section 3 Nicholas Shahan Spring 2016 Adapted from slides by Cody A. Schroeder, and Dan Grossman

Today’s Agenda Standard Library Documentation (for HW3) Anonymous Functions “Unnecessary Function Wrapping” Returning Functions High-Order Functions Map Filter Fold More Practice Tree example Expression example

What is in a Standard Library? Things that you simply can’t implement on your own. Creating a timer, opening a file, etc. Things that are so common a “standardized” version will save you time and effort List.map, string concatenation, etc. A standard library makes writing and reading code easier. Common operations don’t have to be implemented, and are immediately recognizable.

Standard Library Documentation Online Documentation http://www.standardml.org/Basis/index.html http://www.smlnj.org/doc/smlnj-lib/Manual/toc.html Helpful Subset Top-Level http://www.standardml.org/Basis/top-level-chapter.html List http://www.standardml.org/Basis/list.html ListPair http://www.standardml.org/Basis/list-pair.html Real http://www.standardml.org/Basis/real.html String http://www.standardml.org/Basis/string.html

Anonymous Functions fn pattern => expression An expression that evaluates to a new function with no name Usually used as an argument or returned from a higher-order function Almost equivalent to the following: Do something to 5 and cube examples let fun name pattern = expression in name end The difference is that anonymous functions cannot be recursive!

"Unnecessary Function Wrapping" fn x => f x f vs. When called both functions will evaluate to the same result However, one creates an unnecessary function to wrap tl Compare to: if e1 then true else false el vs. Bad Style: Lose Points Good Style: Happy TA  if x > 0 then true else false x > 0 n_times((fn ys => tl ys), 3, xs) n_times(tl, 3, xs)

Returning Functions Remember - Functions are first-class values We can return them from functions Example: fun double_or_triple f = if f 7 then fn x => 2 * x else fn x => 3 * x Translation example Has type (int -> bool) -> (int -> int) The REPL will print (int -> bool) -> int -> int because it never prints an unnecessary parenthesis

High-order Hall of Fame fun map (f, xs) = case xs of [] => [] | x::xs’ => (f x)::(map(f, xs’)) fun filter (f, xs) = case xs of [] => [] | x::xs’ => if f x then x::(filter(f, xs’)) else filter(f, xs’)

Fold Fold (synonyms/close relatives reduce, inject, etc.) is another very famous iterator over recursive structures Accumulates an answer by repeatedly applying a function f to the answer so far fold(f, acc,[x1, x2, x3, x4]) computes f(f(f(f(acc, x1),x2),x3),x4) fun fold (f, acc, xs) = case xs of [] => acc | x::xs’ => fold(f, f(acc, x), xs’) val fold = fn : ('a * 'b -> 'a) * 'a * 'b list -> 'a

Practice - Tree Example (* Generic Binary Tree Type *) datatype 'a tree = Empty | Node of 'a * ' a tree * ' a tree (* Apply a function to each element in a tree. *) val tree_map = fn: (' a -> 'b) * 'a tree -> 'b tree (* Returns true iff the given predicate returns true when applied to each element in a tree. *) val tree_all = fn: (' a -> bool) * 'a tree -> bool

Practice - Expression Example (* Modified expression datatype from lecture 5. Now there are variables. *) datatype exp = Constant of int | Negate of exp | Add of exp * exp | Multiply of exp * exp | Var of string (* Do a post order traversal of the given exp. At each node, apply a function f to it and replace the node with the result. *) val visit_post_order = fn : (exp -> exp) * exp -> exp (* Simplify the root of the expression if possible. *) val simplify_once = fn : exp -> exp (* Almost the same as evaluate but leaves variables alone. *) val simplify = fn : exp -> exp