Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs

Questions tagged [sicp]

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.

Filter by
Sorted by
Tagged with
6 votes
1 answer
478 views

Redefine count-leaves as an accumulation

Exercise 2.35. Redefine count-leaves from section 2.2.2 as an accumulation: ...
5 votes
1 answer
224 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 ...
5 votes
3 answers
550 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 the ...
10 votes
2 answers
5k views

Sum of squares of two largest of three numbers

Given the following problem (SICP Exercise 1.3): Define a procedure that takes three numbers as arguments and returns the sum of squares of the two largest numbers. I wrote the following (...
3 votes
2 answers
2k 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. ...
3 votes
1 answer
200 views

SICP 1.3 Sum of Squares of two largest numbers

Define a procedure that takes three numbers as arguments and returns the sum of squares of the two largest numbers. I'm using just the machinery that was developed so far in SICP to be true to the ...
5 votes
5 answers
63k 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 are said to be ”in ...
0 votes
2 answers
1k views

Iterative "counting change" implementation in mit-scheme

Here is my iterative implementation of the Counting change example in SICP. Please note that I'm a beginner, which means I only know the basic syntax for the time being. Requirement: How many ...
3 votes
2 answers
749 views

SICP - exercise 1.7 better end test for square root approximation

Here is exercise 1.7 from SICP: Exercise 1.7 The good-enough? test used in computing square roots will not be very effective for finding the square roots ...
5 votes
4 answers
4k 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 ...
2 votes
1 answer
995 views

Generating all unique permutations of a string

Here is a plan for generating the permutations of S: For each item x in S, recursively generate the sequence of permutations of S - x, and adjoin x to the front of each one. This yields, for each ...
3 votes
1 answer
1k 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
2 answers
8k views

Reverse the order of a list in Racket

I am using the SICP book. There is an exercise in which you need to create a function that will receive a list as an argument and return a list with the same elements in a reverse order. I know there ...
1 vote
1 answer
161 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 $$ \frac{d(u^n)}{dr} = nu^{n-1}(\frac{...
2 votes
1 answer
658 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 ...
2 votes
0 answers
148 views

Computing nth roots of a number - SICP exercise 1.45

From SICP Exercise 1.45: We saw in 1.3.3 that attempting to compute square roots by naively finding a fixed point of x/y does not converge, and that this can be fixed by average damping. ...
1 vote
2 answers
2k views

Pascal's triangle in mit-scheme

As suggested by the name of source file (ex1.12.scm), I just started learning mit-scheme by reading SICP. In Exercise 1.12, I'm asked to "compute elements of Pascal'...
3 votes
1 answer
474 views

Church Numerals

Here is exercise 2.6 from SICP: Exercise 2.6: In case representing pairs as procedures wasn’t mind-boggling enough, consider that, in a language that can manipulate procedures, we can get by ...
3 votes
1 answer
222 views

SICP exercise 1.28 - miller-rabin primality test part II

This is a follow-up to SICP exercise 1.28 - miller-rabin primality test. Exercise 1.28: One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; ...
2 votes
1 answer
301 views

SICP exercise 1.28 - miller-rabin primality test

From SICP Exercise 1.28: One variant of the Fermat test that cannot be fooled is called the Miller-Rabin test (Miller 1976; Rabin 1980). This starts from an alternate form of Fermat’s ...
3 votes
1 answer
1k views

SICP - exercise 1.12 - pascal's triangle

From SICP: Exercise 1.12: The following pattern of numbers is called Pascal’s triangle. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 . . . The numbers at the ...
2 votes
1 answer
446 views

SICP - exercise 1.11 - tree recursion

From SICP Exercise 1.11: A function \$f\$ is defined by the rule that: \$f(n) = n\$ if \$n < 3\$, and \$f(n) = f(n-1)+2f(n-2)+3f(n-3)\$ if \$n >= 3\$. Write a procedure ...
3 votes
1 answer
255 views

Square root calculation in Scheme (SICP Exercise 1.7)

I have done exercise 1.7 in SICP (calculate square root precision when change in guesses is under a certain value), but I am calling the change-in-precision function twice in each iteration, which ...
2 votes
1 answer
169 views

SICP exercise 1.3 - sum of squares of two largest of three numbers

From SICP Exercise 1.3: Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers. Square is: ...
4 votes
1 answer
286 views

SICP - exercise 2.69 - generate a huffman tree from a set of ordered leaves

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 tree ...
2 votes
1 answer
197 views

SICP exercise 2.28 - counting leaves in a tree (recursive process)

From SICP Exercise 2.28: Write a procedure fringe that takes as argument a tree (represented as a list) and returns a list whose elements are all the leaves of the tree arranged in left-to-...
2 votes
2 answers
611 views

SICP - exercise 2.27 - reversing elements of a list and sublists

From SICP Exercise 2.27: Modify your deep-reverse procedure of Exercise 2.18 to produce a deep-deep-reverse procedure that takes a list as argument and returns as its value the list with its ...
2 votes
1 answer
109 views

SICP - exercise 2.5 - representing pairs of nonnegative integers using only numbers and arithmetic operations

From SICP Exercise 2.5: Show that we can represent pairs of nonnegative integers using only numbers and arithmetic operations if we represent the pair a and b as the integer that is the product 2^x*3^...
2 votes
1 answer
54 views

Replacing elements from a list and its sublists - part II

This is sort of a follow-up to Replacing elements from a list and its sublists but now there are arbitrary numbers of words that would be replaced stored in a list. Now write substitute2 that takes ...
1 vote
2 answers
400 views

SICP - exercise 2.20 - same-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 definition, a parameter ...
2 votes
1 answer
85 views

Replacing elements from a list and its sublists

Write a procedure substitute that takes three arguments: a list, an old word, and a new word. It should return a copy of the list, but with every occurrence of the old word replaced by the new word, ...
3 votes
1 answer
1k views

Reversing a list without (append)

I would like to reverse a list using cdr, car and cons. Since lists in lisp are asymmetrical (can only insert at the beginning), I am interested on how one would write a procedure to do that without ...
5 votes
1 answer
199 views

Finding next perfect number - brute force

A “perfect number” is defined as a number equal to the sum of all its factors less than itself. For example, the first perfect number is 6, because its factors are 1, 2, 3, and 6, and 1+2+3=6. The ...
3 votes
1 answer
112 views

Replacing words from a sentence

I am extremely new at scheme and I am doing this problem from here: Write a procedure switch that takes a sentence as its argument and returns a sentence in which every instance of the words I or ...
4 votes
2 answers
243 views

Squaring a tree in Clojure

I am working on Problem 2.30 from Structure and Interpretation of Computer Programs. I book is in scheme, but I am doing the exercises in Clojure. The problem is to write code that takes a tree of ...
6 votes
3 answers
2k 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 ...
-1 votes
2 answers
3k 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 are said to be ”in place....
0 votes
1 answer
495 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
1 answer
1k 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. ...
4 votes
1 answer
156 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 the ...
3 votes
1 answer
92 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 the ...
5 votes
1 answer
217 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 the ...
3 votes
1 answer
255 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
0 answers
805 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). Then, in ...
4 votes
1 answer
210 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 ...
1 vote
2 answers
3k 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
1 answer
318 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 (...
0 votes
1 answer
2k views

Iterative sum using recursion

Given the following recursive definition of sum: (define (sum term a next b) (if (> a b) 0 (+ (term a) (sum term (next a) next b)))) ...
0 votes
1 answer
1k 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 ...
2 votes
1 answer
562 views

Make a version of make-rat that handles positive and negative arguments

Given the following task from SICP Exercise 2.1 Define a better version of make-rat that handles both positive and negative arguments. Make-rat should normalize the sign so that if the ...