I’m trying to wrap my head around functional programming concepts. Consider the problem of removing empty (zero length) Strings from a list
The following is a purely functional implementation in Haskell which is very readable even if you know nothing about the language
removeEmpty :: [String] -> [String]
removeEmpty [] = []
removeEmpty ([] :strs) = removeEmpty strs
removeEmpty (str:strs) = str : removeEmpty strs
Now consider my implementation in Scala
def removeEmpty(dirty: List[String]): List[String] = {
if (Nil == dirty)
dirty
else {
if (dirty(0).length() == 0)
removeEmpty(dirty.tail)
else
dirty.head::removeEmpty(dirty.tail)
}
}
It does the same thing but has a very procedural feel about it. Is there a more functional way to write the same method in Scala?
Dirk’s answer probably contains the better solution, but my answer is closer to the original, I think.