This snippet: (average (f x) x) Means a different thing than this: (average (f) x) For starters, f is a function that expects a single parameter, which in this case must be x, hence we say (f x) . If you write (f) you're trying to call f with no...

Figured it out. The rules are going to apply in every node as promised. You can vote to delete the question. But, I would add some explanation on how I made it working. I changed some code. The original code seems written with some other semantic in mind. I added...

You need to understand how let forms work. In (let ((left-tree (car left-result)) (non-left-elts (cdr left-result)) left-tree does not "call" anything. It is created as a new lexical variable, and assigned the value of (car left-result). The parentheses around it are just for grouping together the elements describing one variable...

algorithm,scheme,time-complexity,primes,sicp

Your long ans short questions are actually addressing two different problems. EX 1.23 is problem(below), why the (next) procedure isn't twice faster than (+ 1)? You provide two possible reasons to explain the relative lack of speed up (and 30% speed gain is already a good achievement): The apparent use...

The problem is that assoc is an existing function in scheme. Try renaming the function to my-assoc, and it will work as expected.

Just do a substitution: (p-car (p-cons "foo" "bar")) ;substituting p-cons: (p-car (lambda (proc) (proc "foo" "bar"))) ;substituting p-car: ((lambda (proc) (proc "foo" "bar")) (lambda (p q) p)) ((lambda (p q) p) "foo" "bar") "foo" And similarly for p-cdr. Essentially p-cons just creates a lambda which expects a procedure which will...

They are the same, since one gets expanded into the other. The first expression is easier to both write & read; having it expand into the second simplifies the interpreter (something you should appreciate).

The SICP language tries to make the examples of the book work directly in the language, but there are bugs lurking around. All Scheme versions prior to R6RS are case insensitive, which means you can write this and make it run ok: (define (test a) (+ A a)) ; A...

function,lambda,nested,scheme,sicp

You're correct that only the outer lambda form is applied to the argument 5. Then it returns its body with a replaced with 5, so it would return (lambda (b) (+ (sqrt 5) (sqrt b))) which is itself a function. This could later be applied to another argument, to produce...

'(x y z) is a list containing three symbols, x, y, and z. It's the same as (list 'x 'y 'z). What you need to use, instead, is (list x y z). The reason using '(4 10 2) (for example) works is that numbers are "self-evaluating". That means (list '4...

Bear in mind that (cdr (cdr ...)) is returning a list (not an element!), so pair? will return true if the list has enough elements (three or more). Perhaps you were aiming for something like this? (define (multiplicand p) (if (null? (cdddr p)) ; assuming list has at least 3...

If we think too hard on recursion, we already fail. Personally, I use two metaphors in thinking recursions. One is from the small book "the little schemer": The Seventh Commandment - Recur on the subparts that are of the same nature. Another is the divide-conquer-combine paradigm for designing algorithms. Essentially,...

data-structures,scheme,abstraction,sicp

The trick is that you don't have to care. cons, cdr and car are abstractions and so their underlying implementation shouldn't matter. What you have there are what are called "Church pairs", we build everything from functions. In modern machines we build everything from strings of 1s and 0s, but...

Your problem is in this line: cc amount, (kinds_of_coins - 1) + cc((amount - (first_denomination(kinds_of_coins))), kinds_of_coins) Which is actually cc(amount, (kinds_of_coins - 1) + cc((amount - (first_denomination(kinds_of_coins))), kinds_of_coins)) when amount is 5 and kinds_of_coins is 2 (amount - (first_denomination(kinds_of_coins))) returns 0, which means that cc(0, anything) returns 1 - and...

Short answer: the first version of reverse is incorrectly building an improper list, the second version is inefficiently building a proper list. We can do better as long as we understand the difference between append and cons. append concatenates two lists. If we use it for just adding an element...

You need to learn what a list is ang get quite intimate with it to be a good lisper. The list (1 2 3) is just visual sugar for the pairs (1 . (2 . (3 . ()))) and can be made with (cons 1 (cons 2 (cons 3 '())))...

1.0 You can. Personally I love the original videos and the transcript are not the same as the lecture notes you linked to but they will teach you some Scheme. 1.1 They seem to be more modern than that 80's video with the wizards themselves. 1.2 SICP was a standard...

functional-programming,scheme,closures,sicp

You're right; this text is not referring to "closures"--an implementation strategy to ensure that functions-as-values refer correctly to lexical bindings--but more generally to the mathematical notion of "closure", as for instance in the statement "the integers are closed under the addition operation". That is: applying the operation to any two...

Thanks to @Gerstmann's suggestion I was able to write the construction I was looking for. The new implementation now looks like: (setq the-empty-stream nil) (defun stream-null? (s) (eq s the-empty-stream)) (defun my/sicp-delay (exp) `(lambda () ,exp)) (defun my/sicp-force (exp) (funcall exp)) (defmacro my/cons-stream (a b) (list 'cons a (my/sicp-delay b)))...

unbind-variable seems to do the trick: 1 ]=> (+ 1 2) ;Value: 3 1 ]=> (define (+ a b) "nonsense") ;Value: + 1 ]=> (+ 1 2) ;Value 2: "nonsense" 1 ]=> (unbind-variable (the-environment) '+) ;Value: #t 1 ]=> (+ 1 2) ;Value: 3 ...

Your version of the function "works" only because you are lucky. Try this experiment: evaluate (fast-prime? 561 3) a hundred times. Depending on the random witnesses that your function chooses, sometimes it will return true and sometimes it will return false. When I did that I got 12 true and...

The key insight to understand here is that in Scheme we can pass functions as parameters to another function as easily as we would pass, say, numbers. Be aware that the template is incomplete, it should be: (define (<name> <term> a <next> b) (if (> a b) 0 (+ (<term>...

I think we can give a more accurate answer by considering other data types, and recursively testing the elements in proper/improper lists. Here's my shot: (define (same-type? a b) (or (and (number? a) (number? b)) (and (symbol? a) (symbol? b)) (and (string? a) (string? b)) (and (list? a) (list? b))...

There is random-integer: (random-integer 10) ;=> 3 Since it works exactly how SICP wants its random we can alias the name for convenience. (define random random-integer) (random 10) ;=> 5 If you keep on working on the online editor, you can just keep that definition in the top of the...

According to the Banach fixed-point theorem, such a point exists iff the mapping (function) is a contraction. That means that, for example, y=2x doesn't have fixed point and y = 0,999... * x has. In general, if f maps [a,b] to [a,b], then |f(x) - f(y)| should be equal to...

In your second test case, which displays the bug, your loop doesn't terminate until a = 6 and b = 5. Then it adds an extra 6 and 5 to the total of 14, to get 25. The reason you found this bug tricky to find may be because the...