Programming with Lists

Slides:



Advertisements
Similar presentations
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. 2 List Utilities Scheme built-in procedures –(list x y z...) –(list-ref lst index) –(length lst) –(append.
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node 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]
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.
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.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
מבוא מורחב - שיעור 91 Lecture 9 Lists continued: Map, Filter, Accumulate, Lists as interfaces.
Cs7100(Prasad)L11Clos1 Closures and Streams. Contemporary Interest in Closures The concept of closures was developed in the 1960s and was first fully.
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.
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Crash course on SML Wojciech Moczydłowski SML – functional programming language Complete formal semantics Extremely convenient for writing compilers, interpreters,
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
Cse321, Programming Languages and Compilers 1 7/14/2015 Lecture #3b, Jan. 22, 2007 References While Loops Accumulating parameter functions Regular Expressions.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
CSE-321 Programming Languages Introduction to Functional Programming (Part II) POSTECH March 13, 2006 박성우.
1 Append: process  (append list1 list2) (cons 1 (append ‘(2) list2)) (cons 1 (cons 2 (append ‘() list2))) (cons 1 (cons 2 list2)) (define (append list1.
Practice session #6: 1. Sequence operations 2. Partial evaluation with currying 3. Lazy-lists.
Today’s topic: Abstraction Compound Data Data Abstractions: Isolate use of data abstraction from details of implementation Relationship between data abstraction.
Chapter 9: Functional Programming in a Typed Language.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
PPL Lazy Lists. Midterm 2012 (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
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,
Recursion Higher Order Functions CSCE 314 Spring 2016.
© M. Winter COSC 4P41 – Functional Programming Some functions id :: a -> a id x = x const :: a -> b -> a const k _ = k ($) :: (a -> b) -> a -> b.
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
6.001 SICP Data abstractions
Streams Sections 3.5.1,3.5.2 Pages
PROGRAMMING IN HASKELL
Lecture 18 Infinite Streams and
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
Abstracting Repetitions
PPL Sequence Interface.
PROGRAMMING IN HASKELL
Lecture 18.
Lecture #8 מבוא מורחב.
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
CS 457/557: Functional Languages Folds
CSE-321 Programming Languages Introduction to Functional Programming
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
CSE-321 Programming Languages Introduction to Functional Programming
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
list data list 만들기 list 사용하기 nil : list link :  * list -> list
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

Programming with Lists cs776 (Prasad) L5lists

Lists E.g., (true, [fn i:int => "i"]) a is a type ----------- a list 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. Type constructor. Infix to prefix operator. Functions => constructors. bool * (int -> string) list = bool * ( (int -> string) list ) cs776 (Prasad) L5lists

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 *) cs776 (Prasad) L5lists

Catalog of List functions init [1,2,3] = [1,2] last [1,2,3] = 3 Specs: init (xs @ [x]) = xs last (xs @ [x]) = x Definitions: fun init (x::[]) = [] | init (x::xs) = x :: init xs; fun last (x::[]) = x | last (x::xs) = last xs; Illustrates recursive definitions. Spec describes the intent and is a consequence of the definition. Declarative specs vs Constructive definitions. Readability and correctness considerations for using list functions. Init and last are complementary to car-cdr. Non-exhaustive pattern generates warnings about “incompleteness”. Overlapping patterns are allowed because the rules are ordered. cs776 (Prasad) L5lists

Definition: fun take 0 xs = [] 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; Takes or drops an initial segment. Completeness and pattern matching. Signature enables discarding non-list arguments by the type checker. (Cf. Prolog where the corresponding first rule might succeed with args (0,anything)) Ensure completeness for list patterns. In general, the requirement that the first argument must be non-negative integer can be expressed either using an user-defined exception or by letting ML system catch it when the negative value creates trouble (such as inifinite loop). In this specific case, as the computation bottoms out for the empty list. Exception is the appropriate mechanism because n < 0 can be thought of as pre-condition violation by the caller. cs776 (Prasad) L5lists

Definition: fun takewhile p [] = [] 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) = then dropwhile p xs else x::xs; Takes or drops an initial segment conditionally. Conditional expression: Observe that both THEN and ELSE clauses necessary. Also for conditional expression to be well-typed, both parts must have the same type or we can determine their least-upper-bound. Higher-order functions. The use of (p x) in the condition enables type inference engine to determine that p is a predicate. cs776 (Prasad) L5lists

Role of patterns Signatures 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 Alternative to separate null?, car, cdr, etc in LISP/Scheme. List.take and List.drop are SML built-ins with the given signature. cs776 (Prasad) L5lists

Selectors #i (a1,…, ai, …, an) = ai nth ([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. *) Cartesian product – fixed arity. Non-exhaustive patterns cause ML Warnings. If you ignore, you are implicitly stating a precondition. ML is robust, so it will catch any violations downstream. To make this code robust, we need to add a user-defined exception for the empty-list case or negative args. cs776 (Prasad) L5lists

filter : (’a -> bool) -> ’a list -> ’a list 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 Scans the entire list as opposed to the initial segment. cs776 (Prasad) L5lists

(p x) orelse (exists p xs) fun exists p [] = false | exists p (x::xs) = (p x) orelse (exists p xs) exists : (’a -> bool) -> ’a list -> bool fun all p [] = true | all p (x::xs) = (p x) andalso (all p xs) all : (’a -> bool) -> ’a list -> bool Note the initialization for the empty-list case. Identity wrt boolean op. cs776 (Prasad) L5lists

| pair (x::xs) (y::ys) = (x,y) :: pair xs ys ; 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 Pair accepts lists of any length and uses the initial segment of the longer list that has the same length as the shorter list. Pairs corresponding elements in the list (cf. cartesian product) Zip generalizes pair and expects the two lists to be of the same length. If the input does not satisfy this, exception is raised at run-time. Resembles map for binary functions. Notice the generality captured by the signature. Even though patterns can be overlapping, ML orders clauses from top to bottom, thereby giving preference to earlier definition. The ordering of clauses in zip contributes to efficiency because non-empty lists are more likely inputs than the empty cases. cs776 (Prasad) L5lists

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 … cs776 (Prasad) L5lists

val concat : 'a list list -> 'a list … 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 - … cs776 (Prasad) L5lists

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) John Backus’s Turing Award Lecture : Define Algebra of programs (identities) -- Suitable for formal manipulation and reasoning about programs. HOF improve Readability through modular construction of programs. Efficiency obtained thro automatic translation. Reliability/Correctness through formal equational reasoning. Code naturally. Pattern directed optimization by orienting identities to improve space/time requirements. cs776 (Prasad) L5lists

