diff options
Diffstat (limited to 'semestr-2/racket/l9z16')
-rw-r--r-- | semestr-2/racket/l9z16/solution.rkt | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/semestr-2/racket/l9z16/solution.rkt b/semestr-2/racket/l9z16/solution.rkt new file mode 100644 index 0000000..0af169d --- /dev/null +++ b/semestr-2/racket/l9z16/solution.rkt @@ -0,0 +1,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)))
\ No newline at end of file |