Skip to content

Instantly share code, notes, and snippets.

@captainalan
Created April 11, 2019 15:16
Show Gist options
  • Save captainalan/3b76dbb9a11027a6e8d3e67c1ff426e9 to your computer and use it in GitHub Desktop.
Save captainalan/3b76dbb9a11027a6e8d3e67c1ff426e9 to your computer and use it in GitHub Desktop.
Haskell Practice: List Sorted on Second Element of Tuple
{- Write a program which returns a list of sorted tuples based on the
- second element:
- e.g. tuple_list = [ (a, 5), (b, 2), (c, 3), (d, 1), (e, 4) ]
- Output: [d, b, c, e, a]
-}
import Data.Tuple
import Data.List
foo :: (Ord b) => [(a, b)] -> [a]
foo xs = map fst sorted
where sorted = sortOn snd xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment