praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

Uniques.hs (724B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 
      3 import qualified Data.Set as S
      4 import qualified Data.List as L
      5 
      6 allUniqueFast :: String -> Bool
      7 allUniqueFast = loop mempty where
      8   loop set input = case input of
      9     []           -> True
     10     (char:chars) ->
     11          not (S.member char set)
     12       && loop (S.insert char set) chars
     13 
     14 allUniqueSlow :: String -> Bool
     15 allUniqueSlow = loop Nothing . L.sort where
     16   loop acc input = case input of
     17     []           -> True
     18     (char:chars) -> case acc of
     19       Nothing   -> loop (Just char) chars
     20       Just prev ->
     21              (char /= prev)
     22           && loop (Just char) chars
     23 
     24 pour :: [a] -> [a]
     25 pour = loop mempty where
     26   loop acc input = case input of
     27     []    -> acc
     28     (h:t) -> loop (h:acc) t
     29