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

Pages: 1-

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: Anonymous 2012-02-11 14:05

Lol i didnt read youre post and ur mums a faggg

Name: Anonymous 2012-02-11 14:28

(define (mkrng min max)
  (let loop ((i min)
             (acc '()))
    (if (> i max)
        acc
        (loop (+ i 1) (cons i acc)))))

Name: Anonymous 2012-02-11 15:08

>>3 I dont see why that would be any faster than my current function.

Name: Anonymous 2012-02-11 15:26

enumFromTo min max

Name: Anonymous 2012-02-11 15:39

>>4
It uses tail recursion, so it will use order one stack space. Lazy lists of numbers would be good, except you can't easily do that in scheme.

Name: Anonymous 2012-02-11 16:00

One way is to use delay and force.

(define (make-range x y)
  (if (> x y)
    '()
    (cons x (delay (make-range (+ x 1) y)))))


This will return a pair of x and a "promise" to evaluate (make-range (+ x 1) y) later. You can evaluate it by calling (force (cdr range)), where range is the pair returned by make-range.

Here's an example of how you would use it:

(define (sum-of-range range)
  (if (null? range)
    0
    (+ (car range)
       (sum-of-range (force (cdr range))))))

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

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

>>7
well that is handy! please ignore >>8 op.

Name: Anonymous 2012-02-11 18:16

>>7-9
Ahve you red you're SICP today?

Name: Anonymous 2012-02-11 18:55

>>10
Have you check my doubles today?

Name: Anonymous 2012-02-11 19:41

[\code]
MS separator detected.

Name: Anonymous 2012-02-11 22:40

>>7
You should still use tail recursion when iterating over a lazy list, at least if you know it will be huge like in OP's program.

Name: Anonymous 2012-02-12 0:29

>>10
no, I have not. such is why I must reinvent delayed evaluation using anonymous functions that take no arguments.

Name: Anonymous 2012-02-12 3:46

>>14
fag

Name: Anonymous 2012-02-12 4:32

>>15
Well, you know, that's exactly how some other languages provide lazy evaluation. Have you read your PLRM today?

http://www.adobe.com/products/postscript/pdfs/PLRM.pdf

Name: Anonymous 2012-02-12 5:18

>>16
It's not lazy evaluation in Scheme, although that implementation strategy is used for delay and force.

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