Newbie question: Separating a string of alphanumeric into alphas and digits

24 views
Skip to first unread message

Dudley Brooks

unread,
Jun 26, 2012, 6:03:28 PM6/26/12
to baha...@googlegroups.com
I need to process a string like "46a12b159c" into ["46", "a", "12", "b", "159", "c"].  Functions like (split) which split on a character eliminate that character from the result, and filtering on (isAlpha) or (isDigit) would give me "4612159" or "abc" instead.  I could write this function myself, but I'm hoping that there's a pre-existing function -- presumably a more general one which, in this case, would take (isAlpha) and/or (isDigit) as inputs.  Presumably something with type String -> (char -> Bool) -> [String], or String -> (char-> Bool) -> (Char -> Bool) -> [String].

(This is to be mapped over a list of strings, and each resulting list of strings is to have another function mapped over it.  Not that this has anything to do with what the function I'm asking about needs to be.)

Thanks.

BTW, when I tried sending this as e-mail I got the bounce notice "We're writing to let you know that the group you tried to contact (bahaskell) may not exist, or you may not have permission to post messages to the group."  What gives?

Aaron D. Ball

unread,
Jun 26, 2012, 6:24:22 PM6/26/12
to baha...@googlegroups.com
If you have only digits and alphas, groupBy will do it:

Prelude> :m +Data.List
Prelude Data.List> s
"46a12b159c"
Prelude Data.List> :m +Char
Prelude Data.List Char> groupBy (\a b -> isDigit a == isDigit b) s
["46","a","12","b","159","c"]

> --
> You received this message because you are subscribed to the Google Groups
> "Bay Area Haskell Users Group" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/bahaskell/-/KvdbLJ89YYsJ.
> To post to this group, send email to baha...@googlegroups.com.
> To unsubscribe from this group, send email to
> bahaskell+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/bahaskell?hl=en.

Richard Smith

unread,
Jun 26, 2012, 6:42:31 PM6/26/12
to baha...@googlegroups.com
On Tue, Jun 26, 2012 at 3:24 PM, Aaron D. Ball <aaron...@gmail.com> wrote:
If you have only digits and alphas, groupBy will do it:

Prelude> :m +Data.List
Prelude Data.List> s
"46a12b159c"
Prelude Data.List> :m +Char
Prelude Data.List Char> groupBy (\a b -> isDigit a == isDigit b) s
["46","a","12","b","159","c"]

On Tue, Jun 26, 2012 at 3:03 PM, Dudley Brooks <dudley...@gmail.com> wrote:
> I need to process a string like "46a12b159c" into ["46", "a", "12", "b",
> "159", "c"].  Functions like (split) which split on a character eliminate
> that character from the result, and filtering on (isAlpha) or (isDigit)
> would give me "4612159" or "abc" instead.  I could write this function
> myself, but I'm hoping that there's a pre-existing function -- presumably a
> more general one which, in this case, would take (isAlpha) and/or (isDigit)
> as inputs.  Presumably something with type String -> (char -> Bool) ->
> [String], or String -> (char-> Bool) -> (Char -> Bool) -> [String].

One for the toolbox:

groupOn :: Eq b => (a -> b) -> [a] -> [[a]]
groupOn f = groupBy ((==) `on` f)

Prelude> :m + Data.Function Data.List Data.Char
Prelude Data.Function Data.List Data.Char> let groupOn f = groupBy ((==) `on` f)
Prelude Data.Function Data.List Data.Char> groupOn isDigit "46a12b159c"
["46","a","12","b","159","c"]
 
> (This is to be mapped over a list of strings, and each resulting list of

> strings is to have another function mapped over it.  Not that this has
> anything to do with what the function I'm asking about needs to be.)

map (map anotherFunction . groupOn isDigit) aListOfStrings

Nathan Howell

unread,
Jun 26, 2012, 6:15:06 PM6/26/12
to dudley...@gmail.com, baha...@googlegroups.com
`groupBy` might be what you're looking for. It compares each pair of
elements in a list and splits when a function deems the pair to be
False.

> import Data.Char (generalCategory, isAlpha, isDigit)
> import Data.List (groupBy)

> equalsOn :: Eq b => (a -> b) -> a -> a -> Bool
> equalsOn fun x y = fun x == fun y

> main :: IO ()
> main = do
> print $ groupBy (equalsOn generalCategory) "46a12b159c"
> print $ groupBy (equalsOn isDigit) "46a12b159c"
> print $ groupBy (equalsOn isAlpha) "46a12b159c"


*Main> :main
["46","a","12","b","159","c"]
["46","a","12","b","159","c"]
["46","a","12","b","159","c"]

-n

Dudley Brooks

unread,
Jun 26, 2012, 7:01:55 PM6/26/12
to baha...@googlegroups.com
Thanks, Nathan, Aaron, and Richard, that's exactly what I'm looking for.  If I had thought up possible names better, I would have searched on "group..." rather than "split...".  ;^)

And now ... what do I have to do to be able to post here by e-mail?  I keep getting bounce notices.

-- Dudley

Sean Corfield

unread,
Jun 26, 2012, 7:13:35 PM6/26/12
to baha...@googlegroups.com
On Tue, Jun 26, 2012 at 4:01 PM, Dudley Brooks <dudley...@gmail.com> wrote:
> And now ... what do I have to do to be able to post here by e-mail? I keep
> getting bounce notices.

My guess is your email client is sending from an address other than
the one you are subscribed under (dudley...@gmail.com).
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

Sam Stokes

unread,
Jun 26, 2012, 7:15:39 PM6/26/12
to baha...@googlegroups.com
Richard, thanks for pointing out Data.Function.on - I've reinvented
that a few times, each time feeling like it ought to already exist.
Pleased to see that it did!

--
Sam Stokes
Tech Lead, Rapportive
LinkedIn

@samstokes
http://linkedin.com/in/samstokesuk
http://rapportive.com
http://www.samstokes.co.uk
> --
> You received this message because you are subscribed to the Google Groups
> "Bay Area Haskell Users Group" group.

Dudley Brooks

unread,
Jun 26, 2012, 10:33:55 PM6/26/12
to baha...@googlegroups.com


On Tuesday, June 26, 2012 4:13:35 PM UTC-7, Sean Corfield wrote:
On Tue, Jun 26, 2012 at 4:01 PM, Dudley Brooks wrote:
> And now ... what do I have to do to be able to post here by e-mail?  I keep
> getting bounce notices.

My guess is your email client is sending from an address other than
the one you are subscribed under.

Good guess!  Thanks. Sean.

Rats!  I didn't mean to subscribe under that address, but my e-mail program had gotten reset before I subscribed.  Guess I'll have to unsubscribe and subscribe again as the "real" me! 

Max Cantor

unread,
Jun 27, 2012, 12:10:04 AM6/27/12
to baha...@googlegroups.com
A little off subject, but Sam, is LinkedIn doing a lot of Haskell development?  I thought it was a very conventional java house.

--
You received this message because you are subscribed to the Google Groups "Bay Area Haskell Users Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/bahaskell/-/tyKAK6r2WY0J.

Sam Stokes

unread,
Jul 2, 2012, 9:01:28 PM7/2/12
to baha...@googlegroups.com
Sorry for the late reply. I don't know of anyone at LinkedIn using
Haskell (I use it for personal projects), but there's a lot of Scala,
and there are teams using Python, Ruby, Clojure and Node (see e.g.
http://www.slideshare.net/phegaro/linkedin-mobile-how-do-we-do-it).
Enough of us are waving the polyglot flag now that a lot of the
internal tools are being made nicely language-agnostic.

I can't go into detail on a public mailing list, but feel free to mail
me directly if you're interested.
--
Sam Stokes
Tech Lead, Rapportive
LinkedIn

@samstokes
http://linkedin.com/in/samstokesuk
http://rapportive.com
http://www.samstokes.co.uk


Reply all
Reply to author
Forward
0 new messages