Created
July 9, 2012 08:07
-
-
Save lewurm/3074977 to your computer and use it in GitHub Desktop.
haskell bruteforce
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
brute | |
brute.hi | |
brute.o |
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://stackoverflow.com/questions/11391377/bruteforce-with-lazy-evaluation-and-memory-consumption |
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
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) |
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
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