List Processing in SML

Similar documents
List Processing in SML

List Processing in Ocaml

Problem Set 3 Due: 11:59 pm on Thursday, September 25

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Introduc)on To Standard ML

CS 321 Programming Languages

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

CS109A ML Notes for the Week of 1/16/96. Using ML. ML can be used as an interactive language. We. shall use a version running under UNIX, called

Sum-of-Product (SOP) Datatypes in SML

CSE 341 Section 5. Winter 2018

Exercises on ML. Programming Languages. Chanseok Oh

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

CSci 4223 Principles of Programming Languages

Problem Set 2 Solutions

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

CSE341 Autumn 2017, Midterm Examination October 30, 2017

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A quick introduction to SML

CSE341, Fall 2011, Midterm Examination October 31, 2011

SML A F unctional Functional Language Language Lecture 19

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

Functional Programming and Haskell

A list is a finite sequence of elements. Elements may appear more than once

CSE341, Fall 2011, Midterm Examination October 31, 2011

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring

CSE 341 : Programming Languages Midterm, Spring 2015

Chapter 3 Linear Structures: Lists

List Functions, and Higher-Order Functions

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Higher-Order Functions

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

F28PL1 Programming Languages. Lecture 14: Standard ML 4

Chapter 3 Linear Structures: Lists

Handout 2 August 25, 2008

Valex: Mul*ple Value Types, Condi*onals, Dynamic Type Checking and Desugaring SOLUTIONS

Chapter 2 SML, a Functional Programming Language

2.1. Expressions. Chapter 2 SML, a Functional Programming Language. Integers. Interacting with ML

Standard ML. Curried Functions. ML Curried Functions.1

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

CSc 372 Comparative Programming Languages

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

CSE 3302 Programming Languages Lecture 8: Functional Programming

CSE341 Spring 2016, Midterm Examination April 29, 2016

More fun with recursion

Note that pcall can be implemented using futures. That is, instead of. we can use

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Lecture 2: The Basis of SML

Assignment 0. Computer Science 52. Due Friday, September 7, 2018, at 5:00 pm

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

CS 320: Concepts of Programming Languages

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

CSE341 Spring 2016, Midterm Examination April 29, 2016

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Standard prelude. Appendix A. A.1 Classes

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Fall 2018 Lecture N

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Metaprogramming in SML: PostFix and S-expressions

Product types add could take a single argument of the product type int * int fun add(,y):int = +y; > val add = fn : int * int -> int add(3,4); > val i

A Brief Introduction to Standard ML

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

2.1. Expressions. Chapter 2 ML, a Functional Programming Language. Integers. Interacting with ML

CSCE 314 Programming Languages

CSE399: Advanced Programming. Handout 2

Metaprogramming in SML: PostFix and S-expressions

Lists. Michael P. Fourman. February 2, 2010

Functional Programming

Shell CSCE 314 TAMU. Higher Order Functions

02157 Functional Programming Tagged values and Higher-order list functions

Mini-ML. CS 502 Lecture 2 8/28/08

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

The Haskell HOP: Higher-order Programming

Module 04: Lists. Topics: Lists and their methods Mutating lists Abstract list functions Readings: ThinkP 8, 10. CS116 Fall : Lists

An introduction introduction to functional functional programming programming using usin Haskell

Principles of Programming Languages

Patterns The Essence of Functional Programming

All the operations used in the expression are over integers. a takes a pair as argument. (a pair is also a tuple, or more specifically, a 2-tuple)

CS 457/557: Functional Languages

Redefinition of an identifier is OK, but this is redefinition not assignment; Thus

Begin at the beginning

Side note: Tail Recursion. Begin at the beginning. Side note: Tail Recursion. Base Types. Base Type: int. Base Type: int

Tail Recursion: Factorial. Begin at the beginning. How does it execute? Tail recursion. Tail recursive factorial. Tail recursive factorial

Haskell Types, Classes, and Functions, Currying, and Polymorphism

02157 Functional Programming. Michael R. Ha. Tagged values and Higher-order list functions. Michael R. Hansen

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Online Resource. Online introductory book on programming in SML (Standard ML): online.pdf

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Shell CSCE 314 TAMU. Functions continued

Lecture 2: List algorithms using recursion and list comprehensions

Transcription:

