blob: 0af169d37bfbc7aa272d184c82c132854cd116fb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#lang racket
(provide lcons lnull lnull? lcar lcdr)
(define (lcons x f) (mcons x f))
(define lnull null)
(define lnull? null?)
(define (lcar xs) (mcar xs))
(define (lcdr xs)
(let ([x (mcdr xs)])
(cond [(not (mpair? x)) (set-mcdr! xs (x))]))
(mcdr xs))
(define (from n)
(lcons n (lambda () (from (+ n 1)))))
(define nats
(from 0))
(define (lnth n xs)
(cond [(= n 0) (lcar xs)]
[else (lnth (- n 1) (lcdr xs))]))
(define (lfilter p xs)
(cond [(lnull? xs) lnull]
[(p (lcar xs))
(lcons (lcar xs) (lambda () (lfilter p (lcdr xs))))]
[else (lfilter p (lcdr xs))]))
(define (prime? n)
(define (factors i)
(cond [(>= i n) (list n)]
[(= (modulo n i) 0) (cons i (factors (+ i 1)))]
[else (factors (+ i 1))]))
(= (length (factors 1)) 2))
(define primes (lfilter prime? (from 2)))
|