【发布时间】:2017-11-22 18:58:46
【问题描述】:
我有一个问题,仍在研究家谱树,这是我目前得到的(抱歉葡萄牙语单词 xD):
data Familia = Node String [Familia]
instance Show Familia where
show (Node a b) = show a ++ "\n\t" ++ show b
raiz :: String -> Familia
raiz n = (Node n [])
juntar :: String -> String -> Familia -> Familia
juntar a b (Node c l)
| b == c = (Node c (l ++ [raiz a]))
| otherwise = (Node c (juntarAux a b l))
juntarAux :: String -> String -> [Familia] -> [Familia]
juntarAux a b [] = []
juntarAux a b [(Node x l)]
| x == b = [(juntar a b (Node x l))]
| otherwise = [(Node x (juntarAux a b l))]
juntarAux a b ((Node x l):xs)
| x == b = (juntar a b (Node x l)):xs
| otherwise = (Node x l):(juntarAux a b xs)
这是按照我想要的方式工作的,问题是,这是我当前的输出:
*Main> let f = raiz "Bob"
*Main> let g = juntar "John" "Bob" f
*Main> g
"Bob"
["John"
[]]
我想要的是,像这样打印它:
Bob
John
Ruth
Hank
所以,家庭的根是 Bob,Bob 的儿子是 John 和 Hank,John 有一个女儿叫 Ruth。
我已经尝试了几种方法来使用我在其他帖子中看到的东西,但这是最新的尝试:
instance Show Familia where
show (Node a b) = show a ++ "\n\t" ++ (unlines $ map (unwords . map show) b)
这给了我以下错误:
t4.hs:14:77:
Couldn't match type ‘Familia’ with ‘[a0]’
Expected type: [[a0]]
Actual type: [Familia]
In the second argument of ‘map’, namely ‘b’
In the second argument of ‘($)’, namely
‘map (unwords . map show) b’
有什么想法吗?提前致谢! :D
【问题讨论】:
-
我认为(尽管我犹豫是否使用我的 dup-hammer)这在结构上与 stackoverflow.com/questions/47423268/instance-show-haskell 相同的问题
-
更接近this question,我在您发布的问题的 cmets 中链接了它
-
@AdamSmith 好点 xD 一定错过了这些帖子,会查看它们并希望从中得到一些东西:D 谢谢