/ Published in: Haskell
Improved version, using lenses instead of the dangerous (!!) operator for list access.
- module BinarySearch where
- import Control.Lens
- import Data.Maybe
- a :: [Int]
- a = [6, 13, 14, 25, 33, 43, 51, 53, 64, 72, 84, 93, 95, 96, 97]
- bsearch _  = Nothing
- bsearch key xs
- | key < fromJust val = bsearch key (take (mid-1) xs)
- | key > fromJust val = bsearch key (drop (mid+1) xs)
- | otherwise = val
- val = xs ^? ix mid
You need to login to post a comment.