Tagged Questions

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.

1answer
78 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 Little ...
1answer
23 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 ...
1answer
37 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 ...
1answer
52 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 ...
1answer
43 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 ...
1answer
53 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: ...
1answer
63 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 ...
1answer
56 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-...
2answers
64 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 ...
1answer
46 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*...
1answer
24 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 ...
2answers
42 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 ...
1answer
53 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 ...
1answer
56 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, ...
1answer
61 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 ...
2answers
87 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 ...
1answer
50 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 ...
3answers
470 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 ...
2answers
247 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 ...
5answers
4k 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 ...
1answer
120 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 ...
1answer
271 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. ...
1answer
67 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 ...
1answer
51 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 ...
1answer
131 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 ...
3answers
323 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 ...
1answer
87 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 ...
0answers
115 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). ...
1answer
95 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 ...
2answers
100 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 ...
2answers
407 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 ...
0answers
161 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". ...
2answers
166 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 ...
0answers
157 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 ...
0answers
170 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 ...
0answers
153 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 ...
1answer
297 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 ...
0answers
460 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. ...
1answer
283 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 ...
0answers
160 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 ...
0answers
341 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 ...
1answer
763 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 ...
1answer
877 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. ...
1answer
346 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 ...
0answers
1k 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 ...
1answer
340 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 ...
1answer
186 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 ...
0answers
399 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 ...
1answer
283 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 ...
1answer
124 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 ...