public
Created

haskell bruteforce

  • Download Gist
.gitignore
1 2 3
brute
brute.hi
brute.o
Makefile
Makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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}
README
1
http://stackoverflow.com/questions/11391377/bruteforce-with-lazy-evaluation-and-memory-consumption
brute.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.