I am supposed to Write a program that will repeatedly ask the user for numbers until she
types in zero, at which point it will tell her the sum of all the numbers, the product of
all the numbers, and, for each number, its factorial.
Here's what I have:
module Main
where
import IO
numbData = do
putStrLn "give numbers"
number <- getLine
if read number == 0
then return []
else do
numbers <- numbData
let final = (number : numbers)
in
return final
The problem is processing the list to calculate the sum etc. I've tried
>>5 getLine :: IO String (+) :: Num a => a -> a -> a
Name:
Anonymous2007-11-17 16:39
>>5
What >>6 is saying is that getLine returns a string, so in number <- getLine, number is a string. You can't add two strings together using (+); you must first convert the string into a number using read, as you did in the predicate of your if statement.
To make your code simpler, you could do: number <- getLine >>= (return . read)
Name:
Anonymous2007-11-17 17:08
omNomNom xs = putStrLn "im hunggry hombre" >> getLine >>= (\ x -> if x == 0 then return xs else omNomNom (x:xs)) . read
Name:
Anonymous2007-11-18 12:35
STOP TALKING ABOUT HASKELL
Name:
Anonymous2007-11-18 13:03
>>9
Don't like Haskell? What's the matter? Too Enterprise-ready for you?
Name:
Anonymous2007-11-18 14:27
>>10
I never metalanguage I didn't like, but STOP FUCKING TALKING ABOUT HASKELL ON /prog/
Name:
Anonymous2007-11-18 15:02
/prog/ needs more Haskell-related discussion.
Name:
Anonymous2007-11-18 15:05
Haskell is shit.
/prog/ needs less toy language trolling and more actual discussion about interesting topics.
process :: IORef [Int] -> IO ()
process ref = do
putStr "enter a number: "
n <- getLine >>= return . read
if n == 0 then do putStr "the sum of numbers is: "
x <- readIORef ref
print . sum $ x
forM_ x $ \y -> do
putStrLn $ "Factorial " ++ show y ++ " is "
++ show (product [1..y])
else do modifyIORef ref ((:) n)
process ref
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
IOREF IS FOR SUCKERS
Name:
Anonymous2007-11-19 19:00
>>20 THAT DOESN'T LOOK VERY REFERENTIALLY TRANSPARENT TO ME!!
Name:
Anonymous2007-11-19 19:36
lol, Haskell
why even bother?
Name:
Anonymous2007-11-20 3:58
>>21 >>27
Come back once you graduate from A Gentle Introduction to writing real Haskell code.
Name:
Anonymous2007-11-20 5:12
>>21 >>27
Come back once you graduate from K&R to writing real C code.
Name:
Anonymous2007-11-20 5:12
>>21 >>27
Come back once you graduate from phpfreaks.com to writing real PHP code.