Structure and Interpretation of Computer Programs (SICP) is a classic textbook for learning how to program. The language used in the book is Scheme, a dialect of Lisp.
5
votes
3answers
114 views
Recursive and iterative approach for mergesort
Problem:
Question 8: *
Mergesort is a type of sorting algorithm. It follows a naturally
recursive procedure:
Break the input list into equally-sized halves Recursively sort both ...
-1
votes
2answers
82 views
Filter a list with given predicate - python [closed]
For the following question, the function
• should mutate the original list
• should NOT create any new lists
• should NOT return anything
Function that do not create new lists ...
2
votes
5answers
516 views
Shift elements left by n indices in a list
For the following question, the function
• should mutate the original list
• should NOT create any new lists
• should NOT return anything
Functions that do not create new lists ...
0
votes
1answer
78 views
Interval multiplication - faster version
For the below given problem from this assignment:
Q4. In passing, Ben also cryptically comments, "By testing the signs of the endpoints of the intervals, it is possible to break ...
1
vote
1answer
75 views
Is there something wrong with my remove-duplicates implementation in Scheme?
For an assignment I handed in this code to remove duplicates from a stream.
...
3
votes
1answer
61 views
SICP Exercise 1.3: Sum of squares of two largest numbers out of three, Prolog Version
The exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following:
Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of ...
3
votes
1answer
40 views
SICP Exercise 1.3: Sum of squares of two largest numbers out of three, Rust Version
The exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following:
Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of ...
5
votes
1answer
117 views
SICP Exercise 1.3: Sum of squares of two largest numbers out of three, Haskell Version
The exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following:
Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of ...
5
votes
3answers
259 views
SICP Exercise 1.3: Sum of squares of two largest numbers out of three
The exercise 1.3 of the book Structure and Interpretation of Computer Programs asks the following:
Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of ...
3
votes
1answer
61 views
SICP streams in C++
To brush up on my C++ chops, I've implemented a toy version of "SICP Streams", which behave like lists with one twist: the first element of the list is always available, the rest of the list is stored ...
4
votes
0answers
87 views
Building Data abstraction and ADT for rectangle using “objects”
For the below given exercise:
Exercise 7: Abstracting Rectangles
Implement a representation for rectangles in a plane. (Hint: You may want to make use of your procedures from exercise 5). ...
4
votes
1answer
76 views
Encapsulated state in clojure
While going through SICP and trying to implement the code in clojure, I've found that while I can get the code in chapter 3 to work, it seems to go against Clojure idioms, but I can't quite imagine ...
3
votes
2answers
95 views
This snippet of scheme calculates a value in pascal's triangle
I'm working through SICP and have implemented exercise 1.11 (Pascal's Triangle). What I'm curious about here is performance considerations by defining functions within the main function. I would ...
7
votes
2answers
382 views
My first accumulators
Notes
I'm working my way through SICP, and as I got very confused by the section on folds, I decided to try to implement foldr in scheme and javascript to understand how it works differently with ...
1
vote
0answers
148 views
SICP ex. 2.42 “eight queens puzzle”
The problem can be found online here.
In short, we're given the following function definition, that will recursively generate all the possible solutions for the "eight-queen-problem".
...
7
votes
2answers
164 views
Write a procedure stream-limit that finds
From SICP:
Exercise 3.64. Write a procedure
stream-limit that takes as arguments a
stream and a number (the tolerance).
It should examine the stream until it
finds two successive elements ...
1
vote
0answers
153 views
Write a definition of a semaphore in terms of test-and-set! operations
From SICP:
Exercise 3.47. A semaphore (of size
n) is a generalization of a mutex.
Like a mutex, a semaphore supports
acquire and release operations, but it
is more general in that up to n
...
1
vote
0answers
159 views
Write a definition of a semaphore in terms of mutexes
From SICP:
Exercise 3.47. A semaphore (of size
n) is a generalization of a mutex.
Like a mutex, a semaphore supports
acquire and release operations, but it
is more general in that up to n
...
3
votes
0answers
135 views
Representing a queue as a procedure with local state
From SICP:
Exercise 3.22. Instead of
representing a queue as a pair of
pointers, we can build a queue as a
procedure with local state. The local
state will consist of pointers to the
...
4
votes
1answer
239 views
Examine a list for cycles
From SICP:
Exercise 3.18. Write a procedure that
examines a list and determines whether
it contains a cycle, that is, whether
a program that tried to find the end
of the list by taking ...
1
vote
0answers
406 views
Correctly count the number of pairs in an irregular list structure
From SICP:
For background, here is exercise 3.16:
Exercise 3.16
Ben Bitdiddle decides
to write a procedure to count the
number of pairs in any list structure.
...
1
vote
1answer
261 views
Order of evaluation of function arguments
From SICP:
Exercise 3.8
When we defined the
evaluation model in section 1.1.3, we
said that the first step in evaluating
an expression is to evaluate its
subexpressions. But we never ...
1
vote
0answers
152 views
Coercion of arguments using successive raising
From SICP:
Exercise 2.84
Using the raise
operation of exercise 2.83, modify the
apply-generic procedure so that it
coerces its arguments to have the same
type by the method of ...
1
vote
0answers
315 views
Coercion with multiple arguments
From SICP:
Exercise 2.82
Show how to generalize
apply-generic to handle coercion in
the general case of multiple
arguments. One strategy is to attempt
to coerce all the arguments to ...
2
votes
1answer
699 views
Huffman encoding successive-merge function
From SICP:
Exercise 2.69. The following
procedure takes as its argument a list
of symbol-frequency pairs (where no
symbol appears in more than one pair)
and generates a Huffman encoding ...
1
vote
1answer
727 views
Encode-symbol for Huffman tree
From the text:
Exercise 2.68. The encode procedure
takes as arguments a message and a
tree and produces the list of bits
that gives the encoded message.
...
2
votes
1answer
318 views
Search on a binary tree
From SICP:
Exercise 2.66. Implement the lookup
procedure for the case where the set
of records is structured as a binary
tree, ordered by the numerical values
of the keys.
I wrote the ...
2
votes
0answers
871 views
Union-set intersection-set for a binary-tree implementation of sets
From SICP:
Exercise 2.65
Use the results of
exercises 2.63 and 2.64 to give (n)
implementations of union-set and
intersection-set for sets implemented
as (balanced) binary trees.41
...
1
vote
1answer
261 views
Adjoin-set for an ordered set representation
From SICP:
Exercise 2.61
Give an implementation
of adjoin-set using the ordered
representation. By analogy with
element-of-set? show how to take
advantage of the ordering to produce a
...
4
votes
1answer
178 views
Set representation allowing duplicates
From SICP:
Exercise 2.60. We specified that a
set would be represented as a list
with no duplicates. Now suppose we
allow duplicates. For instance, the
set {1,2,3} could be represented as
...
1
vote
0answers
368 views
Standard Algebraic Derivative Calculator
I had some difficulty with this problem, so I'm sure there is a better way. Here is the question from SICP:
Exercise 2.58
Suppose we want to
modify the differentiation program so
that it ...
3
votes
1answer
249 views
Extend sums and products functions
Exercise 2.57. Extend the
differentiation program to handle sums
and products of arbitrary numbers of
(two or more) terms. Then the last
example above could be expressed as
...
0
votes
1answer
113 views
Extending basic differentiator to handle more kinds of expressions
Exercise 2.56. Show how to extend the
basic differentiator to handle more
kinds of expressions. For instance,
implement the differentiation rule
by adding a new clause to the deriv
...
1
vote
1answer
649 views
equal? predicate for lists
Exercise 2.54
Two lists are said to
be equal? if they contain equal
elements arranged in the same order.
For example,
...
1
vote
1answer
227 views
Adding, subtracting, and multiplying a vector by a scalar
Exercise 2.46. A two-dimensional
vector v running from the origin to a
point can be represented as a pair
consisting of an x-coordinate and a
y-coordinate. Implement a data
abstraction ...
1
vote
1answer
170 views
Writing a general purpose “split” function (for SICP's imaginary language)
From SICP 2.2.4:
The textbook has already defined a function (right-split ...) as follows:
...
4
votes
3answers
952 views
Eight-queens puzzle
Figure 2.8: A solution to the
eight-queens puzzle. The
``eight-queens puzzle'' asks how to
place eight queens on a chessboard so
that no queen is in check from any
other (i.e., no two ...
7
votes
3answers
834 views
Find all distinct triples less than N that sum to S
Exercise 2.41. Write a procedure to
find all ordered triples of distinct
positive integers i, j, and k less
than or equal to a given integer n
that sum to a given integer s.
...
0
votes
1answer
239 views
Defining a unique-pairs procedure
From the section called Nested Mappings
Exercise 2.40
Define a procedure
unique-pairs that, given an integer n,
generates the sequence of pairs (...
4
votes
2answers
947 views
Reverse in terms of fold-right and fold-left
Exercise 2.39
Complete the following definitions of reverse
(exercise 2.18) in terms of fold-right
and fold-left from exercise 2.38:
...
4
votes
1answer
2k views
Matrix multiplication and dot-product
Exercise 2.37. Suppose we represent
vectors v = (vi) as sequences of
numbers, and matrices m = (mij) as
sequences of vectors (the rows of the
matrix). For example, the matrix
is ...
4
votes
1answer
366 views
Redefine count-leaves as an accumulation
Exercise 2.35. Redefine count-leaves
from section 2.2.2 as an accumulation:
...
4
votes
1answer
876 views
Abstract tree-map function
Exercise 2.31. Abstract your answer
to exercise 2.30 to produce a
procedure tree-map with the property
that square-tree could be defined as
...
1
vote
1answer
231 views
Square-tree using maps and recursion
Define a procedure square-tree analogous to the
square-list procedure of exercise
2.21. That is, square-list should behave as follows:
...
0
votes
1answer
471 views
Producing a deep-reverse procedure
Exercise 2.27
Modify your reverse
procedure of exercise 2.18 to produce
a deep-reverse procedure that takes a
list as argument and returns as its
value the list with its elements
...
1
vote
2answers
2k views
A definition of for-each
Exercise 2.23
The procedure for-each
is similar to map. It takes as
arguments a procedure and a list of
elements. However, rather than forming
a list of the results, for-each just
...
1
vote
2answers
770 views
Filter a list of integers by parity
Exercise 2.20
The procedures +, *,
and list take arbitrary numbers of
arguments. One way to define such
procedures is to use define with
dotted-tail notation. In a procedure
...
1
vote
2answers
2k views
Design a procedure to reverse a list
SICP exercise 2.18 asks the following:
Exercise 2.18. Define a procedure
reverse that takes a list as argument
and returns a list of the same
elements in reverse order:
...
1
vote
1answer
169 views
A more efficient mul-interval
From 2.11
Exercise 2.11. In passing, Ben also
cryptically comments: ``By testing the
signs of the endpoints of the
intervals, it is possible to break
mul-interval into nine cases, only ...
1
vote
1answer
235 views
Interval Subtraction
From the Extended Exercise beginning in section 2.1.4, you can find exercise 2.8:
Exercise 2.8. Using reasoning
analogous to Alyssa's, describe how
the difference of two intervals may be
...