1
Fork 0
This repository has been archived on 2025-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
coalgebra-homework/Search.hs

16 lines
554 B
Haskell

module Search where
import Control.Monad.Instances
import Coalgebra
-- from http://math.andrej.com/2008/11/21/a-haskell-monad-for-infinite-search-in-finite-time/
-- Here we search for elements x such that p x = True
-- If there is none, it may lie
newtype Searchable a = S {find :: (a -> Bool) -> a}
search :: Searchable a -> (a -> Bool) -> Maybe a
search xs p = let x = find xs p in if p x then Just x else Nothing
forsome, forevery :: Searchable a -> (a -> Bool) -> Bool
forsome xs p = p(find xs p)
forevery xs p = not(forsome xs (\x -> not(p x)))