learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-22.scm (1624B)


      1 (define-library (sicp solutions chapter-1 exercise-22)
      2   ;; Exercise 1.22
      3   (import (scheme base)
      4           (scheme write)
      5 
      6           (sicp utils))
      7 
      8   (begin
      9     (define (run-delay-if-full-flag d)
     10       (if (any (cut equal? "--full" <>)
     11                (command-line))
     12           (force d)))
     13 
     14     (run-delay-if-full-flag
     15      (delay
     16        (let ((port (open-output-file "primes.txt")))
     17 
     18          (define (runtime)
     19            (time-nanosecond (current-time time-process)))
     20 
     21          (define (display x)
     22            (format port "~a" x))
     23 
     24          (define (newline)
     25            (format port "\n"))
     26 
     27          (define (timed-prime-test n)
     28            ;;  (newline)
     29            ;;  (display n)
     30            (start-timed-test n (runtime)))
     31 
     32          (define (start-timed-test n start-time)
     33            (cond
     34             ((prime? n)
     35              (newline)
     36              (display "[")
     37              (display n)
     38              (report-prime (- (runtime)
     39                               start-time)))
     40             (else  #f)))
     41 
     42          (define (report-prime elapsed-time)
     43            (display ", ")
     44            (display elapsed-time)
     45            (display "],")
     46            (newline)
     47            #t)
     48 
     49          (define (search-for-primes start-number end-number)
     50            (cond
     51             ((> start-number end-number) '())
     52             ((timed-prime-test start-number)
     53              (cons start-number
     54                    (search-for-primes (+ start-number 2) end-number)))
     55             (else (search-for-primes (+ start-number 2) end-number))))
     56 
     57          (display "[")
     58 
     59          (search-for-primes 3 100001)
     60 
     61          (display "]")
     62          (newline))))))