Created
December 31, 2014 04:07
-
-
Save dag10/a37589adb9c6de2319f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (pascal row col) | |
(cond ((< row 2) 1) | |
((= col 0) 1) | |
((= row col) 1) | |
(else (+ (pascal (- row 1) col) | |
(pascal (- row 1) (- col 1)))))) | |
(define (pascal-row row) | |
(define (pascal->string row col) | |
(number->string (pascal row col))) | |
(define (col-iter base-str start-col) | |
(if (= start-col row) | |
(string-append base-str | |
(pascal->string row start-col)) | |
(col-iter (string-append base-str | |
(pascal->string row start-col) | |
" ") | |
(+ start-col 1)))) | |
(col-iter "" 0)) | |
(define (center-string string size) | |
(define num-spaces (floor (/ (- size (string-length string)) 2))) | |
(string-append (make-string num-spaces) | |
string)) | |
(define (pascal-triangle centered rows) | |
(define cols (string-length (pascal-row (- rows 1)))) | |
(define (pascal-row-centered row) | |
(center-string (pascal-row row) cols)) | |
(define pascal-row-func (if centered pascal-row-centered pascal-row)) | |
(define (pt-iter base-str btm-row) | |
(if (= btm-row 0) | |
(string-append (pascal-row-func btm-row) | |
base-str) | |
(pt-iter (string-append "\n" | |
(pascal-row-func btm-row) | |
base-str) | |
(- btm-row 1)))) | |
(pt-iter "" (- rows 1))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment