I'm trying to write a function of the form
f :: String -> [String]
f str = ...
that returns the list of all the strings formed by removing exactly one character from str
. For example:
ghci> f "stack"
["tack","sack","stck","stak","stac"]
Because String
and [Char]
are synonymous, I could use the index, but I know that you should avoid doing that in Haskell. Is there a better way besides using the index?
You could use recursion like so:
The Josh Kirklin's solution as a one-liner:
Maybe a more readable way to describe it is:
But practically, it is slower than the other solutions.