sicp

sicp sandbox.
git clone git://git.ricketyspace.net/sicp.git
Log | Files | Refs

commit be1b432a67d334b7f9fd19735827c1698b085970
parent 7f64b4262ee7c71c6fe2593cd00b5606a14a8155
Author: rsiddharth <s@ricketyspace.net>
Date:   Sun,  5 Apr 2020 16:29:46 -0400

Add (net ricketyspace sicp two forty).

Diffstat:
net/ricketyspace/sicp/one/twentyseven.scm | 6++----
net/ricketyspace/sicp/two/forty.scm | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+), 4 deletions(-)

diff --git 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 @@ -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)) +;;;