Monday, June 16, 2008

Area/Volume Programs

Well, over the weekend and today, I created two programs that calculate areas. I'll put them below, but for now, I just want to point out some things I observed.

First, the Haskell tutorials are still giving me trouble. I'm doing a little better, but I had a bit of trouble trying to concatenate a number to a string. I thought it could be done with the ++ operator, but no, it would fail. It turns out you need to call a function called show to turn a number into a string or something like that. I never saw it in any of the basic tutorials.

Second, the compile time of GHC is very noticeable now. Here are some numbers:

real 0m1.370s
user 0m0.472s
sys 0m0.104s

real 0m0.243s
user 0m0.104s
sys 0m0.020s

Big difference for such a small program. Not really sure why this is happening, nor do I think I care too much right now. Of course, as programs get bigger, so do compile times. I wonder if this discourages some users from using a language like Haskell.

On the bright side, I'm liking Haskell's math capabilities more and more. You see, I wrote the Haskell version of this program using a tutorial. Well, I went beyond the tutorial, but that's besides the point. Tonight, I wrote the D version and I compared the outputs. It's very interesting to see the difference in precision that you get from the languages. Haskell is more precise and the runtime doesn't seem any different, thought it appears there is a difference:

GHC (ghc -o a area.hs):
real 0m0.004s
user 0m0.000s
sys 0m0.000s

GDC (gdc area.d):
real 0m0.015s
user 0m0.000s
sys 0m0.004s

The numbers are really to small to really tell a difference and I didn't do any optimizations, so who knows if this is accurate. I put the commands I'm using to do the compilation in parenthesis in case someone is curious.

All in all, I think I'm going to stick with the current pattern of doing something in Haskell first, then in D. I can understand and write code in D much faster than Haskell, so I figure I'd learn the "harder" language first, then write code in the "easier."

Anyway, here are the Haskell and D versions of my program:

{- Calculate the area of a circle. -}
circarea r = pi * r ^ 2;

{- Calculate the area of a triangle. -}
triarea b h = (b * h) / 2;

{- Calculate the area of a rectangle. -}
rectarea l w = l * w;

{- Calculate the area of a square. -}
sqarea s = rectarea s s;

{- Calculate the volume of a box. -}
boxvolume l w h = h * (rectarea l w);

{- Calculate the volume of a cylinder. -}
cylvolume r h = (circarea r) * h;

main = do
{- Apparently, the "show" function is required to convert a number
- to a string? -}
putStrLn ("Circle Area: " ++ (show (circarea 2)));
{- print and putStrLn behave differently. Observe the output. -}
print ("Triangle Area: " ++ (show (triarea 2 2)));
putStrLn ("Rectangle Area: " ++ (show (rectarea 2 2)));
putStrLn ("Square Area: " ++ (show (sqarea 2)));
putStrLn ("Box volume: " ++ (show (boxvolume 2 2 2)));
putStrLn ("Cylinder volume: " ++ (show (cylvolume 2 2)));
import std.stdio; // Module for console IO.
import std.math;

real circarea(real r)
return PI * pow(r,2);

real triarea(real b, real h)
return (b * h) / 2;

real rectarea(real l, real w)
return l * w;

real sqarea(real s)
return rectarea(s, s);

real boxvolume(real l, real w, real h)
return rectarea(l, w) * h;

real cylvolume(real r, real h)
return circarea(r) * h;

int main(char[][] args)
writefln("Circle Area: %f", circarea(2));
writefln("Triangle Area: %f", triarea(2, 2));
writefln("Rectangle Area: %f", rectarea(2, 2));
writefln("Square Area: %f", sqarea(2));
writefln("Box Area: %f", boxvolume(2, 2, 2));
writefln("Cylinder Area: %f", cylvolume(2, 2));
return 0;


Blogger Don Stewart said...

If you're interested in performance, you should be compiling the Haskell with optimisations on,

ghc -O2 Foo.hs

for example.

Regarding 'show', if you immediately want to show and then putStrLn, you can instead use the 'print' function, which prints any value in the Show class to standard output.

See you on #haskell !

8:22 PM  
Blogger blockcipher said...


Thanks for the info. I'm mainly trying to learn the language right now, so I'm not worried about performance yet. I mainly did it out of curiosity to see if there was a difference.

As for "show", I'll have to give it a try. I do know I tried to do something like:

print "string" ++ (func a b)

That didn't work, hence why I tried putStrLn and show.

3:22 PM  

Post a Comment

Links to this post:

Create a Link

<< Home