
Denis' version doesn't work for names containing hyphens or apostrophes.
The original works for both.... However, the original explicitly assumes
there are always at least two names and gives erroneous data if there are
more than two. Output below shows the failure on hyphenated names.
Prelude Data.Char Control.Monad.Reader> unwords $ words $ takeWhile
isAlphaOrSpace $ dropWhile (not . isAlpha) $ " \" John Doe \" "
"John Doe"
Prelude Data.Char Control.Monad.Reader> unwords $ words $ takeWhile
isAlphaOrSpace $ dropWhile (not . isAlpha) $ " \" John Doe-Smith \"
"
"John Doe"
Tim Perry
(916) 505-3634
On Thu, Jun 6, 2013 at 4:06 PM, Denis Kasak
On 7 June 2013 00:36, Costello, Roger L.
wrote: Hi Folks,
I have a string that contains a person's name. [snip] Here is an example string:
s = " \" John Doe \" "
After processing, I should have:
John Doe
Below is my implementation. Is there is a shorter and more efficient implementation? [snip code]
How about this?
import Data.Char import Control.Monad.Reader
isAlphaOrSpace = liftM2 (||) isAlpha isSpace
-- alternatively, -- isAlphaOrSpace x = isAlpha x || isSpace x -- if you find this more readable
s = " \" John Doe \" "
fs = unwords . words . takeWhile isAlphaOrSpace . dropWhile (not . isAlpha) $ s
-- Denis Kasak
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners