
Hi All, I have this in my code: infixl 6 ~+, ~- f ~+ g = \p -> f p + g p f ~- g = \p -> f p - g p but I feel that the prelude must have already taken care of such things. What's the proper way to do it? Thanks in advance, Adrian.

On Mon, Mar 18, 2013 at 9:59 AM, Adrian May
infixl 6 ~+, ~- f ~+ g = \p -> f p + g p f ~- g = \p -> f p - g p
but I feel that the prelude must have already taken care of such things. What's the proper way to do it?
Not in the Prelude (which is specified by the Haskell Language Report and difficult to change), but I think you want to look at Control.Applicative. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Mon, Mar 18, 2013 at 10:15:53AM -0400, Brandon Allbery wrote:
On Mon, Mar 18, 2013 at 9:59 AM, Adrian May
wrote: infixl 6 ~+, ~- f ~+ g = \p -> f p + g p f ~- g = \p -> f p - g p
but I feel that the prelude must have already taken care of such things. What's the proper way to do it?
Not in the Prelude (which is specified by the Haskell Language Report and difficult to change), but I think you want to look at Control.Applicative.
In particular, (~+) = liftA2 (+), and similarly for (~-). This uses the ((->) e) instance of Applicative. -Brent

Thanks everyone! I knew it would be in there somewhere but I wasn't sure
where to look. I'll play with that stuff in the morning.
Adrian.
On 18 March 2013 23:23, Daniel Trstenjak
In particular, (~+) = liftA2 (+), and similarly for (~-). This uses the ((->) e) instance of Applicative.
Or by using '<$>' and '<*>', which is a bit more general:
(+) <$> f <*> g
funcWith3Args <$> f <*> g <*> h
Greetings, Daniel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

So function application itself is the functor, i.e., the box-like thing, right? Lifting a function of values to work on boxes containing those values corresponds with lifting a function of values to work on functions yielding those values. Right? In which case, fishing something out of the box corresponds with applying the function. Why not? With record notation you use a function to fish out a field anyway. That's very interesting. Now I have this: type Amount = Float type Moment = Day -- from Data.Time.Calendar data Period = Period Moment Moment type Flowfunc = Period -> Amount f0 = const 0 -- Add and subtract flowfuncs infixl 6 ~+, ~- (~+), (~-) :: Flowfunc -> Flowfunc -> Flowfunc f ~+ g = (+) <$> f <*> g f ~- g = (-) <$> f <*> g But here I still seem to be being naive again... -- Multiply flowfunc by a float infixl 7 ~* (~*) :: Float -> Flowfunc -> Flowfunc f ~* n = \p -> n * (f p) -- or with the params the other way around I guess I could write: f ~* n = (*) <$> f <*> const n but it seems clunky. Should it be a monad? How about: -- Sum a list of them sumf :: [Flowfunc] -> Flowfunc sumf l = foldl (~+) f0 l TIA, Adrian.
participants (4)
-
Adrian May
-
Brandon Allbery
-
Brent Yorgey
-
Daniel Trstenjak