Haskell在列表中显示实例 [英] Haskell show instance on list
问题描述
data结构=结构字符串[结构]
我想要这样的输出:
strct
strct1
strct2
strct3
我一直在试试这个
instance显示结构其中
show(Structure a (xs))= show a ++\\\
++++ show xs
但是它的输出是
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
[]]
所以,我不需要括号,也不需要逗号和引号。有任何想法吗?谢谢
我确定有更好的库例程,但不会工作吗? b
$ b
unlines $ a:[++ show x | x < - xs]
但是,这仅涵盖一个级别。您可能想要定义与show不同的功能来维护缩进,或者您必须使用 lines
来分割子节目以查找注入缩进的位置。
这种缩进插入函数的草稿是:
pre> 前缀ps = unlines [p ++ l | l< - 行s]
再次,我确信库中有更好的东西。即使这些简短的片段也经历了几个细化步骤( foldl1(++)
- > concat
- > unlines
,然后以:
的形式加入第一行。
Im having issues with adding a show instance to my data structure, wich is:
data Structure = Structure String [Structure]
and I would like to have this output:
strct
strct1
strct2
strct3
I've been trying this
instance Show Structure where
show (Structure a (xs)) = show a ++ "\n" ++ " " ++ show xs
But its output is
"strct"
["strct1"
[], "strct2"
[]]
So, I would need no brackets, no commas and no quotation marks. Any ideas? Thanks
I'm sure there are better library routines for this, but wouldn't this work?
unlines $ a : [" " ++ show x | x <- xs]
However, that covers only one level. You probably want to define a different function than show to maintain the indentation, or you'd have to keep splitting sub-shows with lines
to find where to inject indentation.
A rough draft of such an indentation insertion function is:
prefix p s = unlines [p ++ l | l <- lines s]
Again, I'm sure there's something better in a library. Even these short snippets have gone through a few steps of refinement (foldl1 (++)
-> concat
-> unlines
, then join the first line as head with :
).
这篇关于Haskell在列表中显示实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!