gaucheの遅延ストリームでオーソドックスふるいをつかって。長い。(use util.stream)(define ones (stream-cons 1 ones)) (define (add-streams s1 s2) (stream-map + s1 s2))(define integer (stream-cons 1 (add-streams ones integer)))(define prime-stream (let loop ((primes (stream-cdr integer))) (stream-cons (stream-car primes) (loop (stream-filter (lambda (x) (not (= (remainder x (stream-car primes)) 0))) primes)))))(let loop ((stream prime-stream)) (if (> 100 (stream-car stream)) (cons (stream-car stream) (loop (stream-cdr stream))) '()))
Re:キミならどう書く 2.0 - ROUND 1 -
gaucheの遅延ストリームでオーソドックスふるいをつかって。
長い。
(use util.stream)
(define ones (stream-cons 1 ones))
(define (add-streams s1 s2) (stream-map + s1 s2))
(define integer (stream-cons 1 (add-streams ones integer)))
(define prime-stream
(let loop ((primes (stream-cdr integer)))
(stream-cons (stream-car primes)
(loop
(stream-filter
(lambda (x)
(not (= (remainder x (stream-car primes)) 0)))
primes)))))
(let loop ((stream prime-stream))
(if (> 100 (stream-car stream))
(cons (stream-car stream) (loop (stream-cdr stream)))
'()))