Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Thanks to Haskell's laziness, the following implementation of `minimum` is O(n):

  import Data.List (sortBy)

  minimum :: Ord a => [a] -> a
  minimum = head . sortBy compare
`sortBy` is O(n log n). But `head` only needs the first element which can be found in linear time. So `sortBy` does not compute the rest and `minimum` is O(n).


Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: