-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
error #8
Comments
If I paste the code above into a Jupyter notebook cell and run it, there are no errors. There is also no output. |
sorry , i lose the below (define (integers-starting-from n) (define integers ;(display-line (stream-ref integers 0)) (define odd-numbers (display-stream (stream-subseq odd-numbers 50 60)) ;(let ((x (cons-stream 1 (cons-stream 2 '(3))))) (define (stream-add s n) (define (add-streams s1 s2) (define fib (display-stream (stream-subseq fib 150 160)) (define (divisible? x y) (divisible? 10 2) (define (sieve stream) (define primes (display-stream (stream-subseq primes 1000 1010)) |
I haven't run the new example, but I expect you're running into problem called "the top level is hopeless". Here's a smaller example:
If you google "racket the top level is hopeless" you should find some discussions. I'm working on an addition to the IRacket docs that warns about this issue. Meanwhile, the best way to avoid it is to avoid redefinitions --- especially, avoid redefining names imported from modules. You can probably do that here by removing |
thank you for you reply . the problem is Lazy Evaluation .
|
i can run in racket , but not jupyter .
; 如下的这个在racket里会正常,但在jupyter里会造成死机。
(require racket)
;(define (delay exp)
; (lambda () exp))
; ;(memo-proc (lambda ()
; ; exp)))
;
;(define (force delayed-object)
; (delayed-object))
;
;(define (memo-proc proc)
; (let ((already-run? false) (result false))
; (lambda ()
; (if (not already-run?)
; (begin (set! result (proc))
; (set! already-run? true)
; result)
; result))))
(define (delay ex)
(lambda () ex))
(define (force delayed-object)
(delayed-object))
(define (stream-car stream) (car stream))
(define (stream-cdr stream) (force (cdr stream)))
; this won't work as a simple function
;(define (cons-stream a b)
; (cons a (delay b)))
; This is scheme syntax for macro
; http://stackoverflow.com/questions/5610480/scheme-sicp-r5rs-why-is-delay-not-a-special-form
(define-syntax cons-stream
(syntax-rules ()
[(cons-stream x y) (cons x (delay y))]))
(define the-empty-stream '())
(define (stream-null? stream)
(null? stream))
(define (stream-filter pred stream)
(cond ((stream-null? stream) the-empty-stream)
((pred (stream-car stream))
(cons-stream (stream-car stream)
(stream-filter pred (stream-cdr stream))))
(else (stream-filter pred (stream-cdr stream)))))
(define (stream-ref s n)
(if (stream-null? s) the-empty-stream
(if (= n 0)
(stream-car s)
(stream-ref (stream-cdr s) (- n 1)))))
(define (stream-map proc . argstreams)
(if (stream-null? (car argstreams))
the-empty-stream
(cons-stream (apply proc (map stream-car argstreams))
(apply stream-map
(cons proc (map stream-cdr argstreams))))))
(define (stream-for-each proc s)
(if (stream-null? s)
'done
(begin (proc (stream-car s))
(stream-for-each proc (stream-cdr s)))))
; Neil, 2012-05-10
(define (stream-subseq stream a b)
(cond ((stream-null? stream) the-empty-stream)
((= a b) the-empty-stream)
((> a b) the-empty-stream)
(else (cons-stream (stream-ref stream a)
(stream-subseq stream (+ a 1) b)))))
(define (display-line x)
(newline)
(display x))
(define (display-stream s)
(stream-for-each display-line s))
; examples
;(let ((x (delay (+ 1 2))))
; (for ([i (in-range 1 10)])
; (display (force x))))
;
(define (integers-starting-from n)
(cons-stream n (integers-starting-from (+ n 1))))
(define integers
(integers-starting-from 1))
;(display-line (stream-ref integers 0))
(let ((x (stream-subseq integers 10000 10010)))
(display-stream x))
(define odd-numbers
(stream-filter odd? integers))
(display-stream (stream-subseq odd-numbers 50 60))
;(let ((x (cons-stream 1 (cons-stream 2 '(3)))))
; (display-stream x))
(define (stream-add s n)
(stream-map (lambda (x)
(+ x n)) s))
(define (add-streams s1 s2)
(stream-map + s1 s2))
(define fib
(cons-stream 1
(cons-stream 1
(add-streams fib
(stream-cdr fib)))))
(display-stream (stream-subseq fib 150 160))
(define (divisible? x y)
(= (remainder x y) 0))
(divisible? 10 2)
(define (sieve stream)
(cons-stream
(stream-car stream)
(sieve (stream-filter
(lambda (x)
(not (divisible? x (stream-car stream))))
(stream-cdr stream)))))
(define primes
(sieve (integers-starting-from 2)))
(display-stream (stream-subseq primes 1000 1010))
The text was updated successfully, but these errors were encountered: