I'm just starting to learn haskell, and this is a completely different way of thinking than what I'm used to (C style languages).
In any case, for one problem, I am working on the fact that I need to get user input. It will be presented in the form
2 10 20
eg. The format is the first line that indicates the number of lines. My first thought was that I would read the first line and then run the loop. This is Haskell! As far as I know, loops are impossible.
My next thought was that I would use the first line of input to populate the list with the other n numbers that follow. I have no idea how to do this. I am here because Iβm not even sure what I will look for to understand this.
Thanks in advance for showing me a haskell way of doing this. It is still difficult, but I hear rave reviews from people who are "enlightened", so I believe that this can not stop myself from learning a language.
Here is the code that will run once just fine, but should run once for each of the lines with two by n following the first line.
ln = (-1)^n/(2*(fromIntegral n)+1) am = sum [ln | n <- [0..(m-1)]] main = do b <- readLn print (ab)
(In addition, I would like to hear if there are other improvements that I could make to my code, but in this particular case, it is for competition to solve the problem with the least number of characters. I do not want to get more specific information if others people are trying to find the answer to the same problem.)
EDIT: Thanks for the answers. In the end, I got what behaved the way I wanted it. I put the code below for posterity. Unfortunately, even though he passed the test tests with flying colors, the actual data they tested was different, and all they tell me is that I got a βwrong answerβ. This code "works" but does not give you the correct answer.
import Control.Monad ln = (-1)^n/(2*(fromIntegral n)+1) am = sum [ln | n <- [0..(m-1)]] main = do b <- readLn s <- replicateM b readLn mapM_ print [ac | c <- s]
haskell
maccam912
source share