Skip to content

Instantly share code, notes, and snippets.

@brool
Created February 24, 2009 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brool/69686 to your computer and use it in GitHub Desktop.
Save brool/69686 to your computer and use it in GitHub Desktop.
import Control.Monad.State
import System.Random
first lst = first' lst [] []
first' [] _ result = reverse result
first' (h:t) found result =
if any (== h) found then first' t found (False:result)
else first' t (h:found) (True:result)
state_first' [] = return []
state_first' (h:t) = do current <- get
let found = (any (== h) current)
put (if found then current else (h:current))
rest <- state_first' t
return $ (not found):rest
-- isfound h = do current <- get
-- return (any (== h) current)
state_first lst = evalState (state_first' lst) []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment