sicp

using lambda instead of let in scheme

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 using DrScheme, why does cons return a list, instead of a pair of lists?

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?

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...

Is there a Scheme interpreter that uses Normal-order evaluation?

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...

After "The Little Schemer", what's next?

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...

Dr Racket problems with SICP

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...

Problem with 'let' syntax in scheme

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...

SICP Accumulate function

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...

Confused on Miller-Rabin

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 SICP 3.2, The Environment Model of Evaluation, is an environment initially the same as its first frame?

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...

SICP, Scheme, DrRacket Question: Timer/profiler function?

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...

Is there an equivalent of Common Lisp's *print-circle* in Scheme?

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...

Why does average damping magically speed up the convergence of fixed-point calculators?

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...

Why the bleep isn't my continued fraction approximating properly?

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-...

What is wrong with my scheme code?

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...

Arithmetic with Church Numerals.

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...

Question about SICP chpt 4.1 : How does (analyze expr) help speed up eval?

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...