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.

Slides:



Advertisements
Similar presentations
Programming with Lists
Advertisements

Modern Programming Languages, 2nd ed.
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 First Look at ML.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
Cs776(Prasad)L7fold1 Fold Operations Abstracting Repetitions.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
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 =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
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.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
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 박성우.
Chapter 9: Functional Programming in a Typed Language.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
F28PL1 Programming Languages Lecture 13: Standard ML 3.
Overview of the Haskell 98 Programming Language
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Chapter SevenModern Programming Languages1 A Second Look At ML.
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.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Principles of programming languages 12: Functional programming
CS 550 Programming Languages Jeremy Johnson
ML: a quasi-functional language with strong typing
ML Again ( Chapter 7) Patterns Local variable definitions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSE 341 Lecture 3 let expressions; pattern matching Ullman
Higher Order Functions
Abstracting Repetitions
PROGRAMMING IN HASKELL
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 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
Higher Order Functions
Functions, Patterns and Datatypes
PROGRAMMING IN HASKELL
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Announcements Quiz 5 HW6 due October 23
Functions, Patterns and Datatypes
PROGRAMMING IN HASKELL
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Functions, Patterns and Datatypes
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

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 may appear more than once, and the order matters: [3,4,3] [3,3,4]  Elements may have any type…  …But all elements of a list must have the same type: [(1,"One"),(2,"Two")] : (int*string) list [[3.1],[],[5.7, ~0.6]]: real list list  The empty list [] has the polymorphic type ‘ a list

ML Lists.3 Building a List  Every list is either Empty or An element followed by a list  Examples: [1, 2, 3, 4] Head = 1, Tail = [2,3,4] [1, 2] Head = 1, Tail = [2] [1] Head = 1, Tail = [] [] [] The tail is a list ! The empty list cannot be disassembled

ML Lists.4 Building a List (cont.)  nil is a synonym for the empty list [] They are completely interchangeable  The operator :: (also called cons) makes a new list by combining an element and an existing list. -1 :: [2,3]; val it = [1,2,3] : int list  Every list is either nil or has the form x::xs where x is its head and xs is its tail (which is a list itself).  The infixr operator :: groups to the right.  It is a constructor, which is a special kind of function. Basically, it means it can be used in patterns  The notation [x1,x2,...,xn] stands for x1 :: x2 ::... :: xn :: nil 3::(5::(9::nil)) is [3,5,9]

ML Lists.5 Built-in Fundamental Functions  Testing lists and taking them apart null - tests whether a list is empty. Could have been defined like that: - fun null [] = true | null (_::_) = false; val null = fn : ‘a list -> bool hd – evaluates to the head of a non-empty list - fun hd (x::_) = x; **Warning: Patterns not exhaustive val hd = fn : ‘a list -> ‘a tl - evaluates to the tail of a non-empty list - fun tl (_::xs) = xs; **Warning: Patterns not exhaustive val tl = fn : ‘a list -> ‘a list Note how list constructors are used in patterns

ML Lists.6 hd and tl Examples - hd[ [ [1,2], [3] ], [ [4] ] ]; val it = [[1,2],[3]] : (int list) list - hd it; val it = [1,2] : int list - hd it; val it = 1 : int - tl [“How",“are",“you?"]; val it = [“are",“you?"] : string list - tl it; val it = [“you?"] : string list - tl it; val it [] : string list - tl it; Uncaught exception: Empty

ML Lists.7 Building the list of integers [m,m+1,...,n-1]  The implementation: -fun range (m, n) = if m=n then [] else m :: (range (m+1,n)); val range = fn : int * int -> int list - range (2, 5); val it = [2,3,4] : int list  Or as an operator: - infix --; - val op-- = range; val -- = fn : int * int -> int list ; val it = [2,3,4] : int list

