praxis

Various programming exercises.
Log | Files | Refs

commit 0ee3cac16b00e454bdd54cf3bcd4b34aa4c57c5f
parent 8ff6023634a134776667a3c24897f2bd9ba3b65a
Author: Jared Tobin <jared@jtobin.ca>
Date:   Wed, 11 Jul 2018 14:21:42 +1200

Add uniques thing.

Diffstat:
A20180711_uniques/Uniques.hs | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)

diff --git a/20180711_uniques/Uniques.hs b/20180711_uniques/Uniques.hs @@ -0,0 +1,29 @@ +{-# OPTIONS_GHC -Wall #-} + +import qualified Data.Set as S +import qualified Data.List as L + +allUniqueFast :: String -> Bool +allUniqueFast = loop mempty where + loop set input = case input of + [] -> True + (char:chars) -> + not (S.member char set) + && loop (S.insert char set) chars + +allUniqueSlow :: String -> Bool +allUniqueSlow = loop Nothing . L.sort where + loop acc input = case input of + [] -> True + (char:chars) -> case acc of + Nothing -> loop (Just char) chars + Just prev -> + (char /= prev) + && loop (Just char) chars + +pour :: [a] -> [a] +pour = loop mempty where + loop acc input = case input of + [] -> acc + (h:t) -> loop (h:acc) t +