Skip to content

Instantly share code, notes, and snippets.

View benperez's full-sized avatar

Benjamin Perez benperez

  • Engine ML
  • San Francisco, CA
View GitHub Profile
--dropWhereRepeated [1, 2, 2, 3, 3, 5, 7, 10]
--[1,5,7,10]
dropWhereRepeated :: (Eq a) => [a] -> [a]
dropWhereRepeated [] = []
dropWhereRepeated [x] = [x]
dropWhereRepeated (x : y : xs)
| x == y = dropWhereRepeated (dropWhile (== x) (xs))
| otherwise = x : dropWhereRepeated (y : xs)
@benperez
benperez / binsearch.hs
Last active September 17, 2015 02:43
Haskell Recipes
import Control.Applicative ((<$>), (<*>))
-- Binary Search
-- find the index of the given integer in the given sorted list
binsearch :: Int -> [Int] -> Maybe Int
binsearch _ [] = Nothing
binsearch v l = case v `compare` middleValue of
EQ -> Just middleIndex
GT -> (+) <$> Just (middleIndex + 1) <*> binsearch v (take middleIndex l)
LT -> binsearch v $ drop (middleIndex + 1) l