Skip to content

Instantly share code, notes, and snippets.

@YoEight
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save YoEight/9387578 to your computer and use it in GitHub Desktop.

Select an option

Save YoEight/9387578 to your computer and use it in GitHub Desktop.
My answer to http://codepad.org/roOZpzQQ exercise
import Data.Functor.Foldable
-- | takeStrictlyLessThan take elements of a list whils their sum is
-- _strictly_ less than a given number
--
-- Point-free: I didnt' try without parameter, you can easily "hide" the 2nd
-- parameter (ie. takeStrictlyLessThan x = )
-- Level: MEDIUM
--
-- Examples:
-- >>> takeStrictlyLessThan (10::Int) [1..]
-- [1,2,3]
--
-- >>> takeStrictlyLessThan (3::Integer) $ repeat 1
-- [1,1]
--
-- >>> takeStrictlyLessThan (42::Int) $ []
-- []
takeStrictlyLessThan :: (Num a, Ord a) => a -> [a] -> [a]
takeStrictlyLessThan x as = cata go as $ 0
where
go Nil _ = []
go (Cons a k) cur
| cur + a >= x = []
| otherwise = a : k (cur + a)
recursion-schemes == 4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment