commit 003b86c2d451d4fff198dc983ea584bd2798ced9
parent 95d9109ee298e00aba7940524f076e37bf56c744
Author: Yuval Langer <yuval.langer@gmail.com>
Date: Mon, 27 Mar 2023 17:47:47 +0300
A bit better solution to exercise 2.69.
Diffstat:
3 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/sicp/solutions/exercise-1.21.scm b/sicp/solutions/exercise-1.21.scm
@@ -1,4 +1,4 @@
-(define-library (solutions exercise-1.21)
+(define-library (sicp solutions exercise-1.21)
(import (scheme base))
(import (srfi srfi-1))
(import (srfi srfi-64))
diff --git a/sicp/solutions/exercise-2.40.scm b/sicp/solutions/exercise-2.40.scm
@@ -1,9 +1,11 @@
-(define-library (solutions exercise-2.40)
+(define-library (sicp solutions exercise-2.40)
(import (scheme base))
- (import (srfi :1))
- (import (sicp utils))
- (import (solutions exercise-1.21))
- (export unique-pairs prime-sum-pairs my-prime-sum-pairs)
+ (import (only (sicp utils) filter))
+ (import (only (sicp solutions exercise-1.21) prime?))
+ (export my-prime-sum-pairs
+ prime-sum-pairs
+ remove
+ unique-pairs)
(begin
(define (unique-pairs n)
@@ -13,7 +15,7 @@
(enumerate-interval 1 (- i 1))))
(enumerate-interval 1 n)))
- (define (remove item seqeunce)
+ (define (remove item sequence)
(filter (lambda (x)
(not (equal? x
item)))
@@ -36,7 +38,7 @@
(map (lambda (i)
(map (lambda (j)
(list i j))
- (enumerate 1 (- i 1))))
+ (enumerate-interval 1 (- i 1))))
(enumerate-interval 1 n))))
(define (g n)
diff --git a/sicp/solutions/huffman-codes-stuff.scm b/sicp/solutions/huffman-codes-stuff.scm
@@ -1,7 +1,10 @@
(define-library (sicp solutions huffman-codes-stuff)
(import (scheme base))
+ (import (scheme process-context))
+ (import (scheme write))
(import (scheme cxr))
- (import (sicp utils))
+ (import (only (sicp utils) accumulate))
+ (import (only (sicp solutions exercise-2.40) remove))
(export make-code-tree
make-leaf
sample-message
@@ -146,8 +149,7 @@
(car leaf-set)
leaf-set))
(define leaf-set-without-minimal-weighted-leaf
- (filter (lambda (x) (not (equal? minimal-weighted-leaf
- x)))
+ (remove minimal-weighted-leaf
leaf-set))
(define next-to-minimal-weighted-leaf
(accumulate (lambda (x y) (if (< (weight x)
@@ -160,8 +162,7 @@
(cons (make-code-tree
next-to-minimal-weighted-leaf
minimal-weighted-leaf)
- (filter (lambda (x) (not (equal? next-to-minimal-weighted-leaf
- x)))
+ (remove next-to-minimal-weighted-leaf
leaf-set-without-minimal-weighted-leaf)))
results)