
On Fri, Dec 13, 2013 at 5:57 PM, Mark Fredrickson
On Fri, Dec 13, 2013 at 8:59 AM, Antonio Nikishaev
wrote: The previous exchange prompted me to whip up a Num instance for lists:
instance Num a => Num [a] where negate = fmap negate (+) = zipWith (+) (*) = zipWith (+) fromInteger x = [fromInteger x] abs = fmap abs signum = fmap signum
Wouldn't it make more sense to have fromInteger be `repeat . fromInteger`? This would make it an instance for ZipList, not list. You could even give a general instance for applicatives: instance (Num a, Applicative f) => Num (f a) where negate = fmap negate (+) = liftA2 (+) (*) = liftA2 (*) fromInteger = pure . fromInteger abs = fmap abs signum = fmap signum For ZipList this behaves like your instance, except fromInteger produces an infinite list like 'repeat . fromInteger'. The list instance instead computes all possiblities: ghci> [1,2,3] + [4,5,6,7] [5,6,7,8,6,7,8,9,7,8,9,10] Erik