This instance can be made more general without changing the code; change the first line to
instance Num a => Num (e -> a) where
I think this version doesn't even require FlexibleInstances.
This lets you do
f x = if x then 2 else 3
g x = if x then 5 else 10
-- f + g = \x -> if x then 7 else 13
-- ryan
Hi,If you are feeling adventurous enough, you can define a num instance for functions:{-# LANGUAGE FlexibleInstances #-}instance Num a => Num (a -> a) wheref + g = \ x -> f x + g xf - g = \ x -> f x - g xf * g = \ x -> f x * g xabs f = abs . fsignum f = signum . ffromInteger = const . fromInteger
ghci> let f x = x * 2ghci> let g x = x * 3ghci> (f + g) 315ghci> (f+g+2) 217
HTH,OzgurOn 19 March 2012 16:57, <sdiyazg@sjtu.edu.cn> wrote:
By arithmetic I mean the everyday arithmetic operations used in engineering.
In signal processing for example, we write a lot of expressions like f(t)=g(t)+h(t)+g'(t) or f(t)=g(t)*h(t).
I feel it would be very natural to have in haskell something like
g::Float->Float
--define g here
h::Float->Float
--define h here
f::Float->Float
f = g+h --instead of f t = g t+h t
--Of course, f = g+h is defined as f t = g t+h t
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe