What does `Forky (a -> b) means in type declaration?
This is from some exercise:
(<*>) :: Forky (a -> b) -> Forky a -> Forky b
where Forky is defined as
data Forky a = Tip a | Branch (Forky a) (Forky a) deriving (Eq, Show)
Usually, I see
(a -> b) as a function in the type declaration. But what does
Forky (a -> b) means?
a -> bis a type like any other. Substituting it into the
Forkydeclaration means that the first argument to your
<*>operator will be either a
f :: a -> b) or a
Branchconsisting of two
Forky (a -> b).
Forky (a -> b)is just like a
Forky Int, except instead of containing integers at the leaves, it contains functions.
a -> bis still a function type; here, the type constructor
Forkyis applied to it to get another type. Since
Forky ais basically a type of binary trees containing
Forky (a -> b)is a tree containing functions.
Note the similarities:
data Forky a = Tip a | Branch (Forky a) (Forky a) data Tree a = Leaf a | Node (Tree a) (Tree a)
This is a list of three functions
funcs :: [Int -> Int] funcs = [ abs, \x -> 2*x+1, (`mod` 16) ]
Here's a function that maybe returns a function:
adder :: Int -> Maybe (Int -> Int) adder n | n > 0 = Just (\m -> n + m) | otherwise = Nothing
Here's a function that takes a maybe function as an argument:
applyMay :: Maybe (a -> a) -> a -> a applyMay Nothing x = x applyMay (Just f) x = f x