Created
April 15, 2014 16:29
-
-
Save dripolles/10745634 to your computer and use it in GitHub Desktop.
1HaskellADay 2014/04/15
This file contains 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
-- http://lpaste.net/revision/5805301582249590784 | |
import Control.Arrow | |
import Data.Set as S (Set, empty, insert, member) | |
lowestFreeInt :: [Int] -> Int | |
lowestFreeInt = fst . foldl f (0, S.empty) where | |
f (n,s) x = (if (n == x) then nextFreeInt (n+1) s else n, S.insert x s) | |
lowestFreeInt' :: [Int] -> Int | |
lowestFreeInt' = fst . foldl f (0, S.empty) where | |
f (n,s) x = nextFreeInt n &&& id $ S.insert x s | |
nextFreeInt :: Int -> S.Set Int -> Int | |
nextFreeInt n s = head $ dropWhile (flip S.member s) [n..] | |
main = do | |
print $ lowestFreeInt $ [0..9] ++ [15,14..11] | |
print $ lowestFreeInt $ [0..9] ++ [15,14..10] | |
print $ lowestFreeInt' $ [0..9] ++ [15,14..11] | |
print $ lowestFreeInt' $ [0..9] ++ [15,14..10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment