Skip to content

Instantly share code, notes, and snippets.

@sshine
Created December 30, 2011 14:37
Show Gist options
  • Save sshine/1540128 to your computer and use it in GitHub Desktop.
Save sshine/1540128 to your computer and use it in GitHub Desktop.
My first Scheme program!
(define (tabulate n f)
(define (helper m)
(if (= m n)
()
(cons (f m) (helper (+ m 1)))))
(helper 0))
(define (make-table x y fill)
(define table (make-vector x))
(tabulate x
(lambda (i)
(vector-set! table i (make-vector y fill))))
table)
(define (table-ref table x y)
(vector-ref (vector-ref table x) y))
(define (table-set! table x y obj)
(define column (vector-ref table x))
(vector-set! column y obj)
obj)
(define (memoized-walk-count x y)
(define table (make-table (+ x 1) (+ y 1) #f))
(define (lookup x y)
(define value (table-ref table x y))
(if value
value
(table-set! table x y (walk-count x y))))
(define (walk-count x y)
(if (or (= x 0) (= y 0))
1
(+ (lookup (- x 1) y)
(lookup x (- y 1)))))
(walk-count x y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment