Mutually recursive structures

Timothy Docker timd@macquarie.com.au
Wed, 18 Oct 2000 08:25:58 +1100 (EST)


Tom Pledger writes:

 > Timothy Docker writes:
 >  > [...] How can I do this in Haskell? If I don't have mutable
 >  > references, I figure that I must need to use laziness in some way,
 >  > perhaps similar to how I would build an infinite structure.
 > 
 > http://www.mail-archive.com/haskell@haskell.org/msg06321.html
 > 

To be honest, I found this code quite confusing, I think because
of the way in which a the "tail" needs to be joined back to the
"head" in creating a circular data structure.

I did eventually come up with a solution that seems straightforward
enough, although I have no idea of its efficiency...

  | type ParseOutput = [(String,Maybe String)]
  | 
  | data Obj = Obj { oname::String,
  | 		 oparent::(Maybe Obj),
  | 		 ochildren::[Obj] }
  | 
  | convert:: ParseOutput -> [Obj]
  | convert output = converted
  |     where converted = map mkObj output
  | 	  mkObj (name,parent) = (Obj name
  | 				     (fmap (findObj converted) parent)
  | 				     (filter (hasParentNamed name) converted) )
  | 
  | findObj:: [Obj] -> String  -> Obj
  | findObj [] name      = error ("No object with name "++name)
  | findObj (o:os) name  | name == (oname o) = o
  | 		     | otherwise         = findObj os name
  | 
  | hasParentNamed :: String -> Obj -> Bool
  | hasParentNamed name obj = maybe False ((==name).oname) (oparent obj)
  | 

Thanks for the pointer.

Tim