すみません 以下のコードで
main parse errorになってしまいます
どうすればいいですか?
data BST a = Eot
| Node Int a (BST a) (BST a) -- key, val, left, right
deriving Eq
instance Show a => Show (BST a) where
show Eot = "Null"
show node = "BST" ++ (show $ bst2alist node)
-- find a record in a BST
bst_find :: BST a -> Int -> Maybe (Int, a)
bst_find Eot _ = Nothing
bst_find (Node key0 val0 left right) key
| key0 == key = Just (key0, val0)
| key0 < key = bst_find right key
| otherwise = bst_find left key
-- insert a record into a BST
bst_insert :: BST a -> (Int, a) -> BST a
bst_insert Eot (key, val) = Node key val Eot Eot
bst_insert (Node key0 val0 left right) (key, val)
| key0 == key = Node key0 val left right -- if same key value, replace val
| key0 < key = Node key0 val0 left (bst_insert right (key, val))
| otherwise = Node key0 val0 (bst_insert left (key, val)) right
-- traverse
bst_map :: (a -> b) -> BST a -> [(Int,b)]
bst_map _ Eot = []
bst_map f (Node key val left right) = bst_map f left ++ (key, f val) : bst_map f right
-- convert to an association list
bst2alist :: BST a -> [(Int, a)]
bst2alist = bst_map id
-- test data
bst1 = foldl bst_insert Eot [(5, "five"), (6,"six"), (4,"four"),
(3, "three"), (7, "seven"), (1,"One"), (10, "ten") ]
Usage: Compile the code and execute the resulting program.
It will print Dolly's maternal grandfather.
-}
-- everything you need to know about sheep
data Sheep = Sheep {name::String, mother::Maybe Sheep, father::Maybe Sheep}
-- we show sheep by name
instance Show Sheep where
show s = show (name s)
-- comb is a combinator for sequencing operations that return Maybe
comb :: Maybe a -> (a -> Maybe b) -> Maybe b
comb Nothing _ = Nothing
comb (Just x) f = f x
-- now we can use `comb` to build complicated sequences
maternalGrandfather :: Sheep -> Maybe Sheep
maternalGrandfather s = (Just s) `comb` mother `comb` father
fathersMaternalGrandmother :: Sheep -> Maybe Sheep
fathersMaternalGrandmother s = (Just s) `comb` father `comb` mother `comb` mother
mothersPaternalGrandfather :: Sheep -> Maybe Sheep
mothersPaternalGrandfather s = (Just s) `comb` mother `comb` father `comb` father
-- this builds our sheep family tree
breedSheep :: Sheep
breedSheep = let adam = Sheep "Adam" Nothing Nothing
eve = Sheep "Eve" Nothing Nothing
uranus = Sheep "Uranus" Nothing Nothing
gaea = Sheep "Gaea" Nothing Nothing
kronos = Sheep "Kronos" (Just gaea) (Just uranus)
holly = Sheep "Holly" (Just eve) (Just adam)
roger = Sheep "Roger" (Just eve) (Just kronos)
molly = Sheep "Molly" (Just holly) (Just roger)
in Sheep "Dolly" (Just molly) Nothing
-- print Dolly's maternal grandfather
main :: IO ()
main = let dolly = breedSheep
in do print (maternalGrandfather dolly)