I suppose you want a destructor, ie a function extracting the first name from a property for example. You may want to have a look there :
http://haskell.org/hawiki/DecoratingStructures
Pierre
Ralph Hodgson a �crit :
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 -> FirstNamen > 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
-- Pierre Barbier de Reuille
INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France
tel : (33) 4 67 61 65 77 fax : (33) 4 67 61 56 68 _______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
