Hey,
In SICP 1.2.1 there is a function that makes a rational number, as follow:
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
I'm just curious how you can implement the same thing using lambda instead of let, without calling GCD twice. I couldn't figure it out myself.
...
In SICP exercise 2.26, this Scheme code is given:
(define x (list 1 2 3))
(define y (list 4 5 6))
Then this cons call is given:
(cons x y)
I expected a pair of lists would result, ((1 2 3) (4 5 6)) but the interpreter gives,
((1 2 3) 4 5 6)
...a list with 4 elements, the first being a list. Why is y treated differently? I've tried...
What the heck is the Structure and Interpretation of Computer Programs cover drawing about? I mean I know what "eval", "apply", and 'λ' all mean, but I'm having a hard time deciphering the rest of the picture.
Who the heck is the maiden? Does she work for the wizard? Why the heck is she pointing at the table? Is she pointing at that...
I've been slowly working my way though the exercises in Structure and Interpretation of Computer Programs. Section 1.1.5 talks about applicative vs. normal-order evaluation, and the topic has come up several times in the text afterward. Since the interpreter uses applicative-order evaluation, it's easy to just insert display debug stat...
So I just read The Little Schemer and found it really good! I had no prior functional programming background, apart from the "infamous" parenthesis myth I so much heard about Lisp :P
I found it an amazing read and now I'm starving for more. However, after searching a bit, I found this post about a Scheme Bookshelf. In that, the author s...
I'm working through SICP. Currently, in the first chapter, I'm having problems getting Racket to let me redefine "primitives". For instance, I was under the impression that I should be able to arbitrarily do "(define + 5)" and that would be fine or redefine the sqrt procedure. Instead, I get this:
"define-values: cannot change constant v...
I'm going through "Structure and Interpretation of Computer Programs" and I'm having a bit of trouble doing one of the exercises ( 2.1 ) . I'm coding in DrRacket in R5RS mode.
here's my code :
(define (make-rat n d)
(let (((c (gcd n d))
(neg (< (* n d) 0))
(n (/ (abs n) c))
(d (/ (abs d) c)))
(cons...
In Structure and Interpretation of Computer Programs (SICP) Section 2.2.3 several functions are defined using:
(accumulate cons nil
(filter pred
(map op sequence)))
Two examples that make use of this operate on a list of the fibonacci numbers, even-fibs and list-fib-squares.
The accumulate, filter and map functions are de...
As an exercise for myself, I'm implementing the Miller-Rabin test. (Working through SICP). I understand Fermat's little theorem and was able to successfully implement that. The part that I'm getting tripped up on in the Miller-Rabin test is this "1 mod n" business. Isn't 1 mod n (n being some random integer) always 1? So I'm confused at ...
In The Structure and Interpretation of Computer Programs part 3.2, an "environment" is defined as "a sequence of frames." But as far as I can see, the book doesn't further discuss the difference between an environment and a frame. Also, I suspect the drawings of environments conflates them with frames because books drawings are small a...
I'm currently trying to do exercise 1.22, which needs a function called runtime that returns the number of milliseconds the system has been running. However, my environment (R5RS) does not seem to have this. It does not have time, current-milliseconds, current-inexact-milliseconds, etc, either.
What function do I have access to, to pro...
I'm working on a deque in Scheme (SICP exercise 3.23) and I've got a simple doubly-linked list implementation I would like to test out, but I can't seem to find out how to print out a circular list in Scheme (mit-scheme and mzscheme/racket).
In CL there is a flag print-circle for this sort of thing, is there anything equivalent in Schem...
I'm reading through SICP, and the authors brush over the technique of average damping in computing the fixed points of functions. I understand that it's necessary in certain cases, ie square roots in order to damp out the oscillation of the function y = x/y however, I don't understand why it magically aids the convergence of the fixed po...
Reading through more SICP and I'm stuck on exercise 1.3.8. My code works properly for approximating 1/phi, but doesn't work for approximating e - 2.
(define (cont-frac n d k)
(define (frac n d k)
(if (= k 0)
1.0
(+ (d k) (/ (n (+ k 1)) (frac n d (- k 1))))))
(/ (n 1) (frac n d k)))
(define (eulers-...
The function I wrote for SICP 2.20 is:
(define (same-parity x . y)
(if (null? (car y)
'()
(if (= (even? (car y)) (even? x))
(cons (car y) (same-parity (cons x (cdr y))))
(same-parity (cons x (cdr y))))))
And then I try to call it with
(same-parity 1 2 3 4 5 6 7)
The error I get is:
"The object #t, passed as the fir...
I am working through SICP, and the problem 2.6 has put me in something of a quandary. In dealing with Church numerals, the concept of encoding zero and 1 to be arbitrary functions that satisfy certain axioms seems to make sense. Additionally, deriving the direct formulation of individual numbers using the definition of zero, and an add-1...
I'm reading the following section of SICP
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-26.html#%_sec_4.1.7
According to the text, the following transformation of eval will improve offers a performance improvement, since an expression that gets evaluated many times will only be analyzed once?
(define (eval exp env)
((analyz...