0
votes

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!

2

2 Answers

3
votes

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?

2
votes

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.