Week13 - LAB = 1 The result of(delay is of type*promise The result of(force(delay is of whatever type the would be without delay in this case

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Courier New {\*\generator Msftedit 5.41.15.1507 Err:510 \par 1. The result of (delay . ..) is of type *promise*.\par The result of (force (delay . ..)) is of whatever type the ". .." would\par be without delay type integer.\par \par \par 2. (stream-cdr (stream-cdr (cons-stream 1 '(2 3)))) causes an error because\par (2 3) isn't a stream.\par \par (stream-cdr (cons-stream 1 '(2 3))) --> (2 3)\par (stream-cdr '(2 3)) --> (force (cdr '(2 3)))\par --> (force '(3))\par but (3) isn't a promise!\par \par \par 3. (delay (enumerate-interval 1 3)) returns a promise.\par (stream-enumerate-interval 1 3) returns a stream, i.e., a pair whose car\par is 1 and whose cdr is a promise.\par \par The only thing you can do with the result of (delay . ..) is FORCE it can't ask for its stream-car or stream-cdr, etc. By contrast, with the\par stream, you can stream-car or stream-cdr it, but *not* force it, because\par it's not a promise.\par \par \par 4. the 4-2-1 stream.\par \par (define (next-num n)\par (if (even? n)\par (/ n 2)\par (+ (* n 3) 1)))\par \par (define (num-seq n)\par (cons-stream n (num-seq (next-num n))))\par \par (define (seq-length s)\par (if (= (stream-car s) 1)\par 1\par (+ 1 (seq-length (stream-cdr s)))))\par \par \par \par HOMEWORK\par Err:510 3.50 stream-map\par \par (define (stream-map proc . argstreams)\par (if (STREAM-NULL? (car argstreams))\par
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sheet1 Page 2 the-empty-stream\par (CONS-STREAM\par (apply proc (map STREAM-CAR argstreams))\par (apply stream-map\par (cons proc (map STREAM-CDR argstreams))))))\par \par 3.51 stream-ref\par \par > (stream-ref x 5)\par \par 1\par 2\par 3\par 4\par 5\par 5\par \par > (stream-ref x 7)\par \par 6\par 7\par 7\par \par >\par \par The repeated last number in each case is the actual value returned by\par stream-ref shown are the ones we haven't already computed. If we ask for a value\par nearer the front of the stream, nothing is SHOWn at all:\par \par > (stream-ref x 4)\par \par 4\par \par >\par \par Notice that the first element of the stream is zero, not one -- why\par isn't the zero printed? Answer: It was printed when you defined X\par in the first place.\par \par NOTE: The important thing to take away from this example is that if\par DELAY didn't involve memoization, the printed results would be quite\par different. All the numbers would be printed every time.\par \par \par 3.52 accum/filter\par \par To make the situation more visible, I've chosen to define accum to use\par show (as above), so we can see intermediate results:\par \par > (define sum 0)\par SUM\par
Background image of page 2
Sheet1 Page 3 \par > (define (accum x)\par (set! sum (+ (show x) sum))\par sum)\par ACCUM\par \par > (define seq (stream-map accum (stream-enumerate-interval 1 20)))\par 1\par SEQ\par \par \par > sum\par 1\par \par > (define y (stream-filter even? seq))\par 2\par 3\par Y\par \par \par > sum\par 6\par \par > (define z (stream-filter (lambda (x) (= (remainder x 5) 0)) seq))\par
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This homework help was uploaded on 04/09/2008 for the course CS 61A taught by Professor Harvey during the Spring '08 term at University of California, Berkeley.

Page1 / 42

Week13 - LAB = 1 The result of(delay is of type*promise The result of(force(delay is of whatever type the would be without delay in this case

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online