Posts Tagged ‘sicp’
Monday, May 9th, 2011
From left to right:
1 2 3 4 5 6
| (define (list-of-values exps env)
(if (no-operands? exps)
nil
(let ((first (eval (first-operand exps) env)))
(cons first
(list-of-values (rest-operands exps) env))))) |
From right to left:
1 2 3 4 5 6
| (define (list-of-values exps env)
(if (no-operands? exps)
nil
(let ((rest (list-of-values (rest-operands exps) env)))
(cons (eval (first-operand exps) env)
rest)))) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »
Monday, May 9th, 2011
1 2 3 4 5 6 7 8 9
| (define (estimate-integral P x1 x2 y1 y2)
(define experiment-stream
(define test
(lambda () (P (random-in-range (min x1 x2) (max x1 x2))
(random-in-range (min y1 y2) (max y1 y2)))))
(cons-stream test experiment-stream))
(let ((area (* (abs (- x2 x1)) (abs (- y2 y1)))))
(stream-map (lambda (p) (* area p))
(monte-carlo experiment-stream 0 0)))) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »
Monday, May 9th, 2011
Not sure if this works. Cannot test without rand-update:
1 2 3 4 5 6 7 8 9 10 11
| (define (rand random-stream)
(define (random-numbers initial-value)
(cons-stream initial-value
(stream-map rand-update random-numbers)))
(define (dispatch op)
(cond ((eq? op 'generate) (random-numbers random-init))
((eq? op 'reset)
(lambda (new-value)
(random-numbers (stream-car (stream-filter (lambda (n) (= n new-value)) (random-numbers random-init))))))
(else (error "Operation not found -- RAND" op))))
dispatch) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »
Monday, May 9th, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| (define (RLC R L C dt)
(lambda (vc0 il0)
(cons (vc-stream vc0 C dt)
(il-stream il0 R L dt))))
(define (vc-stream vc0 C dt)
(define vc (integral (delay dvc) vc0 dt))
(define dvc (scale-stream (delay il-stream) (/ -1.0 C)))
vc)
(define (il-stream il0 R L dt)
(define il (integral (delay dil) il0 dt))
(define dil (add-streams (scale-stream vc-stream (/ 1.0 L))
(scale-stream il (* (- R) (/ 1.0 L)))))
il) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »
Monday, May 9th, 2011
1 2 3 4 5
| (define (solve-2nd f dt y0 dy0)
(define y (integral (delay dy) y0 dt))
(define dy (integral (delay ddy) dy0 dt))
(define ddy (stream-map f y))
y) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »
Monday, May 9th, 2011
1 2 3 4 5 6
| (define (solve-2nd a b dt y0 dy0)
(define y (integral (delay dy) y0 dt))
(define dy (integral (delay ddy) dy0 dt))
(define ddy (add-stream (scale-stream dy a)
(scale-stream y b)))
y) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »
Monday, May 9th, 2011
1 2 3 4 5 6 7 8 9
| (define (integral delayed-integrand initial-value dt)
(cons-stream initial-value
(let ((integral (force delayed-integral)))
(if (stream-null? integrand)
the-empty-stream
(integral (delay (stream-cdr integrand))
(+ (* dt (stream-car integrand))
initial-value)
dt))))) |
Tags:scheme, sicp
Posted in Computer Science | No Comments »