[Haskell-beginners] Re: Boilerplate Code

Christian Maeder Christian.Maeder at dfki.de
Wed Aug 4 04:09:47 EDT 2010


Kyle Murphy schrieb:
[...]
>         typeChecker a b = toConstr a == toConstr b

Right, that is (slightly) cleaner than using my "show" trick below.

>                 On Tue, Aug 3, 2010 at 13:51, Alex Rozenshteyn
>                 <rpglover64 at gmail.com <mailto:rpglover64 at gmail.com>> wrote:
> 
>                     That is a dirty, dirty hack.

[...]

Alternatively you could define your own enumeration type:

  data SchemeType = NumberT | BoolT | AtomT
    deriving Eq

  schemeTypeOf :: SchemeVal -> SchemeType
  schemeTypeOf v = case v of
    Number _ -> NumberT
    Bool _ -> BoolT
    Atom _ -> AtomT

  typeChecker = Data.Function.on (==) schemeTypeOf

C.

>                         >
>                         > numberP :: SchemeVal -> SchemeVal
>                         > numberP (Number _) = Bool True
>                         > numberP _          = Bool False
>                         >
>                         > boolP :: SchemeVal -> SchemeVal
>                         > boolP (Bool _) = Bool True
>                         > boolP _        = Bool False
>                         >
>                         > symbolP :: SchemeVal -> SchemeVal
>                         > symbolP (Atom _) = Bool True
>                         > symbolP _        = Bool False

[...]

>                         typeChecker s1 s2 = let f = takeWhile isAlphaNum
>                         . show in
>                           Bool $ f s1 == f s2
> 
>                         hoping that my "f" just extracts the constructor
>                         as string.




More information about the Beginners mailing list