summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2020-04-05 16:29:46 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-05 16:29:46 -0400
commitbe1b432a67d334b7f9fd19735827c1698b085970 (patch)
treee10317d964016b1b563f4e76fd8090ef3ef39a2d
parent7f64b4262ee7c71c6fe2593cd00b5606a14a8155 (diff)
Add (net ricketyspace sicp two forty).
-rw-r--r--net/ricketyspace/sicp/one/twentyseven.scm6
-rw-r--r--net/ricketyspace/sicp/two/forty.scm55
2 files changed, 57 insertions, 4 deletions
diff --git a/net/ricketyspace/sicp/one/twentyseven.scm b/net/ricketyspace/sicp/one/twentyseven.scm
index cc50dc9..c0e258f 100644
--- a/net/ricketyspace/sicp/one/twentyseven.scm
+++ b/net/ricketyspace/sicp/one/twentyseven.scm
@@ -1,10 +1,8 @@
-;;;; Under Creative Commons Attribution-ShareAlike 4.0
-;;;; International. See
-;;;; <https://creativecommons.org/licenses/by-sa/4.0/>.
+;;;; License: CC0-1.0
(define-module (net ricketyspace sicp one twentyseven)
#:use-module (srfi srfi-1)
- #:export (carmichael-numbers-fool-fermat-test?))
+ #:export (carmichael-numbers-fool-fermat-test? prime?))
(define carmichael-numbers '(561 1105 1729 2465 2821 6601))
diff --git a/net/ricketyspace/sicp/two/forty.scm b/net/ricketyspace/sicp/two/forty.scm
new file mode 100644
index 0000000..e7c2851
--- /dev/null
+++ b/net/ricketyspace/sicp/two/forty.scm
@@ -0,0 +1,55 @@
+;;;; License: CC0-1.0
+
+(define-module (net ricketyspace sicp two forty)
+ #:use-module (net ricketyspace sicp one twentyseven)
+ #:export (unique-pairs
+ prime-sum-pairs))
+
+
+(define (accumulate op initial sequence)
+ (if (null? sequence)
+ initial
+ (op (car sequence)
+ (accumulate op initial (cdr sequence)))))
+
+
+(define (enumerate-interval low high)
+ (if (> low high)
+ '()
+ (cons low (enumerate-interval (+ low 1) high))))
+
+
+(define (prime-sum? pair)
+ (prime? (+ (car pair) (cadr pair))))
+
+
+(define (make-pair-sum pair)
+ (list (car pair) (cadr pair) (+ (car pair) (cadr pair))))
+
+
+(define (flatmap proc seq)
+ (accumulate append '() (map proc seq)))
+
+
+(define (unique-pairs n)
+ (flatmap (lambda (i)
+ (map (lambda (j) (list i j))
+ (enumerate-interval 1 (- i 1))))
+ (enumerate-interval 1 n)))
+
+
+(define (prime-sum-pairs n)
+ (map make-pair-sum
+ (filter prime-sum?
+ (unique-pairs n))))
+
+
+;;;
+;;; Guile REPL
+;;;
+;;; scheme@(guile-user)> ,re (net ricketyspace sicp two forty)
+;;; scheme@(guile-user)> (unique-pairs 6)
+;;; $8 = ((2 1) (3 1) (3 2) (4 1) (4 2) (4 3) (5 1) (5 2) (5 3) (5 4) (6 1) (6 2) (6 3) (6 4) (6 5))
+;;; scheme@(guile-user)> (prime-sum-pairs 6)
+;;; $9 = ((2 1 3) (3 2 5) (4 1 5) (4 3 7) (5 2 7) (6 1 7) (6 5 11))
+;;;