
Hi Miguel,
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.
Also, how the heck does Haskell decide which "success", "failure", "augment", and "combine" to use in function "searchAll", since there are five possibilities.
MIchael
--- On Sat, 5/30/09, Miguel Mitrofanov
The following code is from Section 8.4.2, pgs. 111-112 (PDF paging) of YAHT.
It compiles fine, but upon trying it I get the following error message.
It seems to be trying to 'Show' the Computation class but I'm not sure where to put the 'Deriving'.
Michael
============
Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( graph4.hs, interpreted ) Ok, modules loaded: Main. *Main> let g = Graph [(1,'a'),(2,'b'),(3,'c'),(4,'d')] [(1,2,'p'),(2,3,'q'),(1,4,'r'),(4,3,'s')] *Main> searchAll g 1 3
<interactive>:1:0: No instance for (Show (c [Int])) arising from a use of `print' at <interactive>:1:0-14 Possible fix: add an instance declaration for (Show (c [Int])) In a stmt of a 'do' expression: print it
============================
data Failable a = Success a | Fail String deriving (Show)
data Graph v e = Graph [(Int,v)] [(Int,Int,e)]
class Computation c where success :: a -> c a failure :: String -> c a augment :: c a -> (a -> c b) -> c b combine :: c a -> c a -> c a
instance Computation Maybe where success = Just failure = const Nothing augment (Just x) f = f x augment Nothing _ = Nothing combine Nothing y = y combine x _ = x
instance Computation Failable where success = Success failure = Fail augment (Success x) f = f x augment (Fail s) _ = Fail s combine (Fail _) y = y combine x _ = x
instance Computation [] where success a = [a] failure = const [] augment l f = concat (map f l) combine = (++)
searchAll g@(Graph vl el) src dst | src == dst = success [src] | otherwise = search' el where search' [] = failure "no path" search' ((u,v,_):es) | src == u = (searchAll g v dst `augment` (success . (u:))) `combine` search' es | otherwise = search' es
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe