- Use Stack to create a new project working-with-functors with the simple template:
stack new working-with-functors simple
- Open src/Main.hs in the editor. We will use this file to demonstrate the usage of Functors.
- After initial module definition for Main, import the module that includes the Functor type class:
import Data.Functor
- Define a function to square a number. We will use it to demonstrate application of this function over several data structures:
-- Square a number square :: Num a => a -> a square x = x * x
- Functor f is a type class that needs fmap :: (a -> b) -> f a -> f a. Data.Functor defines a function <$> synonymous to fmap. List defines an instance for Functor. We will use a square function to apply ...