Skip to content

Instantly share code, notes, and snippets.

View cayblood's full-sized avatar

Carl Youngblood cayblood

View GitHub Profile
SICP Exercise 1.2
> (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7)))
-37/150
SICP Exercise 1.3
(define (square a) (* a a))
(define (sum-of-squares a b) (+ (square a) (square b)))
(define (sum-of-largest-two-squares a b c)
(cond ((and (< a b) (< a c)) (sum-of-squares b c))
((and (< b a) (< b c)) (sum-of-squares a c))
(else (sum-of-squares a b))))
SICP Exercise 1.4
If b is greater than 0 then add a and b; otherwise subtract b from a.
SICP Exercise 1.1
> 10
10
> (+ 5 3 4)
12
> (- 9 1)
8
> (/ 6 2)
3
@cayblood
cayblood / presentation.rb
Created February 7, 2012 08:12 — forked from jimweirich/presentation.rb
Vital Ruby Lab 3
class Array
def sum
inject(0.0) { |result, el| result + el }
end
def mean
sum / size
end
end
@cayblood
cayblood / x.rb
Created February 7, 2012 11:58 — forked from jimweirich/x.rb
Vital Ruby my_each
class Array
def my_each
length.times do |index|
yield(self[index])
end
end
def my_inject(initial_value)
accumulator = initial_value || self.dup.shift
my_each do |item|
@cayblood
cayblood / Rakefile
Created February 7, 2012 13:54 — forked from jimweirich/presentation.rb
Vital Ruby Lab 4
require 'processor.rb'
task :parse do
p = Processor.new
p.parse
end
task :default => :parse
@cayblood
cayblood / presentation.rb
Created February 8, 2012 08:57 — forked from jimweirich/read_only_proxy.rb
Vital Ruby Lab 5
class Array
def sum
inject(0.0) { |result, el| result + el }
end
def mean
sum / size
end
end
@cayblood
cayblood / attr_flag.rb
Created February 8, 2012 12:06 — forked from jimweirich/attr_flag.rb
Vital Ruby Lab 6
class Module
def attr_flag(*args)
args.each do |sym|
module_eval <<-EOS
attr_writer :#{sym}
def #{sym}?
@#{sym}
end
EOS
end
@cayblood
cayblood / url_fetcher.rb
Created February 9, 2012 10:20 — forked from jimweirich/url_fetcher.rb
Vital Ruby Advanced Lab 1
#!/usr/bin/env ruby
require 'open-uri'
class UrlFetcher
def self.fetch(url)
returnval = nil
begin
open(url) {|stream| returnval = stream.read}
rescue