PPL Lazy Lists.

Slides:



Advertisements
Similar presentations
Programming with Lists
Advertisements

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
Creating Functional Programs Brad Vander Zanden. Basic Techniques Tail Recursion – Use continuation arguments if necessary – Akin to pre-processing a.
מבוא מורחב - שיעור 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.
מבוא מורחב - שיעור 15 1 Lecture 15 Streams. מבוא מורחב - שיעור 15 2 Streams: Motivation (define (sum-primes a b) (define (iter count accum) (cond ((>
מבוא מורחב - שיעור 81 Lecture 8 Lists and list operations (continue).
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
Recursion in Scheme recursion is the basic means for expressing repetition some recursion is on numbers –factorial –fibonacci some recursion is on structures.
Cs776 (Prasad)L15strm1 Reasoning with Functional Programs Optimization by source to source transformation Well-founded induction Streams : Infinite lists.
Closures and Streams More on Evaluations CS784(pm)1.
1 Lecture OO, the notion of inheritance 3 Stacks in OO style (define (make-stack) (let ((top-ptr '())) (define (empty?) (null? top-ptr)) (define.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 13: Streams 한 태숙.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Scheme & Functional Programming. ( ) >> 64 ( ) >> 666 (* ) >> 1200 (+ (* 3 5) (- 10 6)) >> 19.
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.
Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal.
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))))))
Fall 2008Programming Development Techniques 1 Topic 8 Sequences as Conventional Interfaces Section October 2008.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
CS314 – Section 5 Recitation 9
CS314 – Section 5 Recitation 10
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
CS 326 Programming Languages, Concepts and Implementation
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
(defmacro (delay x) (list 'lambda () x))
CSE 341 Lecture 5 efficiency issues; tail recursion; print
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
6.001 SICP Data abstractions
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
Lecture 8: Recursion Practice CS200: Computer Science
Streams Sections 3.5.1,3.5.2 Pages
Lecture 18 Infinite Streams and
PPL Sequence Interface.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Lecture 18.
Lecture #8 מבוא מורחב.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
6.001 SICP Streams – the lazy way
Chapter 4 Data and Behavior Abstraction
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Today’s topics Abstractions Procedural Data
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
More Scheme CS 331.
Streams Contract is the same as pairs...
Streams… …row, row, row your boat….
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

PPL Lazy Lists

Midterm 2012

(define sum-vals (λ (ts) (if (ts-simple (define sum-vals (λ (ts) (if (ts-simple? ts) (ts-val ts) (accumulate + 0 (map ts-val (ts-inner-slots ts))))))

VERY Long (unlimited) Lists (accumulate + 0 (enumerate-list 1 1000000)) We will need to create a (very) large list... If there was only a way not to...

Lazy Lists (Streams) We need a new data type Elements are not pre-computed Can be infinite! Implemented in a way that delays the computation We use lambdas!

Lazy List In normal-order, all lists are lazy In app-order, all lists are not lazy. All following are already evaluated: (cons head tail) (list e1 … en) (append l1 l2) (map p lst)

Lazy List: Constructor (list) – the empty lazy list cons (same as pair and list) [T * [Empty -> Lazy-List(T)] -> Lazy-List(T)]

Simple Lazy List > (define l0 (list)) > (define l1 (cons 1 (lambda () l0))) > (define l2 (cons 2 (lambda () l1))) > l0 ’() > l1 ’(1 . #<procedure>) > ((cdr l1)) > l2 ’(2 . #<procedure>) > ((cdr l2))

Real-World Example The recursion has no base! ;; [Num -> PAIR(Num,[Empty -> Lazy-List]) (define integers-from (lambda (n) (cons n (lambda () (integers-from (add1 n)))))) > (define ints (integers-from 0)) > ints ’(0 . #<procedure>) > ((cdr ints)) ’(1 . #<procedure>) > ((cdr ((cdr ints)))) ’(2 . #<procedure>) The recursion has no base!

Lazy Lists: Head and Tail ;Signature: head(lz-ist) ;Type: [PAIR(T1,[Empty -> Lazy-list]) -> T1 ] (define head car) ;Signature: tail(lz-ist) ;Type: [PAIR(T1,[Empty -> Lazy-list]) -> Lazy-list ] (define tail (lambda (lz-lst) ((cdr lz-lst)))) > (head ints) 0 > (tail ints) (1. #<procedure>) > (head (tail ints)) 1 …

First n Elements (define take (lambda (lz-lst n) (if (= n 0) (list) (cons (car lz-lst) (take (tail lz-lst) (sub1 n)))))) > (take ints 3) ‘(0 1 2) > (take ints 0) ‘() > (take (integers-from 30) 7) ‘(30 31 32 33 34 35 36)

The nth Element >(nth ints 44) 44 (define nth (lambda (lz-lst n) (if (= n 0) (head lz-lst) (nth (tail lz-lst) (sub1 n))))) >(nth ints 44) 44

Lazy List Lots of examples in following slides Tip: always look for the cons

Integer Lazy List (define ones (cons 1 (lambda () ones))) >(take ones 7) ’(1 1 1 1 1 1 1) > (nth ones 10) 1

Factorial Lazy List (define facts-from (lambda (k) (letrec ((helper (lambda (n fact-n) (cons fact-n (lambda () (helper (add1 n) (* (add1 n) fact-n))))))) (helper k (fact k))))) (define facts-from-3 (facts-from 3)) > (take facts-from-3 6) ’(6 24 120 720 5040 40320)

Fibonacci Lazy List (define fibs (letrec ((fibgen (lambda (a b) (cons a (lambda () (fibgen b (+ a b))))))) (fibgen 0 1))) > (take fibs 7) ’(0 1 1 2 3 5 8)

Lazy List Processing If we want to manipulate a lazy-list, we need to construct another lazy-list Examples on next slides

Applying Square on Lazy List (define squares (lambda (lz-lst) (if (empty? lz-lst) lz-lst (cons (let ((h (head lz-lst))) (* h h)) (lambda () (squares (tail lz-lst))))))) > (take (squares ints) 7) ’(0 1 4 9 16 25 36)

Lazy List Add (define lz-lst-add (lambda (lz1 lz2) (cond ((empty? lz1) lz2) ((empty? lz2) lz1) (else (cons (+ (head lz1) (head lz2)) (lambda () (lz-lst-add (tail lz1) (tail lz2))))))))

Defining Integers using Lazy List Addition Reminder: (define ones (cons 1 (lambda () ones))) (define integers (cons 0 (lambda () (lz-lst-add ones integers)))) > (take integers 7) ’(0 1 2 3 4 5 6)

Fibonacci Using Lazy List Addition (define fib-numbers (cons 0 (lambda () 1 (lz-lst-add (tail fib-numbers) fib-numbers)))))) > (take fib-numbers 7) ’(0 1 1 2 3 5 8)

Lazy List Map (define lz-lst-map (λ (f lz) (if (empty? lz) lz (cons (f (head lz)) (λ () (lz-lst-map f (tail lz))))))) > (take (lz-lst-map (lambda (x) (* x x)) ints) 5) ’(0 1 4 9 16)

Lazy List Filter (define lz-lst-filter (λ (p lz) (cond ((empty? lz) lz) ((p (head lz)) (cons (head lz) (λ () (lz-lst-filter p (tail lz))))) (else (lz-lst-filter p (tail lz)))))) (define (divisible? x y) (= (remainder x y) 0)) (define no-sevens (lz-lst-filter (lambda (x) (not (divisible? x 7))) ints)) > (nth no-sevens 100) ;The 100th integer not divisible by 7: 117

Lazy List of Primes (define primes (cons 2 (λ () (lz-lst-filter prime? (integers-from 3)))))