Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Haskell help

Name: Anonymous 2007-11-17 15:23

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

let sum = foldr (+) 0 final
  in
    return sum


but it gives me an error. What do I do?

Name: Anonymous 2007-11-17 15:25

Fix the error.

Name: Anonymous 2007-11-17 15:29

>>2
Hurrrrr.

The compiler says that it expects a [String] but I deliver something else.

Name: Anonymous 2007-11-17 15:34

number <- getLine
How do I summed a [String]?

Name: Anonymous 2007-11-17 15:38

>>4
huh? Please explain.

Name: Anonymous 2007-11-17 15:40

>>5
getLine :: IO String
(+) :: Num a => a -> a -> a

Name: Anonymous 2007-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: Anonymous 2007-11-17 17:08

omNomNom xs = putStrLn "im hunggry hombre" >> getLine >>= (\ x -> if x == 0 then return xs else omNomNom (x:xs)) . read

Name: Anonymous 2007-11-18 12:35

STOP TALKING ABOUT HASKELL

Name: Anonymous 2007-11-18 13:03

>>9
Don't like Haskell?  What's the matter?  Too Enterprise-ready for you?

Name: Anonymous 2007-11-18 14:27

>>10
I never metalanguage I didn't like, but STOP FUCKING TALKING ABOUT HASKELL ON /prog/

Name: Anonymous 2007-11-18 15:02

/prog/ needs more Haskell-related discussion.

Name: Anonymous 2007-11-18 15:05

Haskell is shit.

/prog/ needs less toy language trolling and more actual discussion about interesting topics.

Name: Anonymous 2007-11-18 15:09

>>13
actual discussion about interesting topics
Like?

Name: Anonymous 2007-11-18 15:26

>>14
Haskell.

Name: Anonymous 2007-11-18 15:34

This thread is now about Richard Stallman's penis.

Name: Anonymous 2007-11-18 18:08

When did prog become a spoiler? I thought it was just You just lost the game..

Name: Anonymous 2007-11-18 18:11

>>17
I think it was a couple of weeks ago.

Name: Anonymous 2007-11-19 10:41

haskellFag

Name: dv 2007-11-19 12:45


module Main where

import Data.IORef
import Control.Monad

main = do
    numbers <- newIORef []
    process numbers

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


This is REAL Haskell.

Name: Anonymous 2007-11-19 12:48

>>20
Just like IOCCC contenders are REAL C.

Name: Anonymous 2007-11-19 13:01

Just like Lisp is a REAL language.

Name: Anonymous 2007-11-19 13:19

Just like Ruby is a REAL language.

Name: Anonymous 2007-11-19 13:28

Just like car is my OTHER cdr.

Name: Anonymous 2007-11-19 13:32

Just like this thread is ANYHOW valuable.

Name: Anonymous 2007-11-19 18:21

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: Anonymous 2007-11-19 19:00

>>20
THAT DOESN'T LOOK VERY REFERENTIALLY TRANSPARENT TO ME!!

Name: Anonymous 2007-11-19 19:36

lol, Haskell

why even bother?

Name: Anonymous 2007-11-20 3:58

>>21
>>27
Come back once you graduate from A Gentle Introduction to writing real Haskell code.

Name: Anonymous 2007-11-20 5:12

>>21
>>27
Come back once you graduate from K&R to writing real C code.

Name: Anonymous 2007-11-20 5:12

>>21
>>27
Come back once you graduate from phpfreaks.com to writing real PHP code.

Name: Anonymous 2007-11-20 5:45

>>29
>>30
>>31
Leave and don't come back.

Name: Anonymous 2007-11-20 5:52

>1-32
Come back once you've realized you're wasting your time posting on /prog/

Name: Anonymous 2007-11-20 6:17

>>33
that doesn't make any sense

Name: Anonymous 2007-11-20 8:19

>>34
Nor does /prog and the fact we're here

Name: Anonymous 2007-11-21 5:19

>>35
/prog/ makes PERFECT sense

Name: Anonymous 2007-11-21 22:26

>>33
You win the thread... have a cake

(which is a lie)

Name: Anonymous 2011-02-04 14:01


Don't change these.
Name: Email:
Entire Thread Thread List