sicp

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

commit 2c3ed4ccc0c8d81f8cca28dfb5f2cdf2daf29ddc
parent 4de900d8769fa687787464ba2cf2e050f0dfb399
Author: rsiddharth <s@ricketyspace.net>
Date:   Wed, 14 Sep 2016 02:21:45 +0000

one/twelve.scm: add printing functions

They work only when all values of the triangle are single digit.

Diffstat:
one/twelve.scm | 18+++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/one/twelve.scm b/one/twelve.scm @@ -8,7 +8,9 @@ #:export (compute-triangle compute-line display-line - value-at)) + value-at + print-triangle + print-line)) (define (compute-triangle n) (cond ((= n 1) (list (compute-line n 1))) @@ -28,3 +30,17 @@ (else (+ (value-at (- line 1) (- pos 1)) (value-at (- line 1) pos))))) + +;;;; printing + +(define (print-triangle tl level) + (cond ((= (length tl) 0) #t) + (else + (print-triangle (cdr tl) (+ 1 level)) + (print-line (car tl) level)))) + +(define (print-line l level) + (let* ((l (map number->string l)) + (spaces (string-join (make-list level " ") "")) + (line (string-append spaces (string-join l)))) + (format #t "~a\n" line)))