0

If I have list of lists, and I want to make a table of them, so that they are all aligned, and all columns the same with. I started but don't know how to continue..

table xxs
    | length (nub [length xs | xs <- xxs])/=1 = error "not simetric"
    | otherwise = (mapM_ print) [ xs | xs <- xxs]

bignumber xxs = maximum [length (show (maximum xs))| xs<-xxs]

Example:

table [[1,2,456],[34,2,34]-->

  1   1 456
 34   2  34
3
  • The code you showed us is not valid haskell code. your function name can not be 2D. And what do you define 2D for if your are not using it? And in your example you have missing parens and the function application is missing. Commented Oct 24, 2014 at 11:05
  • Thank you, I renamed the function. The example I gave is how it should look like, so without parens Commented Oct 24, 2014 at 11:10
  • the function call should be table [[1,2,456],[34,2,34]], so you are asking how to suppress the output of the parens and comma? Commented Oct 24, 2014 at 11:12

2 Answers 2

1

You could use printf for prettyprinting and you could use transpose to calculate maxlen of columns

import Text.Printf
import Data.List (transpose)

table = undefined

showtable xxs = mapM_ (showrow. zip maxlens) xxs
  where
    maxlens = map (show . (+ 1)) $ foldr (max.length.show) 0 $ transpose xxs
    showcell (maxl,c) = printf ("%" ++ ml ++ "s") $ show c
    showrow xs = mapM_ showcell xs >> putStrLn "" 
Sign up to request clarification or add additional context in comments.

Comments

0

How about:

import Data.List

print_ x =  putStr $ (show x) ++ "\t" 

table xxs 
    | length (nub [length xs | xs <- xxs])/=1 = error "not simetric"
    | otherwise = mapM_ printRow xxs 
        where printRow xs =  (mapM_ print_) xs >> putStrLn "" 

Demo:

table [[1,2,456],[34,2,34]]
1   2   456  
34  2   34

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.