[Haskell-cafe] Passing Constructors as arguments

Daniel Fischer daniel.is.fischer at web.de
Wed Apr 13 10:49:39 EDT 2005


Am Mittwoch, 13. April 2005 15:43 schrieb Ralph Hodgson:
> 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
<snip>
> 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.
>
Your code works fine. 
I'm not sure, what your problem is.
Given type-correctness, data constructors can be passed as arguments to 
functions like any other function. Probably that's not your question, 
however. 
As a wild guess, maybe you should use labelled records,

data Contact = Contact
                     { firstName :: FirstName
                     , lastName :: LastName
                     , ...
                     }

and you have your selector-functions.

And it's possible to define partial contacts as

me = Contact{firstName="Daniel", lastName="Fischer"}

-- just don't ask for my phone-number or anything else which is undefined.

If I'm far off, state your problem more precisely.

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

Cheers,
Daniel



More information about the Haskell-Cafe mailing list