简体   繁体   中英

too much recursion scheme javascript

I'm using a scheme interpreter in javascript

I'm getting the error "too much recursion" when I try this code:

;;;basic things
(define map 
  (lambda (f l) 
    (if (null? l) l 
        (cons (f (car l)) (map f (cdr l))))))

(define filter 
  (lambda (p l)
    (if (null? l) l 
        (if (p (car l))
            (cons (car l) (filter p (cdr l)))
            (filter p (cdr l))))))

(define accumulate 
  (lambda (op initial sequence)
    (if (null? sequence)
        initial
        (op (car sequence)
        (accumulate op initial (cdr sequence))))))

(define reduce 
  (lambda (op sequence)
    (accumulate op (car sequence) (cdr sequence))))

(define append 
  (lambda (l1 l2)
    (if (null? l2) l1
        (append (cons (car l2) l1) (cdr l2)))))

(define make-list 
  (lambda (n f l)
    (if (= n 0) l
        (make-list (- n 1) f (cons (f n) l)))))

(define make-list2-
  (lambda (f n1 n2 l)
    (if (> n1 n2) l
        (make-list2- f (+ n1 1) n2 (cons (f n1) l)))))
(define make-list2 (lambda (f n1 n2) (make-list2- f n1 n2 null)))
(define identity (lambda (x) x))
(define randomitem (lambda (l) (nth (ceil (* (random ) (length l))) l))) 
;;;

(define random0to3 (lambda () (floor (* 4 (random )))))

(define moverandom (lambda (this) (move this (random0to3 ))))

(define searchforcreature 
  (lambda (this cx cy)    ;search only for neighbor tiles
    (begin (define y (make-list2 identity (- cy 5) (+ cy 5)))
         (define L (map (lambda (x) (map (lambda (y2) (cons x y2)) y))
           (make-list2 identity (- cx 5) (+ cx 5))))
        (define L2 (reduce append L))
        (define listoftiles (map (lambda (x) (tile-from-pos (car x) (cdr x))) L2))
        (define listoftiles2 (filter identity listoftiles))    ;remove the falses you get from trying invalid positions
        (define listofcreatures (filter (lambda (x) (and (creature? x) (not (= x this)))) (map topthing listoftiles2)))
        ;(pairtostring listofcreatures)
        (if (null? listofcreatures)
            #f
            (car listofcreatures)))))

(define update 
  (lambda (this world) 
    (begin
      (define target (searchforcreature this (creature-x this) (creature-y this)))
      (begin (print target)
      (if target
          (attack this target)
          (moverandom this))))))

but with 10 instead of 5 in the searchforcreatures function, that means I create a list of 400 items, then map them with the tile-from-pos function but when I tested the same function in drScheme it ran fine, is it because javascript doesn't optimize for recursion?

my game where you can test the above code: thesamesite/textarea.html

the main code for the game, in scheme: thesamesite/env0.sc

Scheme implementations are required to optimize tail calls such that the recursion executes in constant space. Most likely, assuming your code is tail recursive, the Javascript based 'interpreter' isn't doing that optimization. [Actually, likely given that the optimization would be done during compilation and less likely during interpretation.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM