Transpose Matrix in Haskell

Published on 11 May 2026 (Updated: 11 May 2026)

Welcome to the Transpose Matrix in Haskell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution


module Main where

import System.Environment
import Text.Read
import Data.List (transpose, intercalate)
import Data.Function ((&))
import Control.Monad (guard)

-- Converts string in format "1, 2, 3" to a Maybe list of int
stringToListMaybe :: String -> Maybe [Int]
stringToListMaybe str = readMaybe $ "[" ++ str ++ "]" :: Maybe [Int]

listToMatrix :: Int -> [Int] -> [[Int]]
listToMatrix _ [] = []
listToMatrix n l  = let (row, xs) = splitAt n l in row : listToMatrix n xs

listToString :: (Show a) => [a] -> String
listToString = intercalate ", " . map show

parseArgs :: [String] -> Maybe (Int, Int, [Int])
parseArgs [cols, rows, list] = do
  cols' <- readMaybe cols
  rows' <- readMaybe rows
  list' <- stringToListMaybe list
  guard $ cols' > 0 && rows' > 0 && cols' * rows' == length list'
  return (cols', rows', list')
parseArgs _ = Nothing

main :: IO ()
main = do
  args <- getArgs
  case parseArgs args of
    Nothing        -> putStrLn "Usage: please enter the dimension of the matrix and the serialized matrix"
    Just (c, _, l) -> listToMatrix c l & transpose & concat & listToString & putStrLn

Transpose Matrix in Haskell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.