Skip to content
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

Open
kerwinxu opened this issue Aug 9, 2020 · 4 comments
Open

error #8

kerwinxu opened this issue Aug 9, 2020 · 4 comments

Comments

@kerwinxu
Copy link

kerwinxu commented Aug 9, 2020

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))

@kerwinxu kerwinxu closed this as completed Aug 9, 2020
@kerwinxu kerwinxu reopened this Aug 9, 2020
@rmculpepper
Copy link
Owner

If I paste the code above into a Jupyter notebook cell and run it, there are no errors. There is also no output.

@kerwinxu
Copy link
Author

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)
(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))

@rmculpepper
Copy link
Owner

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:

(require racket)
;; range : Real Real -> Real
;; compute the length of an interval
(define (range a b) (if (<= a b) (- b a) (range b a)))
(range 5 10) ;; => 5
(range 10 5) ;; => '(5 6 7 8 9)

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 (require racket) and requiring the specific modules that you need.

@kerwinxu
Copy link
Author

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:

(require racket)
;; range : Real Real -> Real
;; compute the length of an interval
(define (range a b) (if (<= a b) (- b a) (range b a)))
(range 5 10) ;; => 5
(range 10 5) ;; => '(5 6 7 8 9)

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 (require racket) and requiring the specific modules that you need.

thank you for you reply .

the problem is Lazy Evaluation .

  • racket : supported by like syntax-rules () , i can run my example work well .
  • iracket : does not supported , i do not know why, the result is, endless loop .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants