learning-sicp

My embarrassing half assed SICP run.
Log | Files | Refs

commit 956214616b85e2569fcd69b6ae2a6e7ef0b699e8
parent 7b651346810e7ff013aba39a29c4a2170899f2ca
Author: Yuval Langer <yuval.langer@gmail.com>
Date:   Tue, 21 Nov 2023 21:52:37 +0200

Rewrite chapter-1 exercise-12 (1.12).

Diffstat:
Msicp/solutions/chapter-1/exercise-12.scm | 75+++++++++++++--------------------------------------------------------------
Asicp/tests/chapter-1/exercise-12.scm | 32++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 62 deletions(-)

diff --git a/sicp/solutions/chapter-1/exercise-12.scm b/sicp/solutions/chapter-1/exercise-12.scm @@ -1,62 +1,13 @@ - -#! - -*Exercise 1.12:* The following pattern of numbers is called "Pascal's - triangle". - -1 -1 1 -1 2 1 -1 3 3 1 -1 4 6 4 1 - -The numbers at the edge of the triangle are all 1, and each number -inside the triangle is the sum of the two numbers above it.(4) -Write a procedure that computes elements of Pascal's triangle by -means of a recursive process. - -!# - -(define (pascal line column) - (cond - ((or (= column 0) - (= column line)) 1) - (else (+ (pascal (1- line) - (1- column)) - (pascal (1- line) - column))))) - -(test-begin "1.12") -(test-equal - (list (pascal 0 0) - (pascal 1 0) - (pascal 1 1) - (pascal 2 0) - (pascal 2 1) - (pascal 2 2) - (pascal 3 0) - (pascal 3 1) - (pascal 3 2) - (pascal 3 3) - (pascal 4 0) - (pascal 4 1) - (pascal 4 2) - (pascal 4 3) - (pascal 4 4) - ) - '(1 - 1 - 1 - 1 - 2 - 1 - 1 - 3 - 3 - 1 - 1 - 4 - 6 - 4 - 1)) -(test-end "1.12") +(define-library (sicp solutions chapter-1 exercise-12) + (import (scheme base)) + (export pascal) + + (begin + (define (pascal line column) + (cond + ((or (= column 0) + (= column line)) 1) + (else (+ (pascal (- line 1) + (- column 1)) + (pascal (- line 1) + column))))))) diff --git a/sicp/tests/chapter-1/exercise-12.scm b/sicp/tests/chapter-1/exercise-12.scm @@ -0,0 +1,32 @@ +(define-library (sicp tests chapter-1 exercise-12) + (import (scheme base) + (scheme cxr) + (srfi :64) + (sicp solutions chapter-1 exercise-12)) + + (begin + (test-begin "chapter-1-exercise-12") + (for-each + (lambda (expected-line-column) + (let ((expected (car expected-line-column)) + (line (cadr expected-line-column)) + (column (caddr expected-line-column))) + (test-equal + expected + (pascal line column)))) + '((1 0 0) + (1 1 0) + (1 1 1) + (1 2 0) + (2 2 1) + (1 2 2) + (1 3 0) + (3 3 1) + (3 3 2) + (1 3 3) + (1 4 0) + (4 4 1) + (6 4 2) + (4 4 3) + (1 4 4))) + (test-end "chapter-1-exercise-12")))