Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Last active August 29, 2015 14:23
Show Gist options
  • Save davidallsopp/c27f2e432c27202b1598 to your computer and use it in GitHub Desktop.
Save davidallsopp/c27f2e432c27202b1598 to your computer and use it in GitHub Desktop.
Simple examples of useful functions from Control.Arrow, via the very helpful https://en.wikibooks.org/wiki/Haskell/Understanding_arrows
module ArrowExamples where
import Control.Arrow
-- Based on https://en.wikibooks.org/wiki/Haskell/Understanding_arrows
-- function composition:
composed :: Int -> Int
composed = succ >>> (*3)
-- composed 1 = 6
-- Operations on pairs of values:
-- first and second pass through one of the elements of the pair unchanged:
incrementFirst :: (Int, Int) -> (Int, Int)
incrementFirst = first succ
-- incrementFirst (1,2) = (2,2)
--- other operations operate on both elements, but independently:
opposite :: (Int, Int) -> (Int, Int)
opposite = succ *** pred
-- opposite (1,1) = (2,0)
-- copy the input to form a pair:
fork :: Int -> (Int, Int)
fork = succ &&& pred
-- fork 1 = (2,0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment