5

I think that Lazy Racket should be useful for handling infinite lists. According to the Wikipedia Lazy Racket article, fibs (the infinite list of Fibonacci numbers) can be defined as:

;; An infinite list:
(define fibs (list* 1 1 (map + fibs (cdr fibs))))

How do we define an infinite list of natural numbers?

Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27

2 Answers2

3
#lang lazy

(define nats (cons 1 (map 1+ nats)))

Apparently this doesn't work, and 1+ should be replaced by (lambda (x) (+ x 1)). Thanks to @kenokabe for testing. (add1 is the proper name.)

Will Ness
  • 70,110
  • 9
  • 98
  • 181
3
#lang lazy
(define Nat (cons 1 (map (lambda (x) (+ x 1)) Nat)))

thanks to Will Ness.

I also found

#lang lazy
;;; infinite sequences represented by a_(n+1) = f(a_n)
(define inf-seq (lambda (a0 f) (cons a0 (inf-seq (f a0) f))))
(define Nat (inf-seq 1 (lambda (x) (+ x 1))))

To output

(define (outputListData list)
   (cond 
       [(null? list) #f] ; actually doesn't really matter what we return
       [else 
            (printf "~s\n" (first list)) ; display the first item ...
            (outputListData (rest list))] ; and start over with the rest
    )
)

(outputListData Nat)
  • for comparison, in Haskell it is known as `iterate` (Haskell's got really good names for such functions): `iterate f x = x : iterate f (f x)`. And for "output" it has ``take n [] = [] ; take 0 xs = [] ; take n (x:xs) = x : take (n-1) xs``. (`a:b` in Haskell is like `(a . b)` in Scheme). So `nats = iterate (1+) 1` and we see first 10 of them with `take 10 nats`. Racket too has `take`. – Will Ness Jun 23 '13 at 11:22