
Am Mittwoch, 13. April 2005 17:14 schrieben Sie:
Thanks for your help Daniel - I am clarifying my message
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
This isn't an argument, by the way, it's a parameter.
say the following:
getProperty c ((c v:_) = v getProperty c ((_:xs) = getProperty c xs
You can't do it thus, a variable-pattern like c may only appear once in a function definition and "c v" isn't a legal pattern, so may not appear on the lhs of the definition. Neither may an incompletely applied constructor: Hugs mode: Restart with command line option +98 for Haskell 98 mode ERROR "./Ini.hs":29 - Constructor "Left" must have exactly 1 argument in pattern The offending line is humm Left = LT You can achieve your goal with dummy values: getProperty c@(FN d) (x:xs) = case x of FN fn -> fn _ -> getProperty c xs getProperty c@(LN d) (x:xs) = case x of LN ln -> ln _ -> getProperty c xs ... and then call getProperty (FN undefined) list. This isn't very nice, though. I'd rather do it (if labelled records aren't the thing to do) using Maybe types: firstName :: ContactProperty -> Maybe FirstName firstName (FN fn) = Just fn firstName _ = Nothing and so on, then (this depends on all properties being represented by a String, if different types were involved, it'd be more complicated) getProperty :: (ContactProperty -> Maybe String) -> [ContactProperty] -> String getProperty f xs = case catMaybes $ map f xs of [] -> "unknown" (p:ps) -> p However, this isn't nice either.
..
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.
Quite natural, I strongly recommend reading the 'Gentle Introduction to Haskell' and every now and then looking whether you already know enough Haskell to profit by reading the report. The sections on pattern matching have valuable information on the problem at hand.
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
Nice to read that :-) Cheers, Daniel