On Jan 25, 2007, at 2:08 , John Ky wrote:

On 1/25/07, Brandon S. Allbery KF8NH <[EMAIL PROTECTED]> wrote:
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]

Leaf is a data constructor, not a type.  Your second one:

   getLeaves :: ANode -> [ANode]

is correct. If you want the type system to ensure they are only leaves, then indeed you can't use this method.

(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.

How so? You were declaring the class and instances anyway; I simply defined a new method to go into it and renamed the constructor fields to obey Haskell's rules, but you will probably be using the class method so your code won't care about the latter.

--
brandon s. allbery    [linux,solaris,freebsd,perl]     [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon university    KF8NH



_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to