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

yc-scheme overflows the javascript stack #27

Open
ldct opened this issue Jan 18, 2013 · 2 comments
Open

yc-scheme overflows the javascript stack #27

ldct opened this issue Jan 18, 2013 · 2 comments
Assignees

Comments

@ldct
Copy link
Owner

ldct commented Jan 18, 2013

evaluating
(integral cube 0 1 0.001)
on 1-3-hop.html on chrome gets
RangeError: Maximum call stack size exceeded, while
(integral cube 0 1 0.01)
works

@ghost ghost assigned yuanchenyang Jan 18, 2013
@yuanchenyang
Copy link
Collaborator

Running this on test_scheme_worker.html works for me, it breaks when dx = 0.0004.

(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))

(define (integral f a b dx)
  (define (add-dx x) (+ x dx))
  (* (sum f (+ a (/ dx 2)) add-dx b)
     dx))

(define (cube x) (* x x x))

(integral cube 0 1 0.01)

(integral cube 0 1 0.001)

This problem can be easily solved by rewriting sum to be tail-recursive:

(define (sum term a next b)
  (define (sum-total term a next b total)
    (if (> a b)
        total
        (sum-total term (next a) next b (+ total (term a)))))
  (sum-total term a next b 0))

In my version of google chrome, I can run ~2360 nested recursions in scheme, but I don't know how to increase that limit =(

@ldct
Copy link
Owner Author

ldct commented Mar 30, 2015

solution: avoid using javascript's call stack

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