[Haskell-cafe] Checking a value against a passed-in constructor?
danielkcook at gmail.com
Tue Jun 2 06:50:32 EDT 2009
> Yeek. Why do you want to do _that_?
Heh. I've got a parser and I want to check what I've parsed (it's an
exercise in Write Yourself a Scheme in 48 Hours).
> check (Atom _) (Atom _) = True
> check (Bool _) (Bool _) = True
> check _ _ = False
Yes I came up with this too, but it seemed ugly to create unnecessary
new values just to take them apart again.
> is_atom (Atom _) = True
> is_atom _ = False
This is nicer. It still requires listing out the possible
constructors (Bool, Atom ... the real code obviously has more). I
don't like that, because I've already listed them out once, in the type
declaration itself. Surely, I shouldn't have to list them out again?
> There are various meta-programming ("Scrap Your Boilerplate",
> "Template Haskell") approaches you can use to automate some of
You hit the nail on the head. "Why I am doing this" is because of
boilerplate. Boilerplate gives me rashes and bulbous spots on the nose.
Consider the following Ruby code:
def check(zeClass, zeValue)
This does not require a new function for every class defined in Ruby.
(To be fair, though, the class of a Ruby object tells you precious
little, compared to a Haskell type constructor).
I figured there would be a clever Haskell idiom that would give me a
similarly concise route. Does it really require Template Haskell? I can
barely parse regular Haskell as it is..
More information about the Haskell-Cafe