Forums Register Login
Learn Scheme
Scheme is a functional language, so it is somewhat similar to Scala, although the syntax is rather different. Everything in Scheme is a function - there are no classes or objects.

A good Scheme implementation written in Java is Kawa, the latest version of which is available here.

You'll also want to get a copy of the Scheme Report, which not only describes the language but also contains an introduction: online HTML, zipped HTML files, PDF. A very good introductory textbook for Scheme is Structure and Interpretation of Computer Programs

Scheme question can be asked in the Other Languages forum here at JavaRanch.

An example

To start with, here's a short Scheme program that solves the first problem of Project Euler : If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Save the following code in a file called euler1.scm in the same directory where you put the Kawa jar file.

One thing to get used to in Scheme (as in all Lisp-derived languages) is the prefix notation of functions: The operator is listed first, and then the arguments. So "(or A B)" is the same as "A || B" in Java. And "=" is not used for assignments, it is the test for equality. So the first define section is a function called dividableBy3or5? that returns true if the argument is dividable by 3 or 5, and false otherwise.

The second define section is the actual program. It receives a single parameter LIMIT (which -in the above example- would be 1000) and proceeds to test all numbers up to 1000 recursively. cond is like an if-then-elsif in Java; it can have as many conditions as desired, and if none matches, the result of the else clause will be the result of the function.

The following are the commands to start a Kawa session from the command line, load the file we created, evaluate the sums until 10 and 1000, respectively, and then exit Kawa. You need to hit return/enter at the end of each line to have Kawa interpret it.

That's close, but not quite there. The result for 10 is 33 - not 23. That's because the recursion starts at 10 inclusively, not exclusively as the problem description states. We can avoid that by introducing a little helper function that starts the calculation at the number below LIMIT. We also move the definitions of dividableBy3or5? and euler1Minus1 inside of euler1; that way they don't clutter up the global namespace, because they're not visible outside of euler1.

More examples

Scheme is particularly well suited for problems that are defined recursively, or that can solved by recursive algorithms. Here are functions that calculate the Factorial of a number (n!) and the numbers of the Fibonacci number sequence.

Note that the fibonacci function is very inefficient, and will become slow very quickly for growing n. But the code mirrors the recursive definition closely, and is thus the "natural" implementation.


You can read but need at least 5 cow(s) to post
This thread has been viewed 679 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 23, 2018 21:04:28.