(defmacro (delay x) (list 'lambda () x))

Slides:



Advertisements
Similar presentations
Recursive definitions. Capiche? Less is more Recursive Definitions 1. Specify a function at its lowest/minimum level (zero? One? Empty?) 2. Give a rule.
Advertisements

Programming with Lists
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.
Recursion (define tell-story (lambda () (print ‘’Once upon a time there was a mountain. ‘’) (print ‘’On the mountain, there was a temple. ‘’) (print ‘’In.
מבוא מורחב - שיעור 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).
Chapter 4 Number Theory. Terms Factors Divides, divisible, divisibility Multiple.
מבוא מורחב 1 Lecture #8. מבוא מורחב 2 Shall we have some real fun.. Lets write a procedure scramble.. (scramble (list )) ==> ( ) (scramble.
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 한 태숙.
Streams and Lazy Evaluation in Lisp and Scheme. Overview Examples of using closures Delay and force Macros Different models of expression evaluation –
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.
Streams and Lazy Evaluation A signal processing view of computation CD player DA con- verter ampspeakerdigital signal audio signal amplified audio signal.
CS 603: Programming Language Organization Lecture 10 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
CS 603: Programming Language Organization Lecture 9 Spring 2003 Department of Computer Science University of Alabama Joel Jones.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
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))))))
CS321 Functional Programming 2 © JAS Programming with Streams A stream is never finite and could be defined as special polymorphic type data stream.
CSE 341 Lecture 21 delayed evaluation; thunks; streams slides created by Marty Stepp
Multiplying Integers Objective: To Multiply Integers.
Introduction to Algorithms: Divide-n-Conquer Algorithms
CS314 – Section 5 Recitation 10
CSC 533: Programming Languages Spring 2016
Additional Scheme examples
Conditional Expressions
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Lecture 16 Streams continue Infinite Streams מבוא מורחב - שיעור 16.
6.001 SICP Variations on a Scheme
PPL Lecture Notes: Chapter 3 High-Order Procedures Revisited.
PPL Lazy Lists.
Racket CSC270 Pepper major portions credited to
CLOS CSC 358/
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
COP4020 Programming Languages
CS21b: Structure and Interpretation
Streams and Lazy Evaluation in Lisp and Scheme
Binomial Priority Queues
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
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
PROGRAMMING IN HASKELL
Streams, Delayed Evaluation and a Normal Order Interpreter
Streams and Lazy Evaluation in Lisp and Scheme
Streams and Lazy Evaluation in Lisp and Scheme
6.001 SICP Data abstractions
List and list operations (continue).
Closures and Streams cs7100(Prasad) L11Clos
Lecture # , , , , מבוא מורחב.
Binomial Priority Queues
Streams and Lazy Evaluation in Lisp and Scheme
Streams Contract is the same as pairs...
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
Presentation transcript:

(defmacro (delay x) (list 'lambda () x)) (define (force (promise <function>)) (promise)) ==> (define p (delay (print "hello"))) ==> (force p) hello

(defclass <stream> ()) (defclass <null-stream> (<stream>)) (defclass <cons-stream> (<stream>) (head-stream :accessor head-stream :initarg :head) (tail-stream :type <function> :accessor tail-stream :initarg :tail)) (defmacro (cons-stream o s) (list 'make <cons-stream> :head o :tail (list 'delay s))) (genericify (head o)) (genericify (tail o)) (defmethod (head (s <stream>)) (head-stream s)) (defmethod (tail (s <stream>)) (force (tail-stream s)))

(defmethod (filter (test <function>) (stream <stream>)) (cond ((null-stream? stream) null-stream) ((test (head stream)) (cons-stream (head stream) (filter test (tail stream)))) (else (filter test (tail stream))))) (defmethod (map (f <function>) (stream <stream>)) (if (null-stream? stream) null-stream (cons-stream (f (head stream)) (map f (tail stream))))) (defmethod (accumulate (combiner <function>) (init <object>) (stream <stream>)) init (combiner (head stream) (accumulate combiner init (tail stream)))))

;; (1 1 1 1 1 1 1 1 1 1 ... (define ones (cons-stream 1 ones)) ;; (0 1 2 3 4 5 6 7 8 9 10 ... (define (integers-from (n <integer>)) (cons-stream n (integers-from (add1 n)))) (define naturals (integers-from 0)) ;; (low low+1 ... high) (define (interval low high) (if (> low high) null-stream (cons-stream low (interval (add1 low) high))))

;; print out first n elements of a stream (define (initial (n <integer>) (s <stream>)) (cond ((< n 1) (echo)) ((null-stream? s) (print "[end of stream]")) (else (printf "~a " (head s)) (initial (sub1 n) (tail s))))) ==> (initial 20 ones) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ==> (initial 20 naturals) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ==> (initial 20 (interval 1 10)) 1 2 3 4 5 6 7 8 9 10 [end of stream]

(define (divisible? (x <integer>) (n <integer>)) (zero? (modulo x n))) (define multiples-of-3 (filter (lambda (x) (divisible? x 3)) naturals)) ==> (initial 20 multiples-of-3) 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57

;; sieve of Eratosthenes (define (sieve (stream <stream>)) (cons-stream (head stream) ;head is prime (sieve ;recursively sieve tail after (filter ;removing all multiples of head (lambda (x) (not (divisible? x (head stream)))) (tail stream))))) ;; stream of primes (define primes (sieve (integers-from 2))) ==> (initial 20 primes) 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71

(define (add-streams (a <stream>) (b <stream>)) (cond ((null-stream? a) b) ((null-stream? b) a) (else (cons-stream (+ (head a) (head b)) (add-streams (tail a) (tail b)))))) ;; alternative definition of (0 1 2 3 4 5 ... (define naturals-1 (cons-stream 0 (add-streams ones naturals))) ==> (initial 20 naturals-1) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

;; fibonacci numbers (0 1 1 2 3 5 8 13 ... (define slow-fibs (cons-stream 0 (cons-stream 1 (add-streams slow-fibs (tail slow-fibs))))) ==> (initial 15 slow-fibs) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

;; a more efficient version of fibonacci numbers (define (fibs-from (a <integer>) (b <integer>)) (cons-stream a (fibs-from b (+ a b)))) (define fast-fibs (fibs-from 0 1)) ==> (initial 20 fast-fibs) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Merge two streams into one Example: joint bank account Dexter stream: 1/1/00 23:59 deposit $500 1/15/00 23:59 deposit $500 2/1/00 23:59 deposit $500 2/15/00 23:59 deposit $500 ... Fran stream: 1/3/00 10:36 withdraw $200 1/7/00 14:15 withdraw $150 1/10/00 15:44 withdraw $500 1/19/00 9:05 withdraw $825

;; merge two ordered streams (define (merge (s1 <stream>) (s2 <stream>)) (cond ((null-stream? s1) s2) ((null-stream? s2) s1) ((< (head s1) (head s2)) (cons-stream (head s1) (merge (tail s1) s2))) (else (cons-stream (head s2) (merge s1 (tail s2)))))) ==> (initial 20 (merge naturals naturals)) 0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

A Divergent Computation (define lose (filter odd? (filter even? naturals)))

(define random-bits (cons-stream (random 2) random-bits)) ==> (initial 20 random-bits) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

;; random bit stream (define random-bits (letrec ((promised-random-bits (cons-stream (delay (random 2)) promised-random-bits))) (map force promised-random-bits))) ==> (initial 20 random-bits) 1 0 0 1 0 1 0 0 0 0 1 1 1 0 0 0 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 0 1 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 1 1 1 1 1 0 0 1 0