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 7859593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:56:45+00:00 2026-06-02T21:56:45+00:00

This code reads the number of lines to process from the first line of

  • 0

This code reads the number of lines to process from the first line of stdin, then it loops number_of_lines_to_process times doing some calculations and prints the result.
I want it to print the line number in “Line #” after “#” but I don’t know how to obtain it

import IO
import Control.Monad (replicateM)

main :: IO ()

main = do
    hSetBuffering stdin LineBuffering
    s <- getLine
    let number_of_lines_to_process = read s :: Integer
    lines <- replicateM (fromIntegral(number_of_lines_to_process)) $ do
        line <- getLine
        let number = read line :: Integer
            result = number*2 --example
        putStrLn ("Line #"++": "++(show result)) --I want to print the number of the iteration and the result
    return ()

I guess that the solution to this problem is really easy, but I’m not familiar with Haskell (coding in it for the first time) and I didn’t find any way of doing this. Can anyone help?

  • 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-02T21:56:47+00:00Added an answer on June 2, 2026 at 9:56 pm

    You could use forM_ instead of replicateM:

    import IO
    import Control.Monad
    
    main :: IO ()
    main = do
        hSetBuffering stdin LineBuffering
        s <- getLine
        let number_of_lines_to_process = read s :: Integer
    
        forM_ [1..number_of_lines_to_process] (\i -> do
            line <- getLine
            let number = read line :: Integer
                result = number * 2
            putStrLn $ "Line #" ++ show i ++ ": " ++ show result)
    

    Note that because you use forM_ (which discards the results of each iteration) you don’t need the additional return () at the end – the do block returns the value of the last statement, which in this case is the () which is returned by forM_.

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

Sidebar

Related Questions

I currently have this code which reads the first field in a database record
I have this code: File.foreach(testfile.txt) { |line| print line } lines = File.readlines(testfile.txt) lines.each
this is part of my code which reads an http response. It's supposed to
I'm using this code to read the connection string from my app.config file but
This code is supposed to read postfix problems from a file and and write
I'm trying to write a piece of code that reads a file line by
The lines of code that reads the images are $regular = fread(fopen('tmp/tmp3.jpg', r), filesize('tmp/tmp3.jpg'));
I'm looking into developing an application that will process data from a line-scan camera
I wrote this simple test code, by adapting a piece from a book, to
I have some code that reads a file into an array of lines, and

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.