cs776 (prasad)l5lists1 programming with lists. cs776 (prasad)l5lists2 lists is a type list list is a...

24
cs776 (Prasad) L5lists 1 Programming with Lists

Upload: jared-ovens

Post on 30-Mar-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 1

Programming with Lists

Page 2: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 2

Lists is a type listlist is a type

(* Homogeneous lists. *)

– E.g., (true, [fn i:int => "i"])

: bool * (int -> string) list.– E.g., [1, 2 , 3], 1::2::3::[] : int list;– E.g., (op ::) : ’a * ’a list ->’a list;

– List constructors [] and :: can be used in patterns.

Page 3: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 3

Built-in operations on lists

hd : ’a list -> ’a tl : ’a list -> ’a list

null: ’a list -> bool

op @ : ’a list * ’a list -> ’a list (* append operation; infix operator *)

length : ’a list -> int (* sets vs lists -- multiplicity; ordering *)

Page 4: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 4

Catalog of List functionsinit [1,2,3] = [1,2]last [1,2,3] = 3

• Specs:init (xs @ [x]) = xslast (xs @ [x]) = x

• Definitions: fun init (x::[]) = [] | init (x::xs) = x :: init xs; fun last (x::[]) = x | last (x::xs) = last xs;

Page 5: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 5

take 3 [1,2,3,4] = [1,2,3] drop 2 [1,2,3] = [3]

• Definition: fun take 0 xs = [] | take n [] = [] | take n (x::xs) = x::take (n-1) xs;

fun drop 0 xs = xs | drop n [] = [] | drop n (x::xs) = drop (n-1) xs;

Page 6: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 6

takewhile even [2,4,1,6,2] = [2,4]dropwhile even [2,3,8] = [3,8]

• Definition: fun takewhile p [] = [] | takewhile p (x::xs) = if p x then x :: takewhile p xs else [];

fun dropwhile p [] = [] | dropwhile p (x::xs) = if p x then dropwhile p xs else x::xs;

Page 7: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 7

• Role of patterns– For testing type (“discrimination”)– For picking sub-expressions apart

• Signatures take, drop : int -> ’a list -> ’a list takewhile, dropwhile : (’a -> bool) -> ’a list -> ’a list

List.take, List.drop : ’a list * int -> ’a list

Page 8: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 8

Selectors #i (a1,…, ai, …, an) = ainth ([a0,…,ai,…,an],i) = ai

• Type of #i cannot be described in ML.

List.nth : ’a list * int -> ’a

fun nth (x::xs, 0) = x | nth (x::xs, i) = nth (xs, i-1) (* Patterns not exhaustive. Exception raised for null list input. *)

Page 9: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 9

fun filter p [] = [] | filter p (x::xs) = if p x then x::filter p xs else filter p xs

filter : (’a -> bool) -> ’a list -> ’a list

Page 10: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 10

fun exists p [] = false | exists p (x::xs) = (p x) orelseorelse (exists p xs) exists : (’a -> bool) -> ’a list -> bool

fun all p [] = true | all p (x::xs) = (p x) andalsoandalso (all p xs)

all : (’a -> bool) -> ’a list -> bool

Page 11: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 11

fun pair [] ys = [] | pair (x::xs) [] = [] | pair (x::xs) (y::ys) = (x,y) :: pair xs ys ;

pair: ’a list -> ’b list ->(’a * ’b) list

exception error;fun zip f (x::xs) (y::ys) = (f x y) :: zip f xs ys | zip f [] [] = [] | zip f xs ys = raise error;

zip : (’a -> ’b -> ’c ) -> ’a list -> ’b list -> ’c list

Page 12: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 12

Module List- open List;

opening List datatype 'a list = :: of 'a * 'a list | nil exception Empty

val null : 'a list -> bool val hd : 'a list -> 'a val tl : 'a list -> 'a list val last : 'a list -> 'a val getItem : 'a list -> ('a * 'a list) option val nth : 'a list * int -> 'a val take : 'a list * int -> 'a list val drop : 'a list * int -> 'a list val length : 'a list -> int val rev : 'a list -> 'a list …

