Agenda SML Docs First-Class Functions Examples Standard Basis

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

Modern Programming Languages, 2nd ed.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
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.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
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.
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.
CSE-321 Programming Languages Introduction to Functional Programming POSTECH March 8, 2006 박성우.
Principles of programming languages 12: Functional programming
CSE341: Programming Languages Lecture 7 First-Class Functions
ML: a quasi-functional language with strong typing
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE 341 Section 4 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
CSE341: Programming Languages Lecture 7 First-Class Functions
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 12 Equivalence
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 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
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 2 Functions, Pairs, Lists
CSE 341 Section 2 Nick Mooney Spring 2017
Functions, Patterns and Datatypes
CSE 341 Section 3 Nick Mooney Spring 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
CSE341: Programming Languages Lecture 7 First-Class Functions
With thanks to Nick Mooney, Spencer Pearson & Alexander Lent
Functions, Patterns and Datatypes
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.
ormap, andmap, and filter
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
Functions, Patterns and Datatypes
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
Higher-Order Functions and Closures
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

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

Agenda SML Docs First-Class Functions Examples Standard Basis Anonymous Style Points Higher-Order Examples

Standard Basis 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 An Anonymous Function fn pattern => expression An expression that creates a new function with no name. Usually used as an argument to a higher-order function. Almost equivalent to the following: let fun name pattern = expression in name end The difference is that anonymous functions cannot be recursive!!!

Anonymous Functions What's the difference between the following two bindings? val name = fn pattern => expression; fun name pattern = expression; Once again, the difference is recursion. However, excluding recursion, a fun binding could just be syntactic sugar for a val binding and an anonymous function. This is because there are no recursive val bindings in SML.

Unnecessary Function Wrapping What's the difference between the following two expressions? (fn xs => tl xs) vs. tl STYLE POINTS! Other than style, these two expressions result in the exact same thing. However, one creates an unnecessary function to wrap tl. This is very similar to this style issue: (if ex then true else false) vs. ex

Higher-Order Functions A function that returns a function or takes a function as an argument. Two Canonical Examples map : ('a -> 'b) * 'a list -> 'b list Applies a function to every element of a list and return a list of the resulting values. Example: map (fn x => x*3, [1,2,3]) === [3,6,9] filter : ('a -> bool) * 'a list -> 'a list Returns the list of elements from the original list that, when a predicate function is applied, result in true. Example: filter (fn x => x>2, [~5,3,2,5]) === [3,5] Note: List.map and List.filter are similarly defined in SML but use currying. We'll cover these later in the course.

Broader Idea Functions are Awesome! SML functions can be passed around like any other value. They can be passed as function arguments, returned, and even stored in data structures or variables. Functions like map are very pervasive in functional languages. A function like map can even be written for other data structures such as trees. piecewise(~3.0) ~3.0;

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 treeMap = fn : ('a –> 'b) * 'a tree –> 'b tree (* Returns true iff the given predicate returns true when applied to each element in a tree. *) val treeAll = fn : ('a –> bool) * 'a tree –> bool