Skip to content

Instantly share code, notes, and snippets.

@joe-warren
Created October 12, 2021 18:27
Show Gist options
  • Select an option

  • Save joe-warren/be54367d3405a1d8f48093aef1277594 to your computer and use it in GitHub Desktop.

Select an option

Save joe-warren/be54367d3405a1d8f48093aef1277594 to your computer and use it in GitHub Desktop.
import Data.List (minimumBy)
import Data.Function (on)
nearestInc :: Int -> [Int] -> Int
nearestInc n xs =
case span (<n) xs of
([], h:_) -> h
(ss, []) -> last ss
(ss, l:_) -> minimumBy (compare `on` abs . (`subtract` n)) [last ss, l]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment