Klein Program Ideas

I have added some new ideas below.

"Meaningful Programs"

For several of your compiler project stages, I will ask you to write a "meaningful Klein program". By this, I mean a program that does something a user of Klein might find useful or interesting. Solving a real problem would be meaningful; so would creating routines that effectively add a new data type or set of operations to the language.

When students can't think of anything to do on their own, they occasionally ask for suggestions. I love to program and am curious about all kinds of problems, so I usually have some ideas. Some of the programs in the Klein programs directory are the result of my curiosity. Most of the rest are the result of previous compiler students' curiosity. Here are some ideas I haven't implemented yet and think might be cool.

New Ideas

I have implemented a few of these functions for fun. Some have never been written by any Klein programmer. You can be the first!

Not all of these are very challenging, because they require only arithmetic and calls to a function from the Klein collection. Others require a helper function and usually some recursion.

Note: Recall that there is a function in the Klein collection to compute exponentials, EXP.

Programs on Integers

Klein's main data type is the integer, and the language feels almost like a high-level integer assembly language. Consider these tasks that manipulate integers in various ways:

Programs on Floating-Point Numbers

Klein doesn't have floating-point numbers, but we can implement programs to simulate and manipulate floating-point numbers.

For example, divide.kln prints the first n digits of a/b and returns (and thus prints) the division's remainder at that point:

$ klein divide 7 12 2       # 7/12 = 0.58...
5
8
4
$ klein divide 7 12 4       # 7/12 = 0.5833...
5
8
3
3
4

Here are a couple of possible programs that take floating-point numbers as arguments, represented in different ways:

You can, of course, implement other operations and functions on floating-point numbers that are represented in these ways.