List Processing in SML CS251 Programming Languages Spring 2017 Lyn Turbak, Meera Hejmadi, Mary Ruth Ngo, & Angela Wu Department of Computer Science Wellesley College Consing Elements into Lists - val nums = 9 :: 4 :: 7 :: []; val nums = [9,4,7] - 5 :: nums; - nums; (* nums is unchanged *) - (1+2) :: (3*4) :: (5-6) :: []; - [1+2, 3*4, 5-6]; [3,12,~1] - [1=2, 3 < 4, false]; : bool list - ["I", "do", String.substring ("note",0,3), "li" ^ "ke"]; - [(#"a", 8), (#"z", 5)]; [(#"a",8),(#"z",5)] : (char * int) list - [[7,2,5], [6], 9::[3,4]]; [[7,2,5],[6],[9,3,4]] list List Processing in SML 2 SML lists are homogeneous Unlike in Racket & Python, all elements of an SML list must have the same type. - 1 :: [2,3,4]; [1,2,3,4] - op:: (1, [2,3,4]); (* op:: is prefix version of infix :: *) [1,2,3,4] - op:: ; fn : 'a * 'a list -> 'a list - "a" :: [1,2,3]; stdin:1.1-8.3 Error: operator and operand don't agree [literal] operator domain: string * string list operand: string * int list in expression: "a" :: 1 :: 2 :: 3 :: nil -[1,2] :: [3,4,5]; stdin:9.1-9.17 Error: operator and operand don't agree [literal] operator domain * int list list operand: int list * int list in expression: (1 :: 2 :: nil) :: 3 :: 4 :: 5 :: nil List Processing in SML 3 Tuples vs. Lists Tuples are heterogeneous fixed-length product types: - (1+2, 3=4, "foo" ^ "bar", String.sub ("baz", 2)); (3,false,"foobar",#"z") : int * bool * string * char Tuples are homogeneous variable-length product types: - [1, 2+3, 4*5, 6-7, 8 mod 3]; [1,5,20,~1,2] - [1=2, 3<4]; [false,true] : bool list - ["foo", "bar" ^ "baz", String.substring ("abcdefg", 2, 3)]; ["foo","barbaz","cde"] - [#"a", String.sub("baz", 2), chr(100)]; - [#"a",#"z",#"d"] : char list List Processing in SML 4

Some Simple List OperaJons - List.length [7,3,6,1]; 4 : int - List.hd [7,3,6,1]; 7 : int - List.tl [7,3,6,1]; [3,6,1] - List.take ([7,3,6,1],2); [7,3] - List.take ([7,3,6,1],3); [7,3,6] - List.drop ([7,3,6,1],2); [6,1] - List.drop ([7,3,6,1],3); [1] use pagern matching instead - List.nth ([7,3,6,1],0); 7 : int - List.nth ([7,3,6,1],1); 3 : int - List.nth ([7,3,6,1],2); 6 : int - List.null [7,3,6,1]; false : bool - List.null []; true : bool - [7,3,6,1] = []; false : bool - List.rev [7,3,6,1]; [1,6,3,7] (* An API for all SMLNJ List operations can be found at: http://www.standardml.org/basis/list.html *) List Processing in SML 5 Appending Lists - [7,2] @ [8,1,6]; [7,2,8,1,6] - [7,2] @ [8,1,6] @ [9] @ []; [7,2,8,1,6,9] (* Appending is different than consing! *) - [7,2] :: [8,1,6] :: [9] :: []; [[7,2],[8,1,6],[9]] list - op::; (* prefix cons function *) fn : 'a * 'a list -> 'a list - op@; (* prefix append function *) fn : 'a list * 'a list -> 'a list (* List.concat appends all elts in a list of lists *) - List.concat [[7,2],[8,1,6],[9]]; [7,2,8,1,6,9] - List.concat; fn : a list list -> a list List Processing in SML 6 PaLern Matching on Lists (* matchtest -> (int * int) list *) fun matchtest xs = case xs of [] => [] [(a,b)] => [(b,a)] (a,b) :: (c,d) :: zs => (a+c,b*d) :: (c,d) :: zs - matchtest []; - matchtest [(1,2)]; - matchtest [(1,2),(3,4)]; Other PaLern-Matching NotaJons fun matchtest2 xs = case xs of [] => [] [(a,b)] => [(b,a)] (a,b) :: (ys as ((c,d) :: zs)) => (a+c,b*d) :: ys (* subpatterns can be named with as *) fun matchtest3 [] = [] matchtest3 [(a,b)] = [(b,a)] matchtest3 ((a,b) :: (ys as ((c,d) :: zs))) (* parens around pattern necessary above *) = (a+c,b*d) :: ys - matchtest [(1,2),(3,4),(5,6)]; List Processing in SML 7 List Processing in SML 8

List AccumulaJon (* Recursively sum a list of integers *) (* sumlistrec -> int *) fun sumlistrec [] = sumlistrec (x::xs) = - sumlistrec []; 0 : int - sumlistrec [5,2,4]; 11 : int (* Iterative (tail-recursive) summation *) fun sumlistiter xs = let fun loop [] sum = loop (y::ys) sum = in loop xs 0 end Instance of the Mapping Idiom (* inclist -> int list *) fun inclist [] = inclist (x::xs) = - inclist [5,2,4]; [6,3,5] - inclist []; [] - sumlistiter [5,2,4]; 11 : int List Processing in SML 9 List Processing in SML 10 AbstracJng Over the Mapping Idiom (* mymap : ('a -> 'b) -> 'a list -> 'b list *) fun mymap f [] = [] mymap f (x::xs) = (f x)::(mymap f xs) - mymap (fn x => x + 1) [5,2,4]; - mymap (fn y => y * 2) [5,2,4]; - mymap (fn z => z > 3) [5,2,4]; : bool list - mymap (fn a => (a, (a mod 2) = 0)) [5,2,4]; : (int * bool) list Cartesian Products of Lists (* 'a list -> 'b list -> ('a * 'b) list *) fun listprod xs ys = List.concat (List.map xs) - listprod ["a", "b"] [1,2,3]; [("a",1),("a",2),("a",3),("b",1),("b",2),("b",3)] - listprod [1,2,3] ["a", "b"]; [(1,"a"),(1, b"),(2,"a"),(2,"b"),(3, a"),(3,"b")] - mymap (fn s => s ^ "side") ["in", "out", "under"]; - mymap (fn xs => 6::xs) [[7,2],[3],[8,4,5]]; list (* SML/NJ supplies map at top-level and as List.map *) List Processing in SML 11 List Processing in SML 12

Zipping: A Different Kind of List Product (* 'a list * 'b list -> ('a * 'b) list *) - ListPair.zip (["a","b","c"],[1,2,3,4]); [("a",1),("b",2),("c",3)] : (string * int) list (* ('a * 'b) list -> 'a list * 'b list *) - ListPair.unzip [("a",1),("b",2),("c",3)]; (["a","b","c"],[1,2,3]) * int list (* An API for all SMLNJ ListPair operations can be found at: http://www.standardml.org/basis/list-pair.html *) Instance of the Filtering Idiom fun filterpos [] = filterpos (x::xs) = - filterpos [3, ~7, ~6, 8, 5]; [3,8,5] - filterpos []; [] List Processing in SML 13 List Processing in SML 14 AbstracJng over the Filtering Idiom (* myfilter : ('a -> bool) -> 'a list -> 'a list *) fun myfilter pred [] = [] myfilter pred (x::xs) = if (pred x) then x :: (myfilter pred xs) else (myfilter pred xs) - myfilter (fn x => x > 0) [3, ~7, ~6, 8, 5]; - myfilter (fn y => (y mod 2) = 0) [5,2,4,1]; - myfilter (fn s => (String.size s) <= 3) = ["I","do","not","like","green","eggs","and","ham"]; - myfilter (fn xs => (sumlistrec xs > 10)) [[7,2],[3],[8,4,5]]; list Some Other Higher-Order List Ops (* List.partition : ('a -> bool) -> 'a list -> 'a list * 'a list splits a list into two: those elements that satisfy the predicate, and those that don t *) - List.partition (fn x => x > 0) [3, ~7, ~6, 8, 5]; ([3,8,5],[~7,~6]) * int list - List.partition (fn y => (y mod 2) = 0) [5,2,4,1]; ([2,4],[5,1]) * int list (* List.all : ('a -> bool) -> 'a list -> bool returns true iff the predicate is true for all elements in the list. *) - List.all (fn x => x > 0) [5,2,4,1]; true : bool - List.all (fn y => (y mod 2) = 0) [5,2,4,1]; false : bool (* List.exists : ('a -> bool) -> 'a list -> bool returns true iff the predicate is true for at least one element in the list. *) - List.exists (fn y => (y mod 2) = 0) [5,2,4,1]; true : bool (* SML/NJ supplies this function as List.filter *) List Processing in SML 15 - List.exists (fn z => z < 0) [5,2,4,1]; false : bool List Processing in SML 16

foldr : The Mother of All List Recursive FuncJons - List.foldr; fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - List.foldr (fn (x,y) => x + y) 0 [5,2,4]; : int - List.foldr op+ 0 [5,2,4]; : int - List.foldr (fn (x,y) => x * y) 1 [5,2,4]; : int - List.foldr (fn (x,y) => x andalso y) true [true,false,true]; : bool - List.foldr (fn (x,y) => x andalso y) true [true,true,true]; : bool - List.foldr (fn (x,y) => x orelse y) false [true,false,true]; : bool - List.foldr (fn (x,y) => (x > 0) andalso y) true [5,2,4]; : bool - List.foldr (fn (x,y) => (x < 0) orelse y) false [5,2,4]; : bool List Processing in SML 17