本文介绍了在haskell中打印列表的二维列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我有列表的列表,并且我想制作一张表格,以便它们全部对齐,并且所有列都与之相同。我开始,但不知道如何继续。 表xxs |长度(nub [长度xs | xs |否则=(mapM_ print)[xs | xs bignumber xxs = maximum [length(show(maximum xs))| xs 示例: table [[1,2,456],[34,2,34] - > 1 1 456 34 2 34 解决方案 您可以使用 printf 进行漂亮打印,您可以使用转置来计算 maxlen 列 import Text.Printf import Data.List(转置) table = undefined showtable xxs = mapM_(showrow。zip maxlens)xxs where maxlens = map(show。(+ 1) )$ foldr(max.length.show)0 $ transcrib xxs showcell(maxl,c)= printf(%++ ml ++s)$ show c showrow xs = mapM_ showcell xs>> putStrLn 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 解决方案 You could use printf for prettyprinting and you could use transpose to calculate maxlen of columnsimport Text.Printfimport Data.List (transpose)table = undefinedshowtable 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 "" 这篇关于在haskell中打印列表的二维列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 16:27