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]
print takes a value of any type that's an instance of Show (meaning that we know how to represent it as a string), calls show with that value to stringify it and then outputs that string to the terminal. Basically, it's just putStrLn .
return is actually just a simple function in Haskell. It does not return something. It wraps a value into a monad. Looks like return is an overloaded function.
in goes along with let to name one or more local expressions in a pure function.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With