KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
In the comments of the question Tacit function composition in Haskell , people mentioned making a Num instance for a -> r , so I thought I'd play with using function notation to represent multiplication: {-# LANGUAGE TypeFamilies #-} import Control.Applicative instance Show (a->r) where -- not needed in recent GHC versions show f = " a function " instance Eq (a->r) where -- not needed in recent GHC versions f == g = error "sorry, Haskell, I lied, I can't really compare functions for equality" instance (Num r,a~r) => Num (a -> r) where (+) = liftA2 (+) (-) = liftA2 (-) (*) = liftA2 (*) abs = liftA abs negate = liftA negate signum = liftA signum fromInteger a = (fromInteger a *) Note that the fromInteger definition means I can write 3 4 which evaluates to 12, and 7 (2+8) is 70, just as you'd hope. Then it all goes wonderfully, entertainingly weird! Please explain this wierdness if you can: *Main> 1 2 3 18 *Main> 1 2 4 32 *Main> 1 2 5 50 *Main> 2 2 3 36 *Main> 2 2 4 64 *Main> 2 2 5 100 *Main> (2 3) (5 2) 600 [Edit: used Applicative instead of Monad because Applicative is great generally, but it doesn't make much difference at all to the code.]
Tags (comma-separated)
Save Edits
Cancel