[Haskell-cafe] On to applicative
Vo Minh Thu
noteed at gmail.com
Thu Aug 26 03:07:45 EDT 2010
This is indeed the case: if you want to apply your sumsqr function or
Ivan's (\ x y z -> z * y + z), to some Functor (Maybe in this case),
you don't have to redefine them, or even to use fmap2 or fmap3: you
just have to use <$> and <*>.
E.g.:
(\ a b c -> a + b + c) <$> Just 1 <*> Just 2 <*> Just 3
2010/8/26 michael rice <nowgate at yahoo.com>
>
> Hmm... it was my understanding that the example was showing how to *avoid* having to create a lot of functions that do the same thing but have different numbers of arguments.
>
> From the Wiki page:
>
> "Anytime you feel the need to define different higher order functions to accommodate for function-arguments with a different number of arguments, think about how defining a proper instance of Applicative can make your life easier."
>
> Not so?
>
> Michael
>
>
>
> --- On Thu, 8/26/10, Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com> wrote:
>
> From: Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com>
> Subject: Re: [Haskell-cafe] On to applicative
> To: "michael rice" <nowgate at yahoo.com>
> Cc: haskell-cafe at haskell.org
> Date: Thursday, August 26, 2010, 2:50 AM
>
> On 26 August 2010 16:47, michael rice <nowgate at yahoo.com> wrote:
> >
> > OK, fmap2 works, but not fmap3. What am I not understanding?
> >
> > Michael
> >
> > import Control.Applicative
> >
> > -- f :: (a -> b -> c)
> > -- fmap :: Functor f => (d -> e) -> f d -> f e
> >
> > sumsqr :: Int -> Int -> Int
> > sumsqr i j = i*i+j*j
> >
> > -- fmap :: Functor f => f a -> f (b -> c) -- Identify d with a, and e with (b -> c)
> >
> >
> > fmap2 f a b = f `fmap` a <*> b
> > fmap3 f a b c = f `fmap` a <*> b <*> c
> > fmap4 f a b c d = f `fmap` a <*> b <*> c <*> d
> >
> >
> > -- fmap2 f a b = f <$> a <*> b
> > -- fmap3 f a b c = f <$> a <*> b <*> c
> > -- fmap4 f a b c d = f <$> a <*> b <*> c <*> d
> >
> >
> > *Main> fmap2 sumsqr (Just 3) (Just 4)
> > Just 25
> > *Main> fmap3 sumsqr (Just 3) (Just 4) (Just 5)
> >
> > <interactive>:1:6:
> > Couldn't match expected type `a2 -> b' against inferred type `Int'
> > In the first argument of `fmap3', namely `sumsqr'
> > In the expression: fmap3 sumsqr (Just 3) (Just 4) (Just 5)
> > In the definition of `it':
> > it = fmap3 sumsqr (Just 3) (Just 4) (Just 5)
> > *Main>
>
> sumsqr takes three arguments; fmap3 has type:
>
> fmap3 :: (a -> b -> c -> d) -> Maybe a -> Maybe b -> Maybe c -> Maybe d
>
> i.e. the function you pass it needs to take 3 arguments.
>
> fmap3 (\ x y z -> z * y + z) (Just 1) (Just 2) (Just 3)
>
> >
> >
> > --- On Thu, 8/26/10, Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com> wrote:
> >
> > From: Ivan Lazar Miljenovic <ivan.miljenovic at gmail.com>
> > Subject: Re: [Haskell-cafe] On to applicative
> > To: "michael rice" <nowgate at yahoo.com>
> > Cc: haskell-cafe at haskell.org
> > Date: Thursday, August 26, 2010, 2:33 AM
> >
> > On 26 August 2010 16:29, michael rice <nowgate at yahoo.com> wrote:
> > >
> > > Can you recommend an example that works?
> >
> > An example of what?
> >
> > The definitions of fmap2, etc. on that page look like they're correct.
> >
> > --
> > Ivan Lazar Miljenovic
> > Ivan.Miljenovic at gmail.com
> > IvanMiljenovic.wordpress.com
> >
>
>
>
> --
> Ivan Lazar Miljenovic
> Ivan.Miljenovic at gmail.com
> IvanMiljenovic.wordpress.com
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
More information about the Haskell-Cafe
mailing list