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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:48:03+00:00 2026-05-27T14:48:03+00:00

I have a problem, user inputs number m (meaning matrix has m-rows, m-columns), then

  • 0

I have a problem, user inputs number m (meaning matrix has m-rows, m-columns), then the matrix is read from stdin. I want to transpose the matrix and then output the matrix back on the screen in the same format.
for example:

Main> main
3
1 2 3
4 5 6
7 8 9

1 4 7
2 5 8
3 6 9

Anyway, I got somewhere in the middle, but i dont know how to parse the [[Int]] back to IO, so it can be print on the screen.

here is my code:

import Control.Monad (replicateM)

transpose :: [[a]]->[[a]]
transpose ([]:_) = []
transpose x = (map head x) : transpose (map tail x)

readMany :: Read a => IO [a]
readMany = fmap (map read . words) getLine

parse :: IO ([[Int]])
parse = do
        [m] <- readMany
        xss <- replicateM m readMany
        let matrix = transpose xss
        return (matrix)

main :: IO ()
main = do     
          parse
          ??    --
          print ??
  • 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-05-27T14:48:03+00:00Added an answer on May 27, 2026 at 2:48 pm

    First, you have to extract the value you parse:

    main :: IO ()
    main = do
      matrix <- parse
      ...
      print ???
    

    Now, what’s the type of print?

    print :: (Show a) => a -> IO ()
    

    Well, [[Int]] is an instance of Show, so we can treat print as a specialisation of its type:

    print :: [[Int]] -> IO ()
    

    So, in fact, it’s as simple as filling in the list we want to print in place of the ???:

    main :: IO ()
    main = do
      matrix <- parse
      print matrix
    

    However, this prints the data out in the same way that GHCi does. If you want to print the data out in a custom format, you want:

    putStr :: String -> IO ()
    

    Let’s say you write a function to format the matrix the way you want into a string:

    formatMatrix :: [[Int]] -> String
    

    This makes the complete program:

    main :: IO ()
    main = do
      matrix <- parse
      putStr (formatMatrix matrix)
    

    The difference between foo <- bar and let foo = bar is that the former extracts the result of an IO computation, while the latter just gives a name to a value.

    So basically, the answer is that you don’t need to put anything into IO to do this, since all the functions that create IO actions to do things like print strings out take pure values. This doesn’t make them limited to printing the results of completely pure computations, however, since you can extract values from previous IO computations, and then feed them in to the printing functions as pure values. (In fact, this is the essence of monads, and why Haskell uses them to model IO in the first place!)

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

Sidebar

Related Questions

I have a bit of a problem with user controls. Basically what I want
I have problem removing rows from a table according to the checkbox. Below is
I have a problem with the MySQL root user in My MySQL setup, and
I have a problem regarding getting the path of a user control. The scenario
I have a problem for querying records into mysql explanation below The user could
I have a problem that i cant solve :( I have a user control
i still have a problem to develop a convenient app to the user. because
I have the following problem: I have an Activity where a user can start
The problem: I have set of pictures, when the user presses on one of
I've got the following problem: I have two tables: (simplified) +--------+ +-----------+ | User

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.