-- A Haskell program for finding all needles in a haystack, as long as the needles and the haystack
-- are both strings.
import System.Environment(getArgs)
import Data.List(tails, isPrefixOf, sortOn)
-- lame O(ST) algorithm:
-- findAll t s = map fst $ filter (\(i, t') -> s `isPrefixOf` t') (zip [0..] (tails t))
-- still O(ST), if not worse, but using a fancy **suffix array**!!!!
findAll t = \s -> filter (\i -> s `isPrefixOf` drop i t) suffixArray
where suffixArray = map fst $ sortOn snd $ (zip [0..] (tails t))
-- The idea is, you would use arrays instead of lists, and then the suffix array
-- can be used to make each search O(S log T) or even, if you think a little
-- harder about it, O(S + log T).
main = do
[haystack, needle] <- getArgs
putStrLn $ show $ findAll haystack needle