Skip to content

Instantly share code, notes, and snippets.

@vertexcite
Last active August 29, 2015 14:16
Show Gist options
  • Save vertexcite/8690ccf6d607cc1fe800 to your computer and use it in GitHub Desktop.
Save vertexcite/8690ccf6d607cc1fe800 to your computer and use it in GitHub Desktop.
Num instance functions: Num a => Num (a -> a)
{-# LANGUAGE FlexibleInstances #-}
module NumFunction where
instance Num a => Num (a -> a) where
(f + g) x = f x + g x
(f * g) x = f x * g x
abs f = abs . f
signum f = signum . f
fromInteger x = const (fromInteger x)
negate f = negate . f
instance Fractional a => Fractional (a -> a) where
(f / g) x = f x / g x
fromRational x = const (fromRational x)
-- Example
f x = x * x
g x = x + 2
h = f + g
u = f / g
test1 = h 0 == 2
test2 = h 1 == 4
test3 = u 0 == 0
test4 = u 1 == 1/3
testAll = and [test1, test2, test2, test4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment