[Haskell] new to haskell

Sebastian Sylvan sebastian.sylvan at gmail.com
Sat May 21 10:10:07 EDT 2005


On 5/21/05, wenduan <xuwenduan2010 at gmail.com> wrote:
> hi all,
> I'm new to haskell and just reading the "yet another haskell tutorial".I
> got stuck on the exercise of chapter 3,the problem goes like "repeatedly
> prompt the user to input some number until a zero is detected,then
> output the sum,product of those numbers and the factorial of each
> number." What I have done is I have an IO action up and running in a
> module I defined as following:
> 
> module Main where
> main = do
>    calculate
> 
> 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)
> Now the question is I couldn't get any clue from the first three
> chapters to do the remaining calculation,what I wanted to do is to get a
> list out of the IO action I just defined, and do the sum,product and
> factorial out of the list,I tried something like"list <- calculate, sum
> list,etc,but it wasn't helpful,so would anyone just give me any hint to
> solve this,sorry for those who feel its quite basic.
> 

Someting like this perhaps?

main = do
  list <- calculate
  let s = sum list
       p = product list
       f = map fac list
  putStrLn $ "Sum: " ++ show s
  putStrLn $ "Product: " ++ show p
  putStrLn £ "Factorials: " ++ show f


/S

-- 
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862


More information about the Haskell mailing list