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.

Slides:



Advertisements
Similar presentations
There is a pattern for factoring trinomials of this form, when c
Advertisements

By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Formal Models of Computation Part II The Logic Model
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Higher-order functions in ML
Data Structures ADT List
Programming with Lists
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]
Squares and Square Root WALK. Solve each problem REVIEW:
A First Look at ML.
A Fourth Look At ML 1. Type Definitions Predefined, but not primitive in ML: Type constructor for lists: Defined for ML in ML datatype bool = true | false;
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Chapter 5 Test Review Sections 5-1 through 5-4.
Addition 1’s to 20.
25 seconds left…...
Week 1.
We will resume in: 25 Minutes.
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.
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.
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 =
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.
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 A Quasi-Functional Language With Strong Typing And Type Inference.
Introduction to ML A Quasi-Functional Language With Strong Typing And Type Inference.
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.
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,
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
A lightening tour in 45 minutes
CS 270 Math Foundations of CS Jeremy Johnson
PROGRAMMING IN HASKELL
CSE 341 Lecture 3 let expressions; pattern matching Ullman
Higher Order Functions
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Abstracting Repetitions
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
Functions, Patterns and Datatypes
Higher Order Functions
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
CSE 3302 Programming Languages
Functions, Patterns and Datatypes
Functions, Patterns and Datatypes
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" ] []  Elements may appear more than once [3,4] [4,3] [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 can be constructed by joining its first element and its tail (which is a list itself)  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 []  The operator :: (also called cons) makes a list by putting an element in front of an existing 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 infix operator :: groups to the right.  The notation [x1,x2,...,xn] stands for x1 :: x2 ::... :: xn :: nil 3::(5::(9::nil)) is [3,5,9]  Clarification: You can build a list either with nil and op:: (the list constructors) or using the brackets notation ([]) as a shortcut.

ML Lists.5 Built-in Fundamental Functions  Testing lists and taking them apart null - tests whether a list is empty - fun null [] = true | null (_::_) = false; val null = fn : ‘a list -> bool hd - returns the head of a non-empty list - fun hd (x::_) = x; **Warning: Patterns not exhaustive val hd = fn : ‘a list -> ‘a tl - returns 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]  The implementation: - fun upto (m,n) = if m>n then [] else m :: upto(m+1,n); val upto = fn : int * int -> int list - upto (2,5); val it = [2,3,4,5] : int list.

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

ML Lists.9 The Computation of take take([9,8,7,6],3) => 9::take([8,7,6],2) => 9::(8::take([7,6],1)) => 9::(8::(7::take([6],0))) => 9::(8::(7::[])) => 9::(8::[7]) => 9::[8,7] => [9,8,7]

ML Lists.10 Tail recursion  Normal recursion - fun prod [] = 1 | prod (n::ns) = n * (prod ns); val prod = fn : int list -> int  Tail recursion (also called an iterative function) - fun maxl [m] : int = m | maxl (m::n::ns) = if m>n then maxl(m::ns) else maxl(n::ns); val maxl = fn : int list -> int  In tail recursion there is no need to "go up" in the recursion.  Tail recursion can be implemented more efficiently, e.g., as a loop.

ML Lists.11 Transforming Normal to Tail Recursion  Transforming prod into an iterative function - local fun iprod([],accumulator) = accumulator | iprod(x::xs,accumulator) = iprod(xs, x * accumulator); in fun prod(ls) = iprod(ls,1); end; val prod = fn : int list -> int  In general, not necessarily a single variable.

ML Lists.12 The Built-in length Function  recursive solution: - fun length [] = 0 | length (_::xs) = 1 + length xs; val length = fn : 'a list -> int  iterative solution: - local fun ilen (n,[]) = n | ilen (n,_::xs) = ilen (n+1,xs) in fun length ls = ilen (0,ls) end; val length = fn : ‘a list -> int - length(explode("ML is neat")); val it = 10 : int

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

ML Lists.14 The Computation of Append => => => => 2::(4::(6::[8,10])) => 2::(4::[6,8,10]) => 2::[4,6,8,10] => [2,4,6,8,10]

ML Lists.15 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  Meaning the second boolean 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.16 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 - 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.17 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 - List.filter (fn x => x mod 2 = 0) [1,2,3,4,5]; val it = [2,4] : int list filter is built-in but bounded as List.filter (this is also the case for some of the other functions in this slides)

ML Lists.18 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) = filter (fn (a,n) => a<>0 andalso 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.19 foldl and foldr  foldl f init [x1, x2, …,xn] calculates f(xn, …,f(x1,init)..) - 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,init)..) - 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.20 Using foldl and foldr  Summarize list of integers - fun sum list = foldl op+ 0 list; val sum = fn : int list -> int  Reverse a list - 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.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, reals or elements of abstract types  Standard ML has equality type variables ranging over the equality types - op= ; val it = fn : (''a * ''a) -> bool

ML Lists.22 exists and forall  Checks if pred is satisfied for an element or the whole list - fun exists pred [] = false | exists pred (x::xs) = (pred x) orelse exists pred xs; val exists = fn:('a ->bool)-> 'a list->bool - fun forall pred [] = true | forall pred (x::xs) = (pred x) andalso forall pred 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) = forall (fn x => forall (fn y => x<>y) ys) xs; val disjoint = fn : ''a list * ''a list -> bool

ML Lists.23 List of Functions - Example  A list of functions is perfectly legitimate - [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.24 שאלה ממבחן

ML Lists.25 שאלה ממבחן