问题描述
我一直试图创建一个展示实例,以便可视化给定的矩阵,并创建一个包含矩阵之间和周围的列的轮廓。我到目前为止设法完成的工作如下:
$ p $ data $ a $ Mat $ [$ a $]
实例(显示a)=> Show(Mat a)
show(Mat x)=\\\
++--- \\\
++ unlines(map(\r - > showRow r ++\ n ---)x)++\\\
其中
showRow list =|++ unwords(map(\v-> show v ++|)列表)
假设我们有一个矩阵 Mat [[1,2,3 ],[4,5,6]]
,我们想测试。
命令行的输出如下:
---
| 1 | 2 | 3 |
---
| 4 | 5 | 6 |
---
我想要实现的格式给定的矩阵在每列的其余部分上方有水平线,如下所示:
--- --- ---
| 1 | 2 | 3 |
--- --- ---
| 4 | 5 | 6 |
--- --- ---
如果假设矩阵的所有行都相同列数(或至少表示第一行) y矩阵的列数至少与其他列数一样多),并且这三个破折号可以作为任何单元格的边界,您可以通过取 如果这些假设不成立,您需要对数组进行一次遍历来计算任何行的最大宽度,然后第二遍来绘制矩阵。如果矩阵可以包含 算法可能是: 正如Rein Henrichs在评论中提到的那样,您可能会考虑将此函数命名为 I've been trying to create a show instance in order to visualize a given matrix and also, to create an outline with columns around and in between the matrix. What I managed to accomplish so far is the following: Assuming we have a matrix The output from the command line is the following: What I would like to achieve is to format the given matrix with horizontal lines above the rest of each column, like that: If you make the assumptions that all rows of a matrix have the same number of columns (or at least that the first row of any matrix has at least as many columns as any other), and that three dashes suffice as the border for any cell, you can calculate the number of dashes to use between rows by taking the If these assumptions do not hold, you need to make one pass over the array to calculate the maximum width of any row, then a second pass to draw the matrix. If matrices can contain numbers outside the range The algorithm might be: As Rein Henrichs mentioned in a comment, you might consider naming this function something other than 这篇关于在Haskell中显示矩阵的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!长度来计算行间使用的破折号数量$ c
[0..9]
范围之外的数字,则还需要计算每列的宽度。
$ b
String
,生成一个 [[String]]
。
[[String]]
中的每个字符串,其列宽为空格。
[String]
,其元素是每一行的图形表示形式。
[String]
中任何元素的长度(因为所有列现在被填充到其最大宽度,所有行现在应该具有相同的长度)并生成这些数字的绘图字符,如ASCII ---
或Unicode ┌─┬─┐
。
String
。 b $ b show
以外的其他名称。 data Mat a = Mat [[a]]
instance (Show a) => Show (Mat a) where
show (Mat x) = "\n" ++ " ---\n"++unlines ( map (\r -> showRow r ++ "\n ---") x ) ++ "\n"
where
showRow list = "¦ "++unwords ( map (\v -> show v ++" ¦") list)
Mat [[1,2,3],[4,5,6]]
that we would like to test. ---
¦ 1 ¦ 2 ¦ 3 ¦
---
¦ 4 ¦ 5 ¦ 6 ¦
---
--- --- ---
¦ 1 ¦ 2 ¦ 3 ¦
--- --- ---
¦ 4 ¦ 5 ¦ 6 ¦
--- --- ---
length
of the leading row.[0..9]
, you would also need to calculate the width of each column.String
, generating a [[String]]
.[[String]]
with spaces to its column width.[String]
whose elements are the graphical representation of each row.[String]
(As all columns are now padded to their maximum width, all rows should now have the same length.) and generate that number of box-drawing characters, such as ASCII "---"
or Unicode "┌─┬─┐"
.String
separated by newlines.show
.