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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:50:08+00:00 2026-06-13T13:50:08+00:00

I need to print out a matrix in haskell so it looks like this:

  • 0

I need to print out a matrix in haskell so it looks like this:

main> putStr (showMat [[1,-500,-4], [100,15043,6], [5,3,10]])
       1  -500 -4
     100 15043  6
       5     3 10

So far I have come up with this:

type Matrix a = [[a]]
type IntMat = Matrix Integer

showMat :: IntMat -> String
showMat [] = ""
showMat ((y:ys):xs)  = (printRow (rowmaxs) (elements) (y:ys)) ++ "\n" ++ showMat xs
  where rowmaxs = rowMaxs ((y:ys):xs) ; elements = elementLengths (y:ys)

rowMaxs :: IntMat -> [Int]
rowMaxs [] = []
rowMaxs (x:xs) = [length (show (maximum (x)))] ++ (rowMaxs xs)

elementLengths :: [Integer] -> [Int]
elementLengths [] = []
elementLengths (y:ys) = [length (show y)] ++ (elementLengths ys)

printRow :: [Int] -> [Int] -> [Integer] -> String
printRow [] (a:as) (y:ys) = ""
printRow (z:zs) (a:as) [] = ""
printRow [] [] (y:ys)     = ""
printRow [] [] []         = ""
printRow (z:zs) (a:as) (y:ys) = addSpaces (z-a) ++ show y ++ [' '] ++ printRow zs as ys

addSpaces :: Int -> String
addSpaces 0 = ""
addSpaces n = " " ++ addSpaces (n-1)

Which returns this:

Main> putStr (showMat [[1,23,1],[23,56,789],[1234,0,1]])
      1  23    1 
      23   56 
     1234

I can see the loss of the elements at the end of rows is due to the cases in the printRow function, but I don’t know how to fix it. Also the characters don’t take into account the ones printed before them. Any help on how I can fix this would be appreciated.

  • 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-13T13:50:09+00:00Added an answer on June 13, 2026 at 1:50 pm

    In showMat you calculate rowmaxs and elements anew for each row. In particular, rowmaxs has an element for each row left in the matrix, but printRow uses it as meaning something for each column of the matrix.

    Edit: Here’s a slightly better version of showMat:

    showMat :: IntMat -> String
    showMat rows = concat (map perRow rows)
      where rowmaxs = rowMaxs rows
            perRow cols = printRow rowmaxs elements cols ++ "\n"
              where elements = elementLengths cols
    

    It’s still buggy — rowMaxs (tries to) calculate the maximum length of the numbers in each row, but you really want the maximum length of the numbers in each column. One consequence of that is that you occasionally pass negative numbers to addSpaces, which doesn’t cope terribly well, so here’s a version of addSpaces that behaves more gracefully:

    addSpaces :: Int -> String
    addSpaces n = replicate n ' '
    

    So now we get this:

    *Main> putStr (showMat [[1,23,1],[23,56,789],[1234,0,1]])
     1  23    1 
    23  56  789 
    1234   0    1 
    

    Better, but not yet working properly.

    I have not fixed all the bugs in your code because I sense you are still learning and need the experience of finding them for yourself.

    I advise using map/zipWith/zipWith3 instead of explicitly writing the recursion, as it makes code easier to understand.

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

Sidebar

Related Questions

I have a situation and it goes like this: I need to print out
i need to print out numbers 1-100 in a random order. the print statement
I need to print out a 3 by 3 matrix in C. I could
I need to print out receipts that inserted into my database, and the latest
I need to print a receipt from my web based apps using dot matrix
I need help, I got a problem with this matrix, I need to have
I need to print out the contents of an xml file into some txt
I need to print out an easy to read object graph from Visual Studio
I'm currently working on a project where I need to print out a lesson
I need to print out only one of various consecutive lines with same first

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.