friki data migration

Ranch Hand

Posts: 772

1

posted 1 year ago

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. All you need to get started is this jar file.

You'll also want to get a copy of the

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

To start with, here's a short Scheme program that solves the first problem of Project Euler :

Save the following code in a file called

One thing to get used to in Scheme (as in all Lisp-derived languages) is the

The second

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

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

Note that the

CategoryLearnSomethingNew

A good Scheme implementation written in Java is Kawa. All you need to get started is this jar file.

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 ProgramsScheme 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 WikiPedia:Factorial of a number (n!) and the numbers of the WikiPedia: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.CategoryLearnSomethingNew

Consider Paul's rocket mass heater. |