Z
Z
Ziggy Pop2020-07-14 02:32:08
Haskell
Ziggy Pop, 2020-07-14 02:32:08

What is the type of the first (left) occurrence of the operator in the expression succ succ "abc"?

A couple of months ago, I quite successfully completed the first part of the "Functional Programming in Haskell" course from Stepik, recently took up the second part, and "broke" on the very first task:

The Data.Functor module defines the <$> operator , which is an infix analogue of the fmap function :

GHCi> :info <$>
(<$>) :: Functor f => (a -> b) -> f a -> f b
        -- Defined in `Data.Functor'
infixl 4 <$>


In the expression succ <$> "abc" this operator is of type (Char -> Char) -> [Char] -> [Char] . What is the type of the first (left) occurrence of this operator in the expression succ <$> succ <$> "abc" ?


I've been struggling with this question for probably a week now and I'm starting to feel like an idiot, but I still haven't found the right answer even despite the hints in the discussion ( https://stepik.org/lesson/28880/step/4? unit=9912 ).

Explain "on your fingers", please, how to answer this question correctly, and most importantly - how to come to the correct answer. Self-esteem has already fallen below the plinth.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mlyamasov, 2020-07-14
@ZiggyPop

Since the (<$>) operator is infixl, the original expression is equivalent to:
(succ <$> succ) <$> "abc".
Therefore (succ <$> succ) is of type (Char -> Char).
Hence the second and first succ functions in parentheses are of type (Char -> Char).
Hence the answer is: (Char -> Char) -> (Char -> Char) -> (Char -> Char).
Notice the following instance:

instance Functor ((->) r) where
    fmap = (.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question