maccam912
maccam912

Reputation: 872

The haskell way to accept user input a user inputted number of times?

I'm just starting to learn haskell, and it is a much different way of thinking than what I'm used to (the C style languages).

Anyway, for one problem I'm working on I need to receive user input. It will come in the form

2
10
20

for example. Format is the first line says the number of lines that follow. My first thought was that I would read the first line, then have a loop run that number of times. This is Haskell though! As far as I know, loops are not possible.

My next thought was that I would use the first line of input to fill a list with the other n number of numbers that follow. I have no idea how I would do this though. I'm here because I'm not even sure what I would search for to figure it out.

Thanks in advance for showing me the haskell way to do this. It is tough going so far, but I hear rave reviews from people who are "enlightened" so I figure it can't hurt to learn the language myself.

Here is the code that will run once just fine, but needs to run once for each of the second through n lines that follow the first line.

l n = (-1)^n/(2*(fromIntegral n)+1)
a m = sum [l n | n <- [0..(m-1)]]
main =
    do  b <- readLn
        print (a b)

(Also, I would love to hear if there are other improvements I could make to my code, but in this specific case it is for a competition to solve a problem in the fewest number of characters possible. I don't want to get more specific in case other people are trying to search for an answer to the same problem.)

EDIT: Thanks for everyones answers. I eventually got something that behaved how I wanted it to. I put the code for that below for posterity. Sadly, even though it passed the test cases with flying colors, the actual data they tested it on was different, and all they tell me is that I got the "wrong answer." This code "works" but doesn't get you the correct answer.

import Control.Monad
l n = (-1)^n/(2*(fromIntegral n)+1)
a m = sum [l n | n <- [0..(m-1)]]
main =
    do  b <- readLn
        s <- replicateM b readLn
        mapM_ print [a c | c <- s]

Upvotes: 8

Views: 1773

Answers (4)

Cubic
Cubic

Reputation: 15673

I'm not sure what exactly you want to do, but to read an integer n and then the next n lines as integers you could do something like:

import Control.Monad

-- read n, then sum integers read from the next n lines
test = do n <- readLn
          xs <- replicateM n readLn
          return $ sum xs

The return $ sum xs at the end of course isn't substantial - if it wasn't there you'd need an explicit type signature for test though.

If you don't understand any of these functions, just hoogle them.

Upvotes: 2

Venge
Venge

Reputation: 2437

Carl's solution will work, but it's somewhat opaque. If you wanted to write it out, you could do something like this:

readLines :: Int -> IO [Int]
readLines 0 = return []
readLines n = do
   x <- fmap read getLine
   rest <- readLines (n-1)
   return $ x : rest

readSomeNumberOfLines :: IO [Int]
readSomeNumberOfLines = do
   n <- fmap read getLine
   readLines n

What you're doing here with readLines is you're essentially defining the obvious base case (to read 0 things, just give an empty list) and the recursive case (to read n things, read one thing, then read the other n-1 things, then combine them together).

Upvotes: 7

Carl
Carl

Reputation: 27003

First of all, you can loop just fine in haskell. It happens all the time. You just don't have syntactic constructs for it, since there's no need for them.

Most of the time, common general-purpose loops are put into libraries. In this case, the loop you need is in the standard libraries, in the module Control.Monad. It's called replicateM. It has the type signature Monad m => Int -> m a -> m [a]. To specialize this signature for your case, it'd have the type Int -> IO Int -> IO [Int]. The first argument is the number of times to loop. The second is the IO action to run on each loop. The result of the function is an IO action that produces the list of inputs.

So if you added inputs <- replicateM b readLn to your do block, it would put a list named inputs into scope that contains the values from the b lines of input following the first one. You could then map your solution function over those lines.

Upvotes: 10

Code-Apprentice
Code-Apprentice

Reputation: 83517

You could create a readInput n where n is the number of lines to read. The call this recursively subtracting 1 from n each time. I am a Haskell noob as well, so this might not be the best approach. It should still work, though.

Upvotes: 1

Related Questions