如果我有列表列表,并且我想要创建一个表,那么它们都对齐了,所有的列都对齐了。我开始了,但不知道怎么继续。
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]示例:
table [[1,2,456],[34,2,34]-->
1 1 456
34 2 34发布于 2014-10-24 12:08:03
您可以使用printf进行打印,也可以使用transpose计算列的maxlen。
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 "" 发布于 2014-10-24 11:28:30
不如:
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 "" 演示:
table [[1,2,456],[34,2,34]]
1 2 456
34 2 34https://stackoverflow.com/questions/26546523
复制相似问题