DEV Community

Discussion on: Daily Challenge #215 - Difference of 2

Collapse
 
cipharius profile image
Valts Liepiņš

Took some iterations to get to this solution, but here it is.
Haskell:

import Data.Maybe (catMaybes)
import Data.List (sort)

pairDifference :: [Int] -> [(Int, Int)]
pairDifference xs = catMaybes $ f <$> sorted <*> sorted
  where
    sorted = sort xs
    f x y | y - x == 2 = Just (x, y)
          | otherwise  = Nothing