与显示函数实例相关的错误

发布于 2024-11-01 22:48:33 字数 2374 浏览 0 评论 0原文

我有以下代码,我认为我遗漏了一些东西:

bruteSolveWithStartAndEnd :: Node -> Node -> [Node] -> [(Float, [Node])] -> [(Float, [Node])]
bruteSolveWithStartAndEnd st en mp r
           | length mp - 1 == length (snd ( head r)) = sortBy (comparing fst) $ map (\x -> (fst x + (distanceBetweenTwoNodes en $ last $ snd x), snd x ++ [en])) r
           | otherwise = bruteSolveWithStartAndEnd st en mp $ nextStepsForMany r $ filter (/= en) mp

bruteSolve :: [Node] -> [(Float, [Node])] -> [(Float, [Node])]
bruteSolve mp [] = bruteSolve mp $ map (\l -> (0, [l])) mp
bruteSolve mp r
           | length mp == length(snd (head r)) = sortBy (comparing fst) $ r
           | otherwise = bruteSolve mp $ nextStepsForMany r mp

nextSteps :: (Float, [Node]) -> [Node] -> [(Float, [Node])]
nextSteps r n = map (\l -> ((fst r) + (distanceBetweenTwoNodes l $ last $ snd r), snd r ++ [l])) [ nn | nn <- n, nn `notElem` (snd r)]

nextStepsForMany :: [(Float, [Node])] -> [Node] -> [(Float, [Node])]
nextStepsForMany ar n = concat $ map (\l -> nextSteps l n) ar

代码本身有效。但奇怪的是,虽然函数 bruteSolve 和 bruteSolveWithStartAndEnd 具有相同的“结果”类型,但 bruteSolveWithStartAndEnd 的结果无法在 GHSi 中打印,而 bruteSolve 的结果可以:

*> bruteSolve [(Node 0 1), (Node 2 3), (Node 4 5)] []
[(5.656854,[Node {x = 0, y = 1},Node {x = 2, y = 3},Node {x = 4, y = 5}]),(5.656854,[Node {x = 4, y = 5},Node {x = 2, y = 3},Node {x = 0, y = 1}]),(8.485281,[Node {x = 0, y = 1},Node {x = 4, y = 5},Node {x = 2, y = 3}]),(8.485281,[Node {x = 2, y = 3},Node {x = 0, y = 1},Node {x = 4, y = 5}]),(8.485281,[Node {x = 2, y = 3},Node {x = 4, y = 5},Node {x = 0, y = 1}]),(8.485281,[Node {x = 4, y = 5},Node {x = 0, y = 1},Node {x = 2, y = 3}])] 


*> bruteSolveWithStartAndEnd (Node 0 0) (Node 5 5) [Node 0 0, Node 1 1, Node 2 2, Node 3 3, Node 4 4, Node 5 5]

<interactive>:1:1:
    No instance for (Show ([(Float, [Node])] -> [(Float, [Node])]))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for
      (Show ([(Float, [Node])] -> [(Float, [Node])]))
    In a stmt of an interactive GHCi command: print it

我做错了什么?我改变了一些东西(不记得是什么),从那时起这个错误就出现了。根据我的理解(Haskell 的第一天),打印函数将检查 [..] 是否派生 Show,然后递归地检查所有内部元素(在本例中为 list->tuple->(Float & Node)) ...无论如何,既然“结果”声明是相同的,为什么会出现这种差异?

干杯!

I have the following code and I think I am missing something:

bruteSolveWithStartAndEnd :: Node -> Node -> [Node] -> [(Float, [Node])] -> [(Float, [Node])]
bruteSolveWithStartAndEnd st en mp r
           | length mp - 1 == length (snd ( head r)) = sortBy (comparing fst) $ map (\x -> (fst x + (distanceBetweenTwoNodes en $ last $ snd x), snd x ++ [en])) r
           | otherwise = bruteSolveWithStartAndEnd st en mp $ nextStepsForMany r $ filter (/= en) mp

bruteSolve :: [Node] -> [(Float, [Node])] -> [(Float, [Node])]
bruteSolve mp [] = bruteSolve mp $ map (\l -> (0, [l])) mp
bruteSolve mp r
           | length mp == length(snd (head r)) = sortBy (comparing fst) $ r
           | otherwise = bruteSolve mp $ nextStepsForMany r mp

nextSteps :: (Float, [Node]) -> [Node] -> [(Float, [Node])]
nextSteps r n = map (\l -> ((fst r) + (distanceBetweenTwoNodes l $ last $ snd r), snd r ++ [l])) [ nn | nn <- n, nn `notElem` (snd r)]

nextStepsForMany :: [(Float, [Node])] -> [Node] -> [(Float, [Node])]
nextStepsForMany ar n = concat $ map (\l -> nextSteps l n) ar

The code itself works. But the strange things is that although the functions bruteSolve and bruteSolveWithStartAndEnd have the same "result" type, the result of bruteSolveWithStartAndEnd can not be printed in GHSi whilst the result of bruteSolve can:

*> bruteSolve [(Node 0 1), (Node 2 3), (Node 4 5)] []
[(5.656854,[Node {x = 0, y = 1},Node {x = 2, y = 3},Node {x = 4, y = 5}]),(5.656854,[Node {x = 4, y = 5},Node {x = 2, y = 3},Node {x = 0, y = 1}]),(8.485281,[Node {x = 0, y = 1},Node {x = 4, y = 5},Node {x = 2, y = 3}]),(8.485281,[Node {x = 2, y = 3},Node {x = 0, y = 1},Node {x = 4, y = 5}]),(8.485281,[Node {x = 2, y = 3},Node {x = 4, y = 5},Node {x = 0, y = 1}]),(8.485281,[Node {x = 4, y = 5},Node {x = 0, y = 1},Node {x = 2, y = 3}])] 


*> bruteSolveWithStartAndEnd (Node 0 0) (Node 5 5) [Node 0 0, Node 1 1, Node 2 2, Node 3 3, Node 4 4, Node 5 5]

<interactive>:1:1:
    No instance for (Show ([(Float, [Node])] -> [(Float, [Node])]))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for
      (Show ([(Float, [Node])] -> [(Float, [Node])]))
    In a stmt of an interactive GHCi command: print it

What am I doing wrong? I changed something (don't remember what) and since then this error comes up. From my understanding (first day of Haskell) the print function will check whether or not the [..] derives Show and then recursively all the inner elements (in this case list->tuple->(Float & Node))... Anyways since "result" declarations are the same, why this difference?

Cheers!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2024-11-08 22:48:33

(Show ([(Float, [Node])] -> [(Float, [Node])])) 的实例没有表明您正在尝试打印函数。这可能不是您想要做的...您是否忘记了 bruteSolveWithStartAndEnd 的参数?

No instance for (Show ([(Float, [Node])] -> [(Float, [Node])])) is saying that you're trying to print a function. That probably isn't what you want to do ... did you forget an argument to bruteSolveWithStartAndEnd?

白首有我共你 2024-11-08 22:48:33

您没有向 bruteSolveWithStartAndEnd 提供足够的参数。像这样的查询

bruteSolveWithStartAndEnd (Node 0 0) (Node 5 5) [Node 0 0, Node 1 1, Node 2 2, Node 3 3, Node 4 4, Node 5 5] []

应该可以工作。

You are not suppliing enough arguments to bruteSolveWithStartAndEnd. A query like

bruteSolveWithStartAndEnd (Node 0 0) (Node 5 5) [Node 0 0, Node 1 1, Node 2 2, Node 3 3, Node 4 4, Node 5 5] []

should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文