Skip to content

Instantly share code, notes, and snippets.

@blouerat
Last active August 29, 2015 14:01
Show Gist options
  • Save blouerat/526b6ee8f03573702811 to your computer and use it in GitHub Desktop.
Save blouerat/526b6ee8f03573702811 to your computer and use it in GitHub Desktop.
1HaskellADay: appendSwapped
import Control.Applicative
import Data.Tuple (swap)
{- Given a list of tuples, return a list containing the initial elements and the swapped elements
It's an easy one to start the week.
Example
> appendSwapped [(1,2),(2,3)]
[(1,2),(2,3),(2,1),(3,2)]
-}
appendSwapped :: [(a, a)] -> [(a, a)]
appendSwapped = ([id, swap] <*>)
-- Find a similar process for an entry type: [(a,b)]
appendSwapped' :: [(a,b)] -> [Either (a,b) (b,a)]
appendSwapped' = ([Left, Right . swap] <*>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment