
Thanks for your help Daniel - I am clarifying my message Daniel Fischer wrote:
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.
I would like to see an example of passing constructors as arguments. I am still getting familiar with constructs like:
getProperty ( a -> b) -> [ContactProperty] -> b
I am not sure how to test the Constructor passed as the argument. Do I say the following:
getProperty c ((c v:_) = v getProperty c ((_:xs) = getProperty c xs ..
I have tried doing this but GHC gives me parse errors. There is Haskell syntax that I don't know yet that I need to learn.
As a wild guess, maybe you should use labelled records,
data Contact = Contact { firstName :: FirstName , lastName :: LastName , ... }
and you have your selector-functions.
thanks - very useful
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