[Haskell-beginners] Generalised data constructor matching

Colin Campbell-McPherson cora.nightshade at gmail.com
Fri Sep 4 08:01:47 EDT 2009


Hi Chaps,

I've been trying to write a function that would get the name of most  
data that is an instance of class "Animal". This getName function  
could then be overwritten for data that doesn't follow the normal  
pattern for "Animal" data types. So the getName defined in the Animal  
class would be a generic, or default implementation.

I've written a couple of  examples that don't use classes, but that I  
hope expresses what I'm trying to accomplish. In the first example  
getName needs to be defined for Dogs and Birds, even though they're  
essentially identical. The tries to define a more general function  
that works for both Birds and Dogs, and anything else that might come  
along.

The first examples works, the second gives a "Parse error in pattern"  
error.

EXAMPLE 1

data Animal = Person String String | Dog String | Bird String deriving  
Show
getName :: Animal -> String
getName (Person firstName lastName) = firstName ++ " " ++ lastName
getName (Dog name) = name
getName (Bird name) = name

logan = Person "Logan" "Campbell"
ebony = Dog "Ebony"
poly  = Bird "Poly"

main = do
  putStrLn $ show $ getName logan
  putStrLn $ show $ getName ebony
  putStrLn $ show $ getName poly


EXAMPLE 2

data Animal = Person String String | Dog String | Bird String deriving  
Show
getName :: Animal -> String
getName (Person firstName lastName) = firstName ++ " " ++ lastName
getName (_ name) = name

logan = Person "Logan" "Campbell"
ebony = Dog "Ebony"
poly  = Bird "Poly"

main = do
  putStrLn $ show $ getName logan
  putStrLn $ show $ getName ebony
  putStrLn $ show $ getName poly

I have a background with Ruby and Erlang, so if drawing from concepts  
in either of those languages would help explain please do so. I'm also  
quite new to haskell so if i've used the wrong terms, or I'm trying to  
apply concepts where they don't belong, sorry about that.


TLDR: How can I write a generic function that will work on most  
instances of a class, but be over over-written for instances of that  
class that deviate from the normal structure (ie. a data constructor  
with two params, rather than one).

Many thanks,
Colin


More information about the Beginners mailing list