Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

agent (5836)


Agent Zhang (章亦春) is a happy Yahoo! China guy who loves Perl more than anything else.

Journal of agent (5836)

Monday May 30, 2005
08:26 AM

Algorithm Simulation Using Haskell and Perl6

[ #24945 ]

=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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.