Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Create ranged list scheme

Name: Anonymous 2012-02-11 14:04

Does anyone have a faster technique to build a list between a start and stop value? 
I made the straight forward

[code]

(define (mkrng min max)
  (cond
   ((< max min) '())
   (else(cons max (mkrng min (- max 1)))))) [\code]
 
But it is really slow for my application which needs lists of tens of millions of numbers

Name: >>6 2012-02-11 16:16

but here is an attempt at creating a lazy list in scheme...


;; a lazy cell consists of a pair (x . xs)
;; where x is an evaluated value that can be accessed,
;; and xs is a function that takes no arguments, that will generate
;; the next lazy cell.

(define lazy-car car)
(define lazy-cdr (lambda (cell)
                    (if (null? (cdr cell))
                         '()
                         ((cdr cell)))))


(define (lazy-range low high)
  (if (> low high)
      '()
      (cons low
            (lambda ()
              (lazy-range (+ 1 low) high)))))

(define (lazy-for-each operator lazy-lis)
  (if (not (null? lazy-lis))
    (begin (operator (lazy-car lazy-lis))
           (lazy-for-each operator (lazy-cdr lazy-lis)))))


(lazy-for-each (lambda (x) (display x) (newline)) (lazy-range 1 20))


lazy-map and friends, coming soon...

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List