This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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) |