O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Use Stack to create a new project working-with-functors with the simple template:
        stack new working-with-functors simple
  1. Open src/Main.hs in the editor. We will use this file to demonstrate the usage of Functors.
  2. After initial module definition for Main, import the module that includes the Functor type class:
        import Data.Functor
  1. 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
  1. Functor f is a type class that needs  fmap :: (a -> b) -> f a -> f aData.Functor defines a function <$> synonymous to fmapList defines an instance for Functor. We will use a square function to apply ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required