CSE 341 Lecture 3 let expressions; pattern matching Ullman

Slides:



Advertisements
Similar presentations
Modern Programming Languages, 2nd ed.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Higher-order functions in OCaml. Higher-order functions A first-order function is one whose parameters and result are all "data" A second-order function.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
CMSC 330: Organization of Programming Languages Tuples, Types, Conditionals and Recursion or “How many different OCaml topics can we cover in a single.
ML Declarations.1 Standard ML Declarations. ML Declarations.2 Declarations o Re-declaration of names in ML o The reserved words "val rec" o Pattern Matching.
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 Declarations.1 Standard ML Declarations. ML Declarations.2  Area of a circle: - val pi = ; val pi = : real - fun area (r) = pi*r*r;
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Data  Consists of constructions that can be inspected, taken apart, or joined to form.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
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.
CSE 341 Lecture 6 exceptions; higher order functions; map, filter, reduce Ullman 5.2, 5.4 slides created by Marty Stepp
Haskell Chapter 3, Part I. Pattern Matching  Pattern matching with tuples  Pattern matching with list comprehensions  As-patterns.
CSE 341 Lecture 11 a record types Ullman 7.1 slides created by Marty Stepp
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Introduction to Functional Programming and ML CS 331 Principles of Programming Languages.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
CSE 341 Lecture 4 merge sort; basic efficiency issues Ullman slides created by Marty Stepp
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 341 Lecture 8 curried functions Ullman 5.5 slides created by Marty Stepp
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Sorts, CompareTo Method and Strings
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Building Java Programs
ML: a quasi-functional language with strong typing
Chapter 15 Recursion.
PPL Lazy Lists.
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2017.
CSE 341 Lecture 5 efficiency issues; tail recursion; print
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Lecture 20 Mutation; memoization slides created by Marty Stepp
Chapter 14: Recursion Starting Out with C++ Early Objects
slides created by Marty Stepp
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
slides created by Marty Stepp
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Zach Tatlock Winter 2018.
CSE 341 Lecture 7 anonymous functions; composition of functions Ullman 5.1.3, 5.6 slides created by Marty Stepp
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2016.
CSE 341 Lecture 11 b closures; scoping rules
CSE 341 Lecture 2 lists and tuples; more functions; mutable state
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Spring 2019.
Arrays Introduction to Arrays Reading for this Lecture:
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Lecture 20 – Practice Exercises 4
CSE341: Programming Languages Lecture 6 Nested Patterns Exceptions Tail Recursion Dan Grossman Autumn 2017.
Presentation transcript:

CSE 341 Lecture 3 let expressions; pattern matching Ullman 3.3 - 3.4 slides created by Marty Stepp http://www.cs.washington.edu/341/

String and char (2.2, 2.4.5) ML's String structure has additional functions: String.size(string) (* length *) String.substring(string, start, length) String.sub(string, index) (* charAt *) function description explode(string) breaks a string into an array of characters implode(char list) combines a list of chars into a string concat(string list) merges all strings from a list into one ord(char) converts a char into its int ASCII value chr(int) converts an int ASCII value into a char

The keyword let (3.4) let val name = expression in expression end; binds a symbol to a function's "local environment" like declaring a local variable in Java the variable will be used only by the function recall that its value cannot change let expressions can appear anywhere an expression can

let example useful when you will be computing a value that is: (* The distance between points (x1,y1),(x2,y2). *) fun dist(x1, y1, x2, y2) = let val dx = x2 - x1 val dy = y2 - y1 in Math.sqrt(dx * dx + dy * dy) end; useful when you will be computing a value that is: used multiple times, or used in a complex way by the overall function's expression.

Using let with functions fun name = expression in expression end; technically, any binding (function or variable) can be made in a let-expression useful for writing "helper" functions subtasks required by a larger function recursive helpers when a function needs more parameters

Function let example (* Least common multiple (LCM) of a and b. *) fun lcm(a, b) = let fun gcd(x, y) = if y = 0 then x else gcd(y, x mod y) in a * b div gcd(a, b) end; Exercise: Change the function convertNames from last lecture to use a let helper function.

More about functions and let a function declared inside a let expression: is part of the environment of the enclosing function can refer to any of the enclosing func.'s parameters/vars defines its own local sub-environment can declare its own let sub-expressions can use parameter names that collide with those of the enclosing function, without ambiguity

Patterns (3.3) ML bindings can contain patterns to match name(s) on the left side of = with the value(s) on the right. basic pattern: one name on left (matches all of right) val point = (3, ~5); tuple pattern: tuple of names on left match parts on right val (x, y) = (3, ~5); val (p, (x2, y2)) = ((3, ~5), (4, 7)); list pattern: list of names on left; same-size list on right val [a, b, c] = [8, 2, 6];

List patterns list pattern with :: matches a head element and tail list val first::rest = [10, 20, 30, 40]; first stores 10; rest stores [20,30,40] You can break out as many elements as you like: val first::second::rest = [10, 20, 30, 40]; first stores 10; second stores 20; rest stores [30,40] list patterns can contain :: but not @

Functions and patterns fun name(pattern1) = expression1 | name(pattern2) = expression2 ... | name(patternN) = expressionN; describes the function's behavior as a series of cases, each corresponding to a pattern of parameter values better than lots of if-then-else expressions avoids a lot of calls on hd, tl, and length on lists must be exhaustive (match all possible parameter values)

Function pattern example fun factorial(0) = 1 | factorial(n) = n * factorial(n - 1); If a client calls factorial and passes 0, it matches the first pattern (base case) if a client calls factorial and passes some other value, it matches the second pattern (recursive case)

(Use patterns in your solutions.) Exercises Write a function fibonacci that accepts an integer n and produces the nth Fibonacci number, where the first two are 1 and all others are the sum of the prior 2. fibonacci(6) produces 13 Write a function evens that accepts a list and produces the elements at even-numbered indexes (0, 2, 4, ...). evens([6, 19, 2, 7]) produces [6,2] evens([3, 0, 1, ~5, 8]) produces [3,1,8] (Use patterns in your solutions.)

Inexhaustive patterns - fun evens([]) = [] = | evens(first::second::rest) = first::evens(rest); val evens = fn : 'a list -> 'a list - evens([6, 19, 2, 8, 5]); uncaught exception Match [nonexhaustive match failure] raised at: stdIn:9.58 ML raises an exception if a call doesn't match any pattern this happens when the recursion reaches evens([5]) we must add a third pattern to match a one-element list

Wildcard patterns (3.3.3) anonymous pattern _ matches any single parameter fun contains([], _) = false | contains(first::rest, value) = first = value orelse contains(rest, value); What, if any, is the difference between these? fun f1() = 42; fun f2(_) = 42;

The as keyword (3.3.2) name as pattern (* Removes any 0s from front of a list. *) fun noLeadZeros([]) = [] | noLeadZeros(lst as first :: rest) = if first = 0 then noLeadZeros(rest) else lst; if you like, you can name the entire parameter and also break its contents apart using a pattern saves us from having to write, else first :: rest;