|
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 "[EMAIL PROTECTED]")]), > ( Contact [(FN "Mills"),(LN "Davis"),(EM "[EMAIL PROTECTED]")])] Tests > t1=firstName $ head c1 -- should be "Ralph" > t2=firstName $ last c1 -- should be "Mills" ___________________________ |
_______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
