sicp | XML sources of SICP and SICP JS
kandi X-RAY | sicp Summary
kandi X-RAY | sicp Summary
This repository contains processing scripts and sources for the textbook SICP JS: Structure and Interpretation of Computer Programs, JavaScript Edition (SICP JS). See Preface for background. Details how to generate these versions are in the repo wiki. Check out our Resources for Learners, Educators and Researchers, and read more About the SICP JS Project in Interactive SICP JS.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of sicp
sicp Key Features
sicp Examples and Code Snippets
Community Discussions
Trending Discussions on sicp
QUESTION
Is there a Clojure function that does for any function what macroexpand-all
does for a macro?
In SICP, Abelson & Sussman give a demonstration of this that they call the “linear recursive process”.
In other words, if we give:
...ANSWER
Answered 2022-Jan-30 at 04:14This doesn't exist because that's not how evaluation works. The form (* 6 (* 5 (* 4 (* 3 (* 2 1)))))
never exists while computing (factorial 6)
, so the runtime can't just give it to you "in passing".
QUESTION
SICP Exercise 3.16 gives the reader a broken procedure for counting the numbers of pairs in a list structure:
...ANSWER
Answered 2022-Jan-28 at 04:54Write it as
QUESTION
Looking into the SICP book and JS functional programming I created two recursive functions. My expectation was that both of them raised a stack overflow error. But it is only the sumAll() function that raised the error. See below the code for both functions sumAll() and factorial():
As expected the sumAll() function did raise a stack overflow error
...ANSWER
Answered 2022-Jan-03 at 14:42I gave the below answer in error, I got mixed up about which function was throwing the exception. Please ignore.
Your first function is capable of taking advantage of tail-call optimization, while your second function is not (or it is, but perhaps not in a way that's implemented in the node.js language).
Consider this: your first function's usual condition is that it ends in return sumAll(n, i + 1, i + result)
, which means that once you get something to return, you can just return that.
Your second function however ends in return n* factorial((n-1))
, which means that once you get something to return, you have to do ANOTHER operation on it (multiply it by n) before you can actually return the result.
I believe the node.js interpreter isn't able to tail-call-optimize the second function because it requires another operation to be performed on it before the return.
Please note: I am not certain this is the answer, and I suspect node.js may not support tail call optimizations of any sort. This is my theory though about why one function might error in that way and the other one wouldn't.
QUESTION
In exercise 2.73 from section 2.4.3, the following code is shown:
...ANSWER
Answered 2022-Jan-03 at 10:31You could write that, but you haven't really assimilated into the data-directed approach. You've just moved the special-casing for number?
and variable?
into a different function. I think the text is asking you why you can't uniformly dispatch on operator
without any special cases, and the answer is because your representation does not define an operator for numbers or variables.
If I were going to try to make this representation more uniform, I would define a constructor make-number
such that (make-number 6)
yields something tagged as a number, such as '(number . 6)
, and likewise for variables. Then you could install handlers for those tags, and just look at the operator of each datum, because each datum will really have an operator.
QUESTION
I've taken some time to work through the generation of "prime pairs" from SICP Section 2.2.3 - Sequences as Conventional Interfaces, for example:
(1 3)
No, since sum = 4(1 4)
Yes, since sum = 5 (prime)
Here is what I have from scratch, which works:
...ANSWER
Answered 2021-Jul-16 at 19:34Your 2D case is tantamount to two nested loops:
QUESTION
I am reading SICP 2nd Edition.
A painter is represented as a procedure that, given a frame as argument, draws a particular image shifted and scaled to fit the frame.
I am using DrRacket (SICP Picture Language).
How to give a frame as argument to DrRacket when I paint a image by using a painter?
...ANSWER
Answered 2021-Jul-09 at 12:54Here is an example
QUESTION
I've asked a few questions here about Scheme/SICP, and quite frequently the answers involve using the apply
procedure, which I haven't seen in SICP, and in the book's Index, it only lists it one time, and it turns out to be a footnote.
Some examples of usage are basically every answer to this question: Going from Curry-0, 1, 2, to ...n.
I am interested in how apply
works, and I wonder if some examples are available. How could the apply procedure be re-written into another function, such as rewriting map
like this?
ANSWER
Answered 2021-Jun-17 at 21:13apply
is used when you want to call a function with a dynamic number of arguments.
Your map
function only allows you to call functions that take exactly one argument. You can use apply
to map functions with different numbers of arguments, using a variable number of lists.
QUESTION
Let's take the following filter
function:
ANSWER
Answered 2021-Jun-15 at 06:05Yes, your double lambda approach does work. But there are nicer ways to do this too.
It turns out define
can do this directly. The following two pieces of code are identical:
QUESTION
In doing some tests I've noticed that append
always gives me the same output as input when using map
:
ANSWER
Answered 2021-Jun-15 at 01:20The append
procedure takes zero or more list arguments, and a final argument that can be any object. When the final argument is a list, the result of appending is a proper list. When the final argument is not a list, but other list arguments have been provided, the result is an improper list. When only one argument is provided, it is just returned. This behavior with one argument is exactly the behavior of an identity procedure.
QUESTION
I'm arduously struggling my way through the N-queens problem in SICP (the book; I spent a few days on it -- last question here: Solving Eight-queens in scheme). Here is what I have for the helper functions:
...ANSWER
Answered 2021-Jun-12 at 09:35When you are doing the SICP problems, it would be most beneficial if you strive to adhere to the spirit of the question. You can determine the spirit from the context: the topics covered till the point you are in the book, any helper code given, the terminology used etc. Specifically, avoid using parts of the scheme language that have not yet been introduced; the focus is not on whether you can solve the problem, it is on how you solve it. If you have been provided helper code, try to use it to the extent you can.
SICP has a way of building complexity; it does not introduce a concept unless it has presented enough motivation and justification for it. The underlying theme of the book is simplification through abstraction, and in this particular section you are introduced to various higher order procedures -- abstractions like accumulate, map, filter, flatmap which operate on sequences/lists, to make your code more structured, compact and ultimately easier to reason about.
As illustrated in the opening of this section, you could very well avoid the use of such higher programming constructs and still have programs that run fine, but their (liberal) use results in more structured, readable, top-down style code. It draws parallels from the design of signal processing systems, and shows how we can take inspiration from it to add structure to our code: using procedures like map, filter etc. compartmentalize our code's logic, not only making it look more hygienic but also more comprehensible.
If you prematurely use techniques which don't come until later in the book, you will be missing out on many key learnings which the authors intend for you from the present section. You need to shed the urge to think in an imperative way. Using set! is not a good way to do things in scheme, until it is. SICP forces you down a 'difficult' path by making you think in a functional manner for a reason -- it is for making your thinking (and code) elegant and 'clean'.
Just imagine how much more difficult it would be to reason about code which generates a tree recursive process, wherein each (child) function call is mutating the parameters of the function. Also, as I mentioned in the comments, assignment places additional burden upon the programmers (and on those who read their code) by making the order of the expressions have a bearing on the results of the computation, so it is harder to verify that the code does what is intended.
Edit: I just wanted to add a couple of points which I feel would add a bit more insight:
- Your code using set! is not wrong (or even very inelegant), it is just that in doing so, you are being very explicit in telling what you are doing. Iteration also reduces the elegance a bit in addition to being bottom up -- it is generally harder to think bottom up.
- I feel that teaching to do things recursively where possible is one of the aims of the book. You will find that recursion is a crucial technique, the use of which is inevitable throughout the book. For instance, in chapter 4, you will be writing evaluators (interpreters) where the authors evaluate the expressions recursively. Even much earlier, in section 2.3, there is the symbolic differentiation problem which is also an exercise in recursive evaluation of expressions. So even though you solved the problem imperatively (using set!, begin) and bottom-up iteration the first time, it is not the right way, as far as the problem statement is concerned.
Having said all this, here is my code for this problem (for all the structure and readability imparted by FP, comments are still indispensable):
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install sicp
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page