O'Reilly logo

Haskell Design Patterns by Ryan Lemmer

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

Arrows

Let's find our way towards arrows from the perspective of monads. Consider the following IO code:

import System.IO
main = do 
main = liftM (length . words)
                   (readFile "jabberwocky.txt" )
            >>= print
-- regular functions: length, words
-- Monadic functions: readFile, print

We use liftM to lift the composed function length . words into the monadic function readFile and then feed the result to another monadic function, print.

We can compose the regular functions with (.), but we know well that we cannot do the following:

  print . length . words . readFile "jabberwocky.txt"
  -- INVALID - types don't align

Let's make the preceding code possible!

Note

The following code is based on a combination of Programming with Arrows by John Hughes, and a blog post ...

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