ML Lists.8 take and drop  take(k, xs) contains the first k elements of xs val take = fn : ‘a list * int -> ‘a list - take (5, explode "Throw Pascal to the dogs!"); val it = [#"T", #"h", #"r", #"o", #"w"] : char list  drop(k, xs) contains all but the first k elements of xs xs = [x 1, x 2, x 3, …, x k, x k+1, …, x n ] take (k, xs)drop (k, xs) explode: converts a string to a list of chars fun take (0, _) = [] | take (i, x::xs) = x :: take (i-1, xs);

ML Lists.9 The Computation of take fun take (0, _) = [] | take (i, x::xs) = x :: take (i-1, xs); - take (3, [9,8,7,6,5,4])  9::take (2, [8,7,6,5,4])  9::(8::take (1, [7,6,5,4]))  9::(8::(7::take (0, [6,5,4])))  9::(8::(7::[]))  9::(8::[7])  9::[8,7]  [9,8,7]

ML Lists.10 The Computation of drop fun drop (0, xs) = xs | drop (i, _::xs) = drop (i-1, xs); - drop (3, [9,8,7,6,5,4])  drop (2, [8,7,6,5,4])  drop (1, [7,6,5,4]))  drop (0, [6,5,4])))  [6,5,4]

ML Lists.11 Tail Recursion  Normal recursion - fun take(0, _) = [] | take(i, x::xs) = x::take(i-1, xs);  Tail recursion (also called an iterative function) - fun drop (0, xs) = xs | drop (i, _::xs) = drop (i-1, xs);  In tail recursion there is no need to "go up" in the recursion, since there is no computation left to do.  Tail recursion can be implemented more efficiently using loops General tail-call elimination (TCO) : using GOTO.

ML Lists.12 Transforming Normal to Tail Recursion  The built-in length function: - fun length [] = 0 | length (_::xs) = 1 + length xs; val length = fn : 'a list -> int  In order to transform it into an iterative function, we will use a helper variable: - local fun ilen (n, []) = n | ilen (n, _::xs) = ilen (n+1, xs) in fun length xs = ilen (0, xs) end; val length = fn : ‘a list -> int  In general, not necessarily a single variable.  It is called “accumulator”, or “acc”.

