Skip to content

Instantly share code, notes, and snippets.

# Man, I hate having to sort out strings vs symbols in Hashes
class AgnosticHash < Hash
def [](key)
super(coerce_key(key))
end
def []=(key, value)
super(coerce_key(key), value)
end
; Turns out if your Scheme is lazy, you can do this:
(define (Y f)
(f (Y f)))
; I'm not kidding, watch:
(define fact (Y (lambda (rec)
(lambda (x)
(if (= x 0)
; Trying to figure out tail call optimisation for normal-order Scheme
(define (fact x)
(begin
(define (rec y acc)
(cond ((= y 0) acc)
(else (rec (- y 1) (* y acc)))))
(rec x 1)))
(fact 4)
(define (count-change amount)
(cc amount 5))
(define (cc amount coin-type)
(cond ((= amount 0) 1)
((or (< amount 0) (= coin-type 0)) 0)
(else (+ (cc amount
(- coin-type 1))
(cc (- amount
(first-denomination coin-type))
coin-type)))))
# Tail end of Rubinius 'rake' output
# Ubuntu 8.10, Ruby 1.8.7-72, Rubygems 1.3.1
Compiling lib/ruby_parser_extras.rb
Compiling lib/sexp.rb
Compiling lib/sexp_processor.rb
Compiling kernel/delta/compiled_file.rb
/usr/bin/gcc -I/home/jcoglan/projects/rubinius/vm/subtend -I. -I.. -I/usr/include -pipe -Wall -DBASE_PATH="/home/jcoglan/projects/rubinius" -DRBA_PATH="/home/jcoglan/projects/rubinius/runtime" -ggdb3 -O2 -Werror -fPIC -c -o readline.o readline.c
/usr/bin/gcc -lreadline -lncurses -shared readline.o -o readline.so
Created readline.so
jcoglan@fournier:~/projects/heist$ ../rubinius/bin/rbx bin/heist sicp/section-1-1
Exercise 1.2
-0.255555555555556
Exercise 1.3
(sum-squares-largest-two 2 9 5) = 106
(sum-squares-largest-two 4 3 1) = 25
(sum-squares-largest-two 7 3 4) = 65
Testing with MRI, using instance_eval
-------------------------------------
jcoglan@fournier:~/projects/heist$ ruby test/test_heist.rb
Loaded suite test/test_heist
Started
Type of if() function: Heist::Runtime::MetaFunction
Application mode: applicative
; Modifies a variable by applying a function to said variable
(define-syntax ! (syntax-rules ()
[(! x (fn arg ...))
(set! x (fn x arg ...))]
[(! x fn)
(set! x (fn x))]))
; for example...
# In file scope.rb
# Expected output:
# BEFORE: 0, AFTER: 0
# BEFORE: 1, AFTER: 1
# BEFORE: 2, AFTER: 2
# BEFORE: 3, AFTER: 3
# BEFORE: 4, AFTER: 4
# BEFORE: 5, AFTER: 5
# BEFORE: 6, AFTER: 6
((call/cc call/cc) (call/cc call/cc)) ; create k1, pass to call/cc
((call/cc k1) (call/cc call/cc)) ; create k2, pass to k1
((k1 k2) (call/cc call/cc)) ; call (k1 k2), abandon stack
(k2 (call/cc call/cc)) ; fill k1 hole with k2. create k3, pass to call/cc
(k2 (call/cc k3)) ; create k4, pass to k3
(k2 (k3 k4)) ; call (k3 k4), abandon stack
(k3 k4) ; fill k3 hole with k4
(k2 k4) ; call (k2 k4), abandon stack