[Haskell-cafe] Trouble understanding records and existential types

John Ky newhoggy at gmail.com
Thu Jan 25 02:08:30 EST 2007


On 1/25/07, Brandon S. Allbery KF8NH <allbery at ece.cmu.edu> wrote:
>
> I'm probably missing something, but:
>
> (a) Why not:
>
> data ANode = Branch { name :: String, description :: String,
> children :: [AnyNode] }
>             | Leaf { name :: String, value :: String } -- this reuse
> is legal
> -- leaving Node available if you still need it


Would I be able to this?

   getLeaves :: ANode -> [Leaf]

If not, is it the case that people generally don't bother and do this
instead?

   getLeaves :: ANode -> [ANode]

(b) I think you *can* do this with a class:
>
> class Node a where
>    name :: a -> String
>
> data Branch = Branch { brName :: String, ... }
> data Leaf = Leaf { lName :: String, ... }
>
> instance Node Branch where
>    name = brName
>
> instance Node Leaf where
>    name = lName
>

Okay, though it's a lot more wordy.

-John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070125/1712ecbe/attachment.htm


More information about the Haskell-Cafe mailing list