From c5fcf7179a83ef65c86c6a4a390029149e518649 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Tue, 5 Oct 2021 21:49:54 +0200 Subject: Duzy commit ze smieciami --- semestr-2/racket/l9z16/solution.rkt | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 semestr-2/racket/l9z16/solution.rkt (limited to 'semestr-2/racket/l9z16') 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 -- cgit v1.2.3