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
SICP Exercise 1.2 | |
> (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5))))) (* 3 (- 6 2) (- 2 7))) | |
-37/150 |
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
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)))) |
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
SICP Exercise 1.4 | |
If b is greater than 0 then add a and b; otherwise subtract b from a. |
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
SICP Exercise 1.1 | |
> 10 | |
10 | |
> (+ 5 3 4) | |
12 | |
> (- 9 1) | |
8 | |
> (/ 6 2) | |
3 |
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
class Array | |
def sum | |
inject(0.0) { |result, el| result + el } | |
end | |
def mean | |
sum / size | |
end | |
end |
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
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| |
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
require 'processor.rb' | |
task :parse do | |
p = Processor.new | |
p.parse | |
end | |
task :default => :parse |
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
class Array | |
def sum | |
inject(0.0) { |result, el| result + el } | |
end | |
def mean | |
sum / size | |
end | |
end |
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
class Module | |
def attr_flag(*args) | |
args.each do |sym| | |
module_eval <<-EOS | |
attr_writer :#{sym} | |
def #{sym}? | |
@#{sym} | |
end | |
EOS | |
end |
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
#!/usr/bin/env ruby | |
require 'open-uri' | |
class UrlFetcher | |
def self.fetch(url) | |
returnval = nil | |
begin | |
open(url) {|stream| returnval = stream.read} | |
rescue |
OlderNewer