[Haskell-cafe] Haskell and C++ program

Sukit Tretriluxana tretriluxana.s at gmail.com
Wed Jan 14 15:45:22 EST 2009

Hi all,

I was looking around Stroustrup's website and found a simple program that he
showed how standard library can be used to make the program succinct and
safe. See http://www.research.att.com/~bs/bs_faq2.html#simple-program. I
wondered how a Haskell program equivalent to it looks like and I came up
with the code below.

import qualified Control.Exception as E

main = E.catch (interact reverseDouble) (\_ -> print "format error")

reverseDouble = unlines . doIt . words
   where doIt = intro . toStrings . reverse . toDoubles . input
         toDoubles = map (read::String->Double)
         toStrings = map show
         input = takeWhile (/= "end")
         intro l = ("read " ++ (show $ length l) ++ " elements") :
                   "elements in reversed order" :

I'm not a Haskell expert and I am pretty sure that this is not the optimal
form a Haskell program that's equivalent to the C++ one. So I would like to
see, from the Haskell experts in this group, how else (of course better
form) such a program can be written.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090114/bc3aa1bc/attachment.htm

More information about the Haskell-Cafe mailing list