如何从单个字符串中提取每个可能的子字符串?我想出了一种繁琐的方法,想找一种更简单的方法。
subStrings :: String -> [String]
subStrings xs = xs : takeEl xs
takeEl :: String -> [String]
takeEl xs = nub (concat [y : (takeEl y) | y <- takeEl'])
where
takeEl' = [del y xs | y <- [0..(length xs - 1)]]
del :: Int -> [a] -> [a]
del k xs = take k xs ++ drop (k+1) xs
我想用一个例子进一步解释一下:如果我在“abc”上使用该函数,我希望它创建一个包含以下元素的列表,没有排列(如果有“ab”,则不需要“ba” )。
`["abc", "a","b","c","ab","ac","bc",""]`
所以 concat inits 。tails 是不够的,因为它不会给我“ac”。