From 2c3ed4ccc0c8d81f8cca28dfb5f2cdf2daf29ddc Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Wed, 14 Sep 2016 02:21:45 +0000 Subject: one/twelve.scm: add printing functions They work only when all values of the triangle are single digit. --- one/twelve.scm | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/one/twelve.scm b/one/twelve.scm index 39c3d8b..1e201dc 100644 --- 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))) -- cgit v1.2.3