Page 13: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 13

… val @ : 'a list * 'a list -> 'a list val concat : 'a list list -> 'a list val revAppend : 'a list * 'a list -> 'a list val app : ('a -> unit) -> 'a list -> unit val map : ('a -> 'b) -> 'a list -> 'b list val mapPartial : ('a -> 'b option) -> 'a list -> 'b list val find : ('a -> bool) -> 'a list -> 'a option val filter : ('a -> bool) -> 'a list -> 'a list val partition : ('a -> bool) -> 'a list -> 'a list * 'a list val foldr : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b val foldl : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b val exists : ('a -> bool) -> 'a list -> bool val all : ('a -> bool) -> 'a list -> bool val tabulate : int * (int -> 'a) -> 'a list- …

Page 14: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 14

Properties of functions

• Semantic Equivalence– Efficiency Transformations– Formal verification ; Debugging tool

map f (map g x) = map (f o g) x all p (filter p x) = true

(map f) o (filter (p o f)) = (filter p) o (map f)

Page 15: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 15

Modular Designs using Lists

Abstraction and Reuse

Ref: Structure and Interpretation of Computer Programs (Abelson and Sussman)

Page 16: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 16

(define (sum-odd-squares tree) (cond ((null? tree) 0) ((pair? tree) (+ (sum-odd-squares (car tree)) (sum-odd-squares (cdr tree)) ) ) (else (if (odd? tree) (* tree tree) 0))))

• Takes a tree and computes the sum of the squares of the leaves that are odd.

Page 17: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 17

(define (even-fibs n)

(define (next k)

(if (> k n) ’( )

(let ((f (fib k))

(if (even? f)

(cons f (next (+ k 1)))

(next (+ k 1)) )) ))

(next 0))

• Takes a number n and constructs a list of even numbers from among the first n Fibonacci numbers.

Page 18: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 18

Abstract Descriptions

• enumerates the leaves of a tree

• filters them, selecting the odd ones

• squares each of the selected ones

• accumulates the results using +, starting with 0

• enumerates the integers from 0 to n

• computes the Fibonacci number for each integer

• filters them, selecting the even ones

• accumulates the results using cons, starting with ()

Page 19: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 19

(define (filter pred seq)

(cond ((null? seq) ( ))

((pred (car seq))

(cons (car seq) (filter pred (cdr seq))))

(else (filter pred (cdr seq)))

))

(define (accumulate op init seq)

(if (null? seq) init

(op (car seq) (accumulate op init (cdr seq)))

))

Page 20: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 20

(define (enum-interval low high)

(if (> low high) ( )

(cons low (enum-interval (+ low 1) high))

))

(define (enum-tree tree)

(if ((null? tree) ( ))

((pair? tree)

(append (enum-tree (car tree))

(enum-tree (cdr tree)) ))

(else (list tree))))

Page 21: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 21

(define (sum-odd-squares tree)

(accumulate + 0

(map (lambda (x) (* x x))

(filter odd?

(enum-tree tree)))))

(define (even-fibs n)

(accumulate cons nil

(filter even?

(map fib

(enum-interval 0 n)))))

Page 22: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 22

Generality

(define (list-fib-squares n)

(map square (map fib

(enum-interval 0 n) )) )

(define (highest-salary-of-programmer records)

(accumulate max 0

(map salary

(filter programmer? records))))

Page 23: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 23

Inefficiencies

• Find the fifth prime in the interval 100 to 1000(caddddr (filter prime? (enum-interval 100 1000))

• Sum all primes between x and y(define (sum-prime x y)

(accumulate + 0

(filter prime? (enum-interval x y))))

Page 24: Cs776 (Prasad)L5lists1 Programming with Lists. cs776 (Prasad)L5lists2 Lists is a type list list is a type (* Homogeneous lists. *) –E.g., (true, [fn i:int

cs776 (Prasad) L5lists 24

Rewrite

(define (sum-prime x y)

(define (iter count accum)

(if (> count y) accum

(if (prime? count)

(iter (+ 1 count) (+ accum count))

(iter (+ 1 count) accum)

)))

(iter x 0))