Algorithm Simulation Using Haskell and Perl6

agent on 2005-05-30T13:26:02

=from 2005.5.30.8:30.AM =to ...5.30.9:15.AM

There are some cases in which we only want to simulate some abstract algorithms without considering the hardware implementation issues. In such situations, Tesla is not very practical. On the other hand, we can perfectly achieve our goals by means of pure "software" methods. For instance, a simple algorithm that converts an unsigned binary number to its octal form can be implemented by the following Haskell codes:

bin2oct :: [Int] -> [Int] bin2oct xs = bin2oct' (replicate (3-r) 0 ++ xs) where r = (length xs) `mod` 3

bin2oct' :: [Int] -> [Int] bin2oct' [] = [] bin2oct' (x:y:z:xs) = [x*4 + y*2 + z] ++ (bin2oct' xs)

As another example, consider the problem of transforming a decimal number to its binary form:

dec2bin :: Int -> [Int] dec2bin 1 = [1] dec2bin 0 = [0] dec2bin x = dec2bin (x `div` 2) ++ [x `mod` 2]

Perl6 can do this job equally well:

sub dec2bin(Int $x) { return $x if $x == 1|0; dec2bin(int $x/2), $x % 2; }

Functional programmers tend to think in a recursive way, I think.

Instead of verify the algorithms involved in logic design "electrically" in Tesla, we can play with such stuff in a complete mathematical sense and take advantage of various advanced programming techniques, such as FP. A lot of joy can be expected then.