In this recipe, we will use the Functor type class to perform some easy tasks. We will see how Functor resembles a map of a list by applying it to a variety of data structures.
Working with Functors
How to do it...
- 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...