A simple explanation of the <$> and <*> operators

I have to give a (simple) talk about Yesod. And yes, .. I have never and rarely used haskell. University teacher ..... yes.

So, I read a book about Damod, and in some chapters the author uses some operators like <$>and <*>. Can someone explain with easy words what these operators do? This is quite difficult for Google for these characters, and if you try to read the Control.Applicative documentation, but to be honest, it's hard to get for a haskell beginner.

so I hope someone has a simple answer for me :)

An example of a book that uses these operators:

......
personForm :: Html -> MForm Handler (FormResult Person, Widget)
personForm = renderDivs $ Person
    <$> areq textField "Name" Nothing
    <*> areq (jqueryDayField def
        { jdsChangeYear = True -- give a year dropdown
        , jdsYearRange = "1900:-5" -- 1900 till five years ago
        }) "Birthday" Nothing
    <*> aopt textField "Favorite color" Nothing
    <*> areq emailField "Email address" Nothing
    <*> aopt urlField "Website" Nothing
data Person = Person
    { personName          :: Text
    , personBirthday      :: Day
    , personFavoriteColor :: Maybe Text
    , personEmail         :: Text
    , personWebsite       :: Maybe Text
    }
  deriving Show
.....

.....................................

Hey,

, . , "" . , ( Google)

+4
4

, , , <$> <*>. ( , , , .) <$> <*> :

......
personForm :: Html -> MForm Handler (FormResult Person, Widget)
personForm = do
    name <- areq textField "Name" Nothing
    bday <- areq (jqueryDayField def
        { jdsChangeYear = True -- give a year dropdown
        , jdsYearRange = "1900:-5" -- 1900 till five years ago
        }) "Birthday" Nothing
    colour <- aopt textField "Favorite color" Nothing
    email <- areq emailField "Email address" Nothing
    url <- aopt urlField "Website" Nothing
    renderDivs $ Person name bday colour email url

, <$> <*> , .

+3

, , , , .

+9

- , . typeclasses Functor Applicative.

class Functor f where
  fmap :: (a -> b) -> (f a -> f b)

(<$>) = fmap -- synonym

class Functor f => Applicative f where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b

: Functors Applicative "" (<$>), (<*>), "" "" .

go x y                    -- works if x and y are regular values
go <$> pure x <*> pure y  -- uses `pure` to add "default" metadata
                          -- but is otherwise identical to the last one

, . "" - . " " " " "".

Monad, . Monad Applicative, (<$>) (<*>) do

do x_val <- x                     go <$> x
   y_val <- y                        <*> y
   return (go x_val y_val)

"" go , " , x, , y , go, " do.


, , Applicative -. Applicative Monoidal.

class Functor f => Monoidal f where
  init :: f ()                             -- similar to pure
  prod :: f a -> f b -> f (a, b)           -- similar to (<*>)

Monoidal Functor (a)

  init :: [()]
  init = []

  init :: Maybe ()
  init = Just ()

,

  prod :: [a] -> [b] -> [(a, b)]
  prod as bs = [(a, b) | a <- as, b <- bs]

  prod :: Maybe a -> Maybe b -> Maybe (a, b)
  prod (Just a) (Just b) = (Just (a, b))
  prod _        _        = Nothing

, Monoidal , fmap

go <$> maybeInt `prod` (maybeChar `prod` maybeBool) where
  go :: (Int, (Char, Bool)) -> Double                       -- it pure!
  go (i, (c, b)) = ...

, , (<$>) (<*>),

go <$> maybeInt <*> maybeChar <*> maybeBool where
  go :: Int -> Char -> Bool -> Double
  go i c b = ...

,

-- forward
init     = pure ()
prod x y = (,) <$> x <*> y

-- back
pure a   = const a <$> init
f <*> x  = ($) <$> prod f x

, (<*>) ($) prod uct Functor.

+6

, , <$> infix fmap. , , :

GHCi> (*2) <$> (Just 3)
Just 6
GHCi> (*2) <$> (Nothing)
Nothing
GHCi> (*3) <$> (Right 7)
Right 21
GHCi> (*2) <$> (Left "error")
Left "error"
GHCi>  (+ 1) <$> [2,4,6,8]
[3,5,7,9]

:

GHCi> (*) <$> (Just 2) <*> (Just 5)
Just 10
GHCi> (*) <$> (Just 2) <*> (Nothing)
Nothing
GHCi> (*) <$> (Right 3) <*> (Right 7)
Right 21
GHCi> (*) <$> (Left "error") <*> (Right 7)
Left "error"
GHCi> (+) <$> [1,2,3] <*> [10,20,30]
[11,21,31,12,22,32,13,23,33]
GHCi> (+) <$> [1,2,3] <*> []
[]

:

GHCi> (Just (*2)) <*> (Just 5)
Just 10
GHCi> (Right (*3)) <*> (Right 7)
Right 21
GHCi> [(+1),(+2),(+3)] <*> [10,20,30]
[11,21,31,12,22,32,13,23,33]

, , , , , (*) <$> (Just 2) <*> (Just 5) Just (2 * 5)

( , , .)

, <$> " " , , , (, Nothng, ).

<*> . , , . , (*) <$> (Right 3) <*> (Right 7) <*> (Right 4) - - * 3 7, , , 4.

, <$> <*> , . .

This can only be done if the box itself is a functor; this is a key limitation for all of this. A functor is a function for which someone has defined a function fmapthat allows you to convert it from a function applied to one type to a function applicable to another type (without changing the essential nature of the function). If you like, Monads (boxes for things) know how to transform functions so that they can be applied to their things.

+5
source

All Articles