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. -- ------------------------------------------------------------------------------------------------------- If you didn't succeed at the first attempt, you are just about average.
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
On 5/21/05, wenduan <xuwenduan2010@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
On Sun, May 22, 2005 at 05:52:33AM +0800, wenduan 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
Wenduan, All the exercises comes with answers. The answer for this exercise is on page 171. -- Danie Roux *shuffle* Adore Unix
On 2005 May 21 Saturday 17:52, wenduan wrote:
let n = read number
This converts type String to Integer.
return (number : rest)
This line is building up the list. Put 'n' on the list rather than 'number', so that the list will be numeric. Then when you try "list <- calculate", you can apply 'sum', etc.
sorry for those who feel its quite basic It is no problem. Beginners are welcome. If you have further questions, I recommend using the haskell-cafe mailing list, which is more appropriate for extended discussions.
participants (5)
-
Danie Roux -
Peter Simons -
Scott Turner -
Sebastian Sylvan -
wenduan