<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Miguel,<br><br>That works. but it gives just a single solution [1,2,3] when there are supposed to be two [[1,2,3],[1,4,3]]. Of course the code in YAHT may be in error.<br><br>Also, how the heck does Haskell decide which "success", "failure", "augment", and "combine" to use in function "searchAll", since there are five possibilities.<br><br>MIchael<br><br>--- On <b>Sat, 5/30/09, Miguel Mitrofanov <i><miguelimo38@yandex.ru></i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Miguel Mitrofanov <miguelimo38@yandex.ru><br>Subject: Re: [Haskell-cafe] Missing a "Deriving"?<br>To: "michael rice" <nowgate@yahoo.com><br>Cc: haskell-cafe@haskell.org<br>Date: Saturday, May 30, 2009, 5:36 PM<br><br><div class="plainMail">It's trying to 'Show' the 'c [Int]' type, but doesn't know
which 'c' to use; so it's trying to find a generic instance, which doesn't exist. You can't fix this with 'deriving' or anything like this; instead, provide the type annotation like this:<br><br>*Main> searchAll g 1 3 :: Maybe [Int]<br><br>On 31 May 2009, at 00:50, michael rice wrote:<br><br>> The following code is from Section 8.4.2, pgs. 111-112 (PDF paging) of YAHT.<br>> <br>> It compiles fine, but upon trying it I get the following error message.<br>> <br>> It seems to be trying to 'Show' the Computation class but I'm not sure where to put the 'Deriving'.<br>> <br>> Michael<br>> <br>> <br>> ============<br>> <br>> Loading package ghc-prim ... linking ... done.<br>> Loading package integer ... linking ... done.<br>> Loading package base ... linking ... done.<br>> [1 of 1] Compiling Main ( graph4.hs, interpreted )<br>> Ok, modules loaded:
Main.<br>> *Main> let g = Graph [(1,'a'),(2,'b'),(3,'c'),(4,'d')] [(1,2,'p'),(2,3,'q'),(1,4,'r'),(4,3,'s')]<br>> *Main> searchAll g 1 3<br>> <br>> <interactive>:1:0:<br>> No instance for (Show (c [Int]))<br>> arising from a use of `print' at <interactive>:1:0-14<br>> Possible fix: add an instance declaration for (Show (c [Int]))<br>> In a stmt of a 'do' expression: print it<br>> <br>> ============================<br>> <br>> data Failable a = Success a | Fail String deriving (Show)<br>> <br>> data Graph v e = Graph [(Int,v)] [(Int,Int,e)]<br>> <br>> class Computation c where<br>> success :: a -> c a<br>> failure :: String -> c a<br>> augment :: c a -> (a -> c b) -> c b<br>>
combine :: c a -> c a -> c a<br>> <br>> instance Computation Maybe where<br>> success = Just<br>> failure = const Nothing<br>> augment (Just x) f = f x<br>> augment Nothing _ = Nothing<br>> combine Nothing y = y<br>> combine x _ = x<br>> <br>> instance Computation Failable where<br>> success = Success<br>> failure = Fail<br>> augment (Success x) f = f x<br>> augment (Fail s) _ = Fail s<br>> combine (Fail _) y = y<br>> combine x _ = x<br>> <br>> instance Computation [] where<br>> success a = [a]<br>> failure = const []<br>>
augment l f = concat (map f l)<br>> combine = (++)<br>> <br>> searchAll g@(Graph vl el) src dst<br>> | src == dst = success [src]<br>> | otherwise = search' el<br>> where search' [] = failure "no path"<br>> search' ((u,v,_):es)<br>> | src == u = (searchAll g v dst `augment`<br>> (success . (u:)))<br>> `combine` search' es<br>> | otherwise = search' es<br>> <br>> <br>> _______________________________________________<br>>
Haskell-Cafe mailing list<br>> <a ymailto="mailto:Haskell-Cafe@haskell.org" href="/mc/compose?to=Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>> <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br><br></div></blockquote></td></tr></table><br>