From be1b432a67d334b7f9fd19735827c1698b085970 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 5 Apr 2020 16:29:46 -0400 Subject: Add (net ricketyspace sicp two forty). --- net/ricketyspace/sicp/one/twentyseven.scm | 6 ++-- net/ricketyspace/sicp/two/forty.scm | 55 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 net/ricketyspace/sicp/two/forty.scm (limited to 'net/ricketyspace') 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 -;;;; . +;;;; 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)) +;;; -- cgit v1.2.3