[Haskell] Re: new to haskell

Peter Simons simons at cryp.to
Sat May 21 10:07:12 EDT 2005


wenduan  writes:

 > I have an IO action up and running in a module I defined
 > as following:

 > calculate   = do
 >      putStrLn "Give me a number (or 0 to stop):"
 >      number <- getLine
 >      let n = read number
 >      if n == 0
 >            then do
 >               return []
 >            else do
 >                rest <- calculate
 >                return (number : rest)

Note that are returning 'number' (the unparsed string!) rather
than 'n'. So your function's signature is:

  calculate :: IO [String]

That's probably not what you wanted.


 > Now the question is I couldn't get any clue from the first
 > three chapters to do the remaining calculation.

The simplest solution is to use the standard function from
Prelude:

  http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Asum

But I guess writing your own versions won't hurt either. ;-)

Peter



More information about the Haskell mailing list