ML Lists.13 The Built-in Append Operation  Puts the elements of one list after those of another list  [y1,...,yn] = [x1,...,xm,y1,...,yn] - - fun ys = ys | ys = = fn : ‘a list * ‘a list -> ‘a list  Examples - ["never","boring"]; ["Append","is","never","boring"] : string list - [[2,4,6,8], [[5], [7]]; [[2,4,6,8],[3,9],[5],[7]] : int list list  Is it a tail recursion?  Why can’t it be used in patterns?

ML Lists.14 Side Note: orelse and andalso  They are short-circuit OR and AND boolean operations. B1 andalso B2 if B1 then B2 else false B1 orelse B2 if B1 then true else B2  B2 is evaluated only if needed.  Is the following powoftwo function a tail recursion? - fun even n = (n mod 2 = 0); val even = fn : int -> bool - fun powoftwo n =(n=1) orelse (even n andalso powoftwo (n div 2)); val powoftwo = fn : int -> bool

ML Lists.15 map  Applying a function to all the elements in a list  map f [x1,...,xn] = [f x1,...,f xn] - fun map f [] = [] | map f (x::xs) = (f x) :: (map f xs); val map = fn:('a -> 'b)-> 'a list -> 'b list  Usage: - val sqlist = map (fn x => x*x); val sqlist = fn : int list -> int list - sqlist [1,2,3]; val it = [1,4,9] : int list  Transposing a matrix using map - fun transp ([]::_) = [] | transp rows = (map hd rows) :: transp (map tl rows); val transp = fn: 'a list list -> 'a list list Note: a curried function

ML Lists.16 filter  filter returns all elements satisfying a predicate - fun filter pred [] = [] | filter pred (x::xs) = if pred x then (x:: filter pred xs) else filter pred xs; val filter = fn : ('a -> bool) -> 'a list-> 'a list  Example - filter (fn x => x mod 2 = 0) [1,2,3,4,5]; val it = [2,4] : int list filter is built-in but bound as List.filter (this is also the case for some of the other functions in this slides)

ML Lists.17 Using map and filter  Polynomial is represented as a list of (coeff,degree) pairs 5x 3 + 2x + 7 is represented by val a = [(5,3),(2,1),(7,0)];  Taking the derivative - we need to take each pair (a,n) and convert it to the pair (a*n, n-1). Then we need to remove elements with negative rank or zero coeff. - fun derive p = List.filter (fn (a,n) => n>=0) (map (fn (a,n) => (a*n, n-1)) p); val derive = fn : (int * int) list -> (int * int) list - derive a; val it = [(15,2),(2,0)] : (int * int) list

ML Lists.18 foldl and foldr  foldl f init [x1, x2, …,xn] calculates f(xn, …,f(x2, f(x1,init)))  Possible implementation: - fun foldl f init [] = init | foldl f init (x::xs) = foldl f (f (x, init)) xs; val foldl = fn : (‘a * ‘b -> ‘b) -> ‘b -> ‘a list -> ‘b  foldr f init [x1, x2, …,xn] calculates f(x1, …,f(xn-1, f(xn,init)))  Possible implementation: - fun foldr f init [] = init | foldr f init (x::xs) = f (x, foldr f init xs); val foldr = fn : (‘a * ‘b -> ‘b) -> ‘b -> ‘a list -> ‘b

ML Lists.19 Using foldl and foldr  Summarize list of integers - fun sum list = foldl op+ 0 list; val sum = fn : int list -> int  Reverse a list (alternative for List.rev) - fun reverse list = foldl op:: [] list; val sum = fn : ’a list -> ’a list  Redefine the append operator - fun ys = foldr op:: ys xs; = fn : ’a list * ’a list -> ’a list

ML Lists.20 exists and all  Inside List: List.exists, List.all  Checks if pred is satisfied for an element or the whole list - fun exists p [] = false | exists p (x::xs) = (p x) orelse exists p xs; val exists = fn:('a -> bool)-> 'a list -> bool - fun all p [] = true | all p (x::xs) = (p x) andalso all p xs; val forall = fn:('a -> bool) -> 'a list -> bool  Useful for converting a predicate over type 'a to a predicate over type 'a list - fun disjoint (xs, ys) = all (fn x => all (fn y => x<>y) ys) xs; val disjoint = fn : ''a list * ''a list -> bool

ML Lists.21 Equality Test in Polymorphic Functions  Equality is polymorphic in a restricted sense Defined for values constructed of integers, strings, booleans, chars, tuples, lists and datatypes Not defined for values containing Functions: equality is undecidable (halting problem) reals, because e.g. nan != nan Elements of abstract types  Standard ML has equality type variables ranging over the equality types - op= ; val it = fn : (''a * ''a) -> bool  Somewhat similar to an interface in other languages

ML Lists.22 List of Functions - Example  A list of functions is a perfectly legitimate value - [fn x => 2*x, fn x => 3*x]; val it = [fn,fn] : (int -> int) list - map (fn f => f 3) it; val it = [6,9] : int list  Example from exam - fun upto m n = if m>n then [] else m::(upto (m+1) n); - map upto (upto 1 4); val it = [fn,fn,fn,fn] : (int -> int list) list - map (fn f => f 4) it; val it = [[1,2,3,4],[2,3,4],[3,4],[4]] : int list list

ML Lists.23 שאלה ממבחן (1)

ML Lists.24 שאלה ממבחן (2)

ML Lists.25 ML’s List-Library Functions seen seen RoleSignatureName Checks whether L is empty'a list -> boolnull L Returns the head of L'a list -> 'ahd L Returns the tail of L'a list -> 'a listtl L Returns the first n elements of L'a list * int -> 'a listtake L n Returns L without its first n elements 'a list * int -> 'a listdrop L n Returns the length of L'a list -> intlength L Returns a “glue” of L and M'a list * 'a list -> 'a M Returns L, when each element is mapped by f ('a -> 'b) -> ('a list -> 'b list)map f L Returns the elements of L which satisfy the predicate pred ('a -> bool) -> ('a list -> 'a list)filter pred L Checks if any of the elements of L satisfies the predicate pred ('a -> bool) -> ('a list -> bool)exists pred L Checks if all of the elements of L satisfies the predicate pred )'a -> bool) -> (‘a list -> bool) all pred L *