Lecture overview -- Keyboard shortcut: 'u'  Previous page: Infinite lists in Scheme: Streams -- Keyboard shortcut: 'p'  Next page: Stream example: The sieve of Eratosthenes -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 5 - Page 24 : 26
Functional Programming in Scheme
The Order of Evaluation
Example streams

Expression

Value

(define ones (cons-stream 1 ones))
(1 . #<promise>)
(stream-section 7 ones)
(1 1 1 1 1 1 1)
(define (integers-starting-from n)
 (cons-stream n 
  (integers-starting-from (+ n 1))))

(define nat-nums
  (integers-starting-from 1))

(stream-section 10 nat-nums)
(1 2 3 4 5 6 7 8 9 10)
(define nat-nums 
 (cons-stream 1 
  (add-streams ones nat-nums)))

(stream-section 10 nat-nums)
(1 2 3 4 5 6 7 8 9 10)
(define fibs
  (cons-stream 0
    (cons-stream 1
      (add-streams (tail fibs) fibs))))

(stream-section 15 fibs)
(0 1 1 2 3 5 8 13 21 34 55 89 144 
 233 377)

Examples of streams. ones is an infinite streams of the element 1. stream-section is a function that returns a finite section of a potentially infinite stream. nat-nums is stream of all the natural numbers, made by use of the recursive function integers-starting-from. The fourth row shows an alternative definition of nat-nums. Finally, fibs is the infinite stream of Fibonacci numbers.

y:/Kurt/Files/courses/prog3/prog3-03/sources/notes/includes/stream.scmThe functions stream-section and add-streams.


y:/Kurt/Files/courses/prog3/prog3-03/sources/notes/includes/stream-stuff.scmAll Scheme stream stuff collected in one file.