Modular Designs using Lists Abstraction and Reuse Ref: Structure and Interpretation of Computer Programs (Abelson and Sussman) Meyer’s Criteria for Modular Design Methodology 1. Decomposability (independence) 2. Composability (reuse) 3. Continuity (independence) 4. Understandability 5. Protection cs776 (Prasad) L5lists

(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. Retained Scheme syntax because of heterogeneity. cs776 (Prasad) L5lists

(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. cs776 (Prasad) L5lists

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 () Instead of using two dissimilar customized recursive definitions for the two problems, understand their similarities, and exploit it by defining and reusing common primitives. cs776 (Prasad) L5lists

(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))) Typically, init and op in accumulate are connected. init op x = x op init = x cs776 (Prasad) L5lists

(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)))) cs776 (Prasad) L5lists

(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))))) In even-fibs, the final accumulate is unnecessary. cs776 (Prasad) L5lists

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)))) Modular – Reuse (applied to different context) Combine reusable blocks a different way and then for a different problem. Explicitly write only what is application specific. cs776 (Prasad) L5lists

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)))) Large list constructed even though a small initial segment is needed. Non-incremental (see alternative in the next overhead). A lot of work into how to take such definitions and execute in an interleaved fashion for efficiency. (cf. Prolog) cs776 (Prasad) L5lists

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)) Does not construct the large list at one time. Incrementally checks each element instead. (Tail recursion) cs776 (Prasad) L5lists