Skip to content

Instantly share code, notes, and snippets.

@lewurm
Created July 9, 2012 08:07
Show Gist options
  • Save lewurm/3074977 to your computer and use it in GitHub Desktop.
Save lewurm/3074977 to your computer and use it in GitHub Desktop.
haskell bruteforce
brute
brute.hi
brute.o
http://stackoverflow.com/questions/11391377/bruteforce-with-lazy-evaluation-and-memory-consumption
import Data.Maybe
bruteforce :: (a -> Bool) -> [a] -> Maybe a
bruteforce f xs
| null result = Nothing
| otherwise = Just $ head result
where
result = mapMaybe bruteforce' xs
-- test one instance
bruteforce' x
| f x = Just x
| otherwise = Nothing
generatorString :: Int -> [String]
generatorString 0 = [""]
generatorString deep = concatMap (\x -> map (\ys -> (x:ys)) nextgen) ['a'..'z']
where nextgen = generatorString (deep - 1)
main :: IO ()
main = do
putStrLn $ fromJust $ bruteforce ((==) "zabcde") (generatorString 6)
SHELL := bash
all: brute
brutenolimit: all
./brute +RTS -s
brute800: all
./brute +RTS -s -M800M
brute200: all
./brute +RTS -s -M200M
brute: brute.hs
ghc --make -O2 $< -o $@ -rtsopts
.PHONY: all clean brutenolimit brute200 brute800
clean:
rm -rf brute{,.o,.hi}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment