By writing idiomatic functional code in Clojure [1], how can you write a function that separates a string by spaces, but keeps the encoded phrases intact? A quick fix, of course, should use regular expressions, but it should be possible without them. With a quick glance, it looks rather complicated! I wrote similar ones in imperative languages, but I would like to see how a functional, recursive approach works.
A quick check on what our function should do:
"Hello there!" -> ["Hello", "there!"] "'A quoted phrase'" -> ["A quoted phrase"] "'a' 'b' cd" -> ["a", "b", "c", "d"] "'ab' 'c d'" -> ["ab", "cd"] "Mid'dle 'quotes do not concern me'" -> ["Mid'dle", "quotes do not concern me"]
I don't mind if the spacing changes between quotation marks (so you can use simple space separation first).
"'lots of spacing' there" -> ["lots of spacing", "there"] ;is ok to me
[1] This question can be answered at a general level, but I think that the functional approach in Clojure can easily be translated into Haskell, ML, etc.
functional-programming recursion clojure
progo
source share