Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:32:23+00:00 2026-06-18T04:32:23+00:00

I’m just starting to learn haskell, and it is a much different way of

  • 0

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]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:32:24+00:00Added an answer on June 18, 2026 at 4:32 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm making a simple page using Google Maps API 3. My first. One marker
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.