This file contains hidden or 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 1.4 | |
;; | |
;; Exercise 1.4. Observe that our model of evaluation allows for | |
;; combinations whose operators are compound expressions. Use this | |
;; observation to describe the behavior of the following procedure: | |
(define (a-plus-abs-b a b) | |
((if (> b 0) + -) a b)) | |
;; (a-plus-abs-b 2 3) |
This file contains hidden or 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 1.5 | |
;; Given: | |
;; | |
;; (define (p) (p)) | |
;; | |
;; (define (test x y) | |
;; (if (= x 0) | |
;; 0 | |
;; y)) |
This file contains hidden or 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 1.5 | |
;; Exercise 1.5. Ben Bitdiddle has invented a test to determine | |
;; whether the interpreter he is faced with is using applicative-order | |
;; evaluation or normal-order evaluation. He defines the following two | |
;; procedures: | |
;; (define (p) (p)) | |
;; | |
;; (define (test x y) |
This file contains hidden or 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 1.7 | |
;; Exercise 1.7. The good-enough? test used in computing square roots | |
;; will not be very effective for finding the square roots of very | |
;; small numbers. Also, in real computers, arithmetic operations are | |
;; almost always performed with limited precision. This makes our test | |
;; inadequate for very large numbers. Explain these statements, with | |
;; examples showing how the test fails for small and large numbers. An | |
;; alternative strategy for implementing good-enough? is to watch how | |
;; guess changes from one iteration to the next and to stop when the |
This file contains hidden or 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
;; Exercise 1.8. Newton's method for cube roots is based on the fact | |
;; that if y is an approximation to the cube root of x, then a better | |
;; approximation is given by the value | |
;; | |
;; Use this formula to implement a cube-root procedure analogous to | |
;; the square-root procedure. (In section 1.3.4 we will see how to | |
;; implement Newton's method in general as an abstraction of these | |
;; square-root and cube-root procedures.) | |
;; ANSWER ------------------------------------------------------------ |
This file contains hidden or 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 C | |
CC = Object.new | |
def self.new | |
CC | |
end | |
end |
This file contains hidden or 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
def f(should_fail=false) | |
puts "BODY 1" | |
fail if should_fail | |
puts "BODY 2" | |
rescue StandardError => ex | |
puts "RESCUE" | |
else | |
puts "ELSE" | |
ensure | |
puts "ENSURE" |
This file contains hidden or 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 'rubygems' | |
require 'ripper' | |
require 'sorcerer' | |
class EvalErr | |
def initialize(str) | |
@string = str | |
end |
This file contains hidden or 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 FL | |
def initialize(array) | |
@ary = array | |
end | |
def ==(array) | |
to_ary == array | |
end | |
def to_ary | |
@ary | |
end |
This file contains hidden or 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 FL | |
def initialize(array) | |
@ary = array | |
end | |
def ==(array) | |
to_ary == array | |
end | |
def to_ary | |
@ary | |
end |