[Haskell-cafe] Passing Constructors as arguments

Ralph Hodgson ralphtq at mac.com
Wed Apr 13 09:43:55 EDT 2005


I am learning Haskell and have set a small exercise for myself on a 
frames and slots program.
Would appreciate some help on how to pass constructors of data 
structures as arguments to a function.

Thanks,

-- Ralph
__________

A Frames test

 > module Frames
 > where 

Define frame slots:

 > type FirstName = String     -- first name
 > type LastName    = String   -- last name
 > type Organization = String
 > type Email = String
 > type WorkPhone = String
 > type CellPhone = String
 > type TelephoneNumber = String

Define slots for a contact

 > data ContactProperty = FN FirstName
 >           | LN LastName
 >           | OR Organization
 >           | EM Email
 >           | WP TelephoneNumber
 >           | MP TelephoneNumber
 >     deriving (Show, Eq)

 > data Contact = Contact [ContactProperty]
 >     deriving (Show, Eq)

 > type Contacts = [ Contact]

Now I need a way to extract properties from the frame. Start by testing 
pattern matching
without using parameters. Then I need to find out how to pass a 
constructor as a parameter.

 > getProperty:: [ContactProperty] -> FirstName
 > getProperty ((FN fn):_) = fn
 > getProperty     (_:xs)  = getProperty xs
 > getProperty          [] = "unknown"

 > firstName:: Contact -> FirstName
 > firstName (Contact cpl) = getProperty cpl


Define Contacts

 > c1::Contacts
 > c1 =
 >  [ ( Contact  [(FN "Ralph"),(LN "Hodgson"),(OR "TopQuadrant"),(EM 
"rhodgson at topquadrant.com")]),
 >    ( Contact  [(FN "Mills"),(LN "Davis"),(EM "mdavis at project10x.com")])]

Tests

 > t1=firstName $ head c1 -- should be "Ralph"
 > t2=firstName $ last c1 -- should be "Mills"
___________________________






-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org//pipermail/haskell-cafe/attachments/20050413/dd23ca15/attachment.htm


More information about the Haskell-Cafe mailing list