Klein Program Ideas
"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.
-
a function to compute the Leyland number of x and
y:
xy + yx -
a function to compute the area of a triangle from the lengths
of its sides, (a, b, c), using
Heron's formula: the square root of
s(s-a)(s-b)(s-c), where s is the semiperimeter of the triangle:s = (a+b+c)/2 - a function to determine if n is a perfect square
- a function to return the smallest prime factor of n
-
a tail-recursive program to compute factorial of
n:
n * (n-1) * (n-2) * ... * 2 * 1 -
a function to compute the
hyperfactorial
of n:
nn * (n-1)(n-1) * (n-2)(n-2) * ... * 22 * 11 -
a tail-recursive program to compute factorial of
n:
n * (n-1) * (n-2) * ... * 2 * 1 - a tail-recursive program to compute the hyperfactorial of n
- a function to determine if n is a left-truncatable prime: it is prime, and you always get a prime regardless of how many leading digits are omitted. For example, 37 is a left-truncatable prime, because it is prime and so it 7. 9137 is, too, because 9137, 137, 37 and 7 are all prime.
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:
-
a program that takes an integer as input and prints true
if it is
a perfect number
and false otherwise. There is already code in the
directory of Klein programs to compute factors of an
integer, such as
factors.kln. -
a program that adds fractions. For example:
$ klein addFraction 7 2 4 3 # 7/2 + 4/3 —> 29/6 29 # prints numerator 6 # returns denominator
You can implement other operations (subtract, multiply, divide, simplify, ...), too. If you add a fifth argument, either a boolean switch or an integer flag, you can implement multiple operations in the same program!
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:
-
a program that adds floating-point numbers, which takes
as its arguments two numbers to add and the number of
digits after the decimal point for each. For example:
$ klein addFloat 13482 2 175176 4 # 134.82 + 17.5176 152 # = 152.3376 3376
-
a program that adds floating-point numbers, which takes
as its arguments two numbers, with the digits before and
after the decimal point separated. For example:
$ klein addFloat 134 82 17 5176 # 134.82 + 17.5176 152 # = 152.3376 3376
You can, of course, implement other operations and functions on floating-point numbers that are represented in these ways.