Skip to content

Instantly share code, notes, and snippets.

@michaelfeathers
Created May 15, 2013 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelfeathers/5584260 to your computer and use it in GitHub Desktop.
Save michaelfeathers/5584260 to your computer and use it in GitHub Desktop.
Create a zip-esque running annotation for a list based upon a predicate.
saddle :: (a->Bool) -> [a] -> [(a,a)]
saddle f [] = []
saddle f (x:xs) = saddle' f x xs
saddle' :: (a->Bool) -> a -> [a] -> [(a,a)]
saddle' f initial [] = []
saddle' f initial (x:xs) = (x, sideValue x) : saddle' f (sideValue x) xs
where sideValue x = if f x then x else initial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment