0

I'm trying to create a program in Haskell that takes in user inputs, adds each input line up, and spits out the final sum once the user inputs a negative number (the total sum not including the negative number, to be specific). I attempt

 sumF1 :: IO () sumF1 = do totSum <- sumF2 0 print totSum sumF2 :: Int -> IO Int sumF2 prev = do n<-getInt if n<0 then return prev else sumF2 (prev+n) 

However, when I try this, I just get a function that prints on every single line and always repeats the input as opposed to summing it up. How do I fix it so that it only prints a sum at the end and that it adds properly.

2
  • Possible duplicate of Reading numbers from input Haskell Commented Nov 28, 2015 at 9:14
  • That code does what you describe (assuming getInt :: IO Int; getInt = fmap read getLine or equivalent). Either you're not showing us all your code or you're trying to do something else. Commented Nov 28, 2015 at 23:59

4 Answers 4

3

I'm trying to create a program in Haskell that takes in user inputs, adds each input line up, and spits out the final sum once the user inputs a negative number

If this is the only thing you want, then you can simply write

main = getContents >>= print . sum . takeWhile (>= 0) . map read . lines 

Otherwise you can use this combinator

repeatWhile :: Monad m => (a -> Bool) -> m a -> m [a] repeatWhile p a = do x <- a if p x then (x:) <$> repeatWhile p a else return [] 

like this

main = repeatWhile (>= 0) readLn >>= print . sum 
Sign up to request clarification or add additional context in comments.

Comments

0

Try writing a function which gets the next number and returns a "IO (Maybe Integer)" with "Just" for a positive input and "Nothing" for a negative one. Then use that in a recursive function to create a list of integers (hint: one of its arguments will be the list of integers so far). Then use that in a function that adds up all the integers in the list.

Comments

0

I don't think it is broken, but I don't know your definition of getInt. For me this one works fine:

getInt :: IO Int getInt = do str <- getLine return (read str) 

taken from here.

2 Comments

Your source says that getLine is provided by the prelude, not getInt. There is no getInt in the prelude.
You can write that much more concisely: getInt = getLine >>= read. Obviously it still needs the type signature.
0

The definitions for the functions sumF1 and sumF2 seem to work well, probably the issue is with the getInt function you use to get n each time.

Try adding this line for the definition of getInt:

getInt = getLine >>= \n -> return (read n :: Int) 

Which uses getLine from the prelude.

1 Comment

That's the implementation I'm also using. My code still has problems where it takes in each line as input and prints each line back out to me (kinda like echoing) instead of actually functioning properly

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.