[Haskell-cafe] data/newtype vs. class
martin
martin.drautzburg at web.de
Mon Jun 16 21:03:06 UTC 2014
Am 06/16/2014 09:28 PM, schrieb Miguel Mitrofanov:
>
> Now, you can just as easy implement all that without classes:
>
>> data AppendNumber a = AppendNumber {appendNumber :: a -> Integer -> a}
>> appendNumberInteger :: AppendNumber Integer
>> appendNumberInteger = AppendNumber {appendNumber = \n d -> n + d}
>> appendNumberString :: AppendNumber String
>> appendNumberString = AppendNumber {appendNumber = \s d -> s ++ show d}
>> increment :: AppendNumber a -> a -> a
>> increment an n = appendNumber an n 1
>> main = print $ increment appendNumberInteger 2048
I just stumbled across some other things:
When I use classes I can implement one function in terms of another function of the same class. When I use data/newtype
I can't seem to do this. So I can replace a class by a data/newtype only when it wraps around a single function.
Also I can use as many parameters as I like in data/newtype, but not in classes (if I stick to standard haskell)
Is this correct?
More information about the Haskell-Cafe
mailing list