Scheme is a functional programming language in the Lisp family, closely modeled on lambda calculus with eager (applicative-order) evaluation.
4
votes
0answers
59 views
Knapsack solution in Scheme/Racket
Following is my solution to Knapsack problem using all combinations of the sent list of items (in Racket programming language). Although it is very short, I am sure it can be improved in many ways.
<...
3
votes
1answer
54 views
Structuring flat html structure into a tree
Given an html page with heading tags such h1, h2 through h6 and ...
1
vote
2answers
55 views
Proper use of tail recursion while forming a list
As a project to better develop my understanding of functional programming, I am writing a prime number generator in Scheme. I am using a simple brute-force algorithm to detect whether a number is ...
1
vote
0answers
19 views
SICP - exercise 2.5 - Represent pairs of nonnegative integers
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 ...
0
votes
0answers
39 views
SICP - exercise 2.3 - Implement a representation for rectangles in a plane
Exercise 2.3. Implement a representation for rectangles in a plane. (Hint: You may want to make use of exercise 2.2.) In terms of
your constructors and selectors, create procedures that compute the
...
2
votes
2answers
215 views
Sudoku solver in Racket
I have written following code to solve Sudoku puzzles in Racket (a Scheme/Lisp derivative programming language). Following relatively simple code appears to work but are there any bugs in it or can it ...
1
vote
1answer
52 views
Approximating pi/4 using Wallis Product - SICP exercise 1.31
From SICP
The sum procedure is only the simplest of a vast number of similar
abstractions that can be captured as higher-order procedures. Write an
analagous procedure called product that ...
0
votes
1answer
57 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 ...
1
vote
2answers
76 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
1answer
40 views
Simplifying Macro-Generating Racket Macro
I'm trying to write a small macro library in Racket that extends a few of the racket/match functions I use, by printing which clause was expanded.
While this seems ...
3
votes
0answers
88 views
Solving Sudokus in Scheme Using Simulated Annealing
This program's purpose is to solve sudokus using simulated annealing. It first fills all the 3x3 blocks in the sudoku such that every block is filled with 1-9. It also builds a list of pairs of ...
3
votes
0answers
29 views
Converting a sentence string to a number in Scheme
I wrote a scheme procedure that converts strings such as "Two hundred and fifty" into a number like "250" that I can use in math calculations (It's for some Natural Language Processing project).
Is ...
1
vote
2answers
53 views
Function that tests whether all items in a list are the same
The program consists of two functions: the helper predicate function singleton-list? that checks whether a given list contains only one item and the function that ...
3
votes
2answers
191 views
Remove the last occurrence of an item in a list
The objective is given a list of items, remove the last occurrence of a specified item from the list using only user-defined functions except for very basic built-in ones like car, cdr, =, - etc. For ...
3
votes
1answer
79 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
1answer
99 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
1answer
37 views
Find common factor of numbers in a list
This program calculates common factor for given numbers. The original use case is to find a possible resolution for pixel games that scale well with new screen resolutions (that's why I don't have any ...
2
votes
1answer
119 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 ...
6
votes
1answer
105 views
Purely functional Sieve of Eratosthenes
Many implementations of Sieve of Eratosthenes (used to find prime numbers up to a given n) use a temporary mutable array to keep track of which numbers are composites. I'm looking to write a purely ...
2
votes
1answer
29 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 ...
1
vote
1answer
58 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 ...
3
votes
1answer
63 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
1answer
63 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 ...
6
votes
1answer
79 views
All possible ways of merging two lists while keeping their order
I am writing a function gives all possible ways of merging two ordered lists such that in the merged list, the elements are still ordered according to the elements in their respective starting list.
...
2
votes
1answer
55 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:
...
1
vote
1answer
60 views
Functional coffee dispenser [closed]
Inspired by this question, I decided to rewrite this code in Scheme, as I'm working through SICP right now. Any feedback welcome.
...
4
votes
1answer
86 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 ...
3
votes
1answer
61 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
2answers
85 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
1answer
54 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
1answer
25 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
2answers
60 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 ...
3
votes
1answer
112 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 ...
3
votes
1answer
60 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, ...
5
votes
1answer
106 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
1answer
59 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 ...
1
vote
1answer
138 views
Looping in Scheme
(define loop
(lambda (x proc)
(when (not (= x 0))
(eval proc)
(loop (- x 1) proc))))
Is this the best way to create a loop function in Scheme?
4
votes
1answer
48 views
Merging list tails in Scheme
In my last post, I wrote a long, non-general tail-merging†facility for insertion sort. I thought I should instead write a general-purpose tail-merging function, that works even when the two lists are ...
6
votes
3answers
134 views
Insertion sort in Scheme
Inspired by my merge sort in Scheme, I thought I'd try my hand at implementing other sorting algorithms, starting with simple ones (like insertion sort) and working my way up. So, here we go:
...
2
votes
1answer
112 views
String Concatenation in Scheme
As plain r5rs is lacking in useful string processing utilities, such as string-split and string-join (I'm using MIT/GNU Scheme), I decided to implement my own. My procedure, string-join, works just ...
3
votes
1answer
109 views
3
votes
1answer
134 views
Seeking 5 primes such that concatenating any two of them produces another prime
I have a program which solves Project Euler problem 60:
The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be ...
2
votes
0answers
28 views
A scheme macro that defines the "where“ syntax
A new scheme user trying to use syntax-rules to define something like the Haskell where keyword.
Sometimes this is useful since ...
1
vote
1answer
368 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.
...
2
votes
4answers
4k views
Splitting a list in Racket
I am working my way into learning a bit of Scheme (actually, Racket) with the help of a few practical problems to solve. In this case, I want to split a flat list of symbols at a certain delimiter ...
6
votes
1answer
149 views
Fisher-Yates shuffle in Scheme
There are many ways to shuffle lists; for example, Racket's built-in shuffle assigns each list element with a random number as a sort key, then sorts the list, ...
3
votes
3answers
623 views
Sum of squares of 2 largest numbers out of a list of 3 numbers
The problem is:
Define a procedure that takes 3 number arguments and returns the sum of the squares of the 2 largest numbers.
I am a CS graduate and I am revising my university notes. This is my ...
3
votes
1answer
121 views
Digit Summing Function
I'm working through the common divisibility tests, and implementing them in Racket. I'm working on divisible by three, for which I thought it'd be nice to keep summing until I got to a single digit (...
3
votes
0answers
155 views
Graphical editor with geometric intersection
If it is possible I would like some comments on the overall style of the program. It feels like I am writing the whole program as one big script and I'm not sure how to break it down into several ...
10
votes
1answer
459 views
WebSocket-based API library in Racket
I've developed a library in (typed) Racket that wraps a WebSocket-based API. The API itself is very simple—it operates via a series of three-letter commands optionally followed by a JSON-encoded ...