aboutsummaryrefslogtreecommitdiff
path: root/semestr-2/racket/l9z16
diff options
context:
space:
mode:
authorFranciszek Malinka <franciszek.malinka@gmail.com>2021-10-05 21:49:54 +0200
committerFranciszek Malinka <franciszek.malinka@gmail.com>2021-10-05 21:49:54 +0200
commitc5fcf7179a83ef65c86c6a4a390029149e518649 (patch)
treed29ffc5b86a0d257453cedcf87d91a13d8bf3b0d /semestr-2/racket/l9z16
parentf8a88b6a4aba1f66d04711a9330eaba49a50c463 (diff)
Duzy commit ze smieciami
Diffstat (limited to 'semestr-2/racket/l9z16')
-rw-r--r--semestr-2/racket/l9z16/solution.rkt42
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