Skip to content

Commit

Permalink
Pretty printing pascal's triangle, sort of
Browse files Browse the repository at this point in the history
  • Loading branch information
dwinston committed Jun 28, 2014
1 parent 9fd4b37 commit e68c2d2
Show file tree
Hide file tree
Showing 6 changed files with 31,125 additions and 2 deletions.
69 changes: 69 additions & 0 deletions cc-graph.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(define (count-change amount)
(cc amount 5))

(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))

(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))

(define (cc-graph amount kinds-of-coins)

(define (display-node label amount kinds-of-coins)
(display " ")
(display label)
(display " [label=\"")
(display `(cc ,amount ,kinds-of-coins))
(display "\"];")
(newline))

(define (display-edge a b)
(display " ")
(display a)
(display " -> ")
(display b)
(display ";")
(newline))

(define (base-case amount kinds-of-coins)
(or (< amount 0)
(= kinds-of-coins 0)
(= amount 0)))

(define (left label)
(string-append label "l"))

(define (right label)
(string-append label "r"))

; label is the unique label of the function invocation, e.g. "sllrl" is
; reached by traveling (from the root) left then left then right then left.
(define (recurse label amount kinds-of-coins)
(cond ((base-case amount kinds-of-coins)
(display-node label amount kinds-of-coins))
(else
(display-node label amount kinds-of-coins)
(display-edge label (left label))
(display-edge label (right label))
(recurse (left label) amount (- kinds-of-coins 1))
(recurse (right label)
(- amount (first-denomination kinds-of-coins))
kinds-of-coins))))

(display "digraph {")
(newline)
(recurse "s" amount kinds-of-coins)
(newline)
(display "}"))

(cc-graph 100 5)
1 change: 1 addition & 0 deletions ex1.10.scm
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@
(define (h n) (A 2 n))

; 2^2^2^2...^2, with (- n 1) 2's in total
; 2^n^n
55 changes: 54 additions & 1 deletion ex1.12.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,64 @@

; assumes row and col are integers >= 0, and that col <= row
(define (pascal row col)
(cond ((= col 0) 1)
(cond ((> col row) (display "bad input"))
((= col 0) 1)
((= col row) 1)
(else (+ (pascal (- row 1) (- col 1))
(pascal (- row 1) col)))))

(define (pascal-triangle row)
(define (pt-helper row from-col)
(cond ((= from-col row) '(1))
(else
(cons (pascal row from-col)
(pt-helper
row
(+ 1 from-col))))))
(pt-helper row 0))

(define (pascal-triangle-until row)
(define (ptu-helper row)
(cond ((< row 0) '())
(else
(cons
(pascal-triangle row)
(ptu-helper (- row 1))))))
(reverse (ptu-helper row)))

(define (display-pt pt-list)
(cond ((null? pt-list) (newline))
(else
(display (indent (length pt-list)))
(pp-list (car pt-list))
(display-pt (cdr pt-list)))))

(define (pp-list lst)
(cond ((null? lst) (newline))
(else
(display
(string-append
(indent
(floor
(/ (- 3
(string-length
(string (car lst))))
2)))
(string (car lst))
(indent
(floor
(/ (- 4
(string-length
(string (car lst))))
2)))))
(pp-list (cdr lst)))))

(define (indent n)
(cond ((= n 0) "")
(else
(string-append " " (indent (- n 1))))))

(display-pt (pascal-triangle-until 8))

1 ]=> (pascal 0 0)

Expand Down
2 changes: 1 addition & 1 deletion ex1.14.scm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(count-change amount)
(define (count-change amount)
(cc amount 5))

(define (cc amount kinds-of-coins)
Expand Down
Loading

0 comments on commit e68c2d2

Please sign in to comment.