1

Let be a string alphanumeric and I want to split it when he founds a number.

remove xs = [ x | x <- xs, not (x `elem` ",.?!-:;\"\'0123456789")]

I started by those numbers and somne punctuation but I how can I split the string, so I give the string "I go 25abc tomorrow 100!"->["I","go","abc","tomorrow"] !

1
  • 1
    There is Data.List.Split package containing splitOn function. Unfortunately it is not standard (hence I don't provide it as an answer), but might be good enough for you. Commented May 10, 2017 at 17:56

5 Answers 5

1
> import Data.List.Split
> split (dropDelims . dropBlanks $ oneOf " 0123456789") "I go 25abc tomorrow 100!"
["I","go","abc","tomorrow","!"]

with words a two step approach will work

> words $ map (\x -> if x `elem` ['a'..'z']++['A'..'Z'] then x else ' ') "I go 25abc tomorrow 100!"
["I","go","abc","tomorrow"]

here I changed the definition of filter to only alpha chars, if not matches your needs you can reverse the if condition by defining the filtered out chars.

By importing Data.Char(isAlpha) you can change

x `elem` ['a'..'z']++['A'..'Z'] 

to isAlpha x

Sign up to request clarification or add additional context in comments.

2 Comments

If one has the Split package, he can use splitOn.
Even I have imported Data.List.Split it's not working...But If I want to made a function like : words::String-> [String] how can I define for any string.
1

You could define a recursive function like this:

import Data.Char

split :: String -> [String]
split [] = []
split s =
  let -- Remove non alphabetic characters from front of list
      s' = dropWhile (not . isAlpha) s
      -- Split the list at the point where the first non-alphabetic
      -- character appears
      (xs,ys) = span isAlpha s'
      -- Recursively call split on the second list, while prepending 
      -- the first list.
  in  xs:(split ys)

Here is what happens with your test string through the recursive calls:

s  = "I go 25abc tomorrow 100!"
s' = "I go 25abc tomorrow 100!"
xs = "I"
ys = " go 25abc tomorrow 100!"

s  = " go 25abc tomorrow 100!"
s' = "go 25abc tomorrow 100!"
xs = "go"
ys = " 25abc tomorrow 100!"

s  = " 25abc tomorrow 100!"
s' = "abc tomorrow 100!"
xs = "abc"
ys = " tomorrow 100!"

s  = " tomorrow 100!"
s' = "tomorrow 100!"
xs = "tomorrow"
ys = " 100!"

s  = " 100!"
s' = ""
xs = ""
ys = ""

Note that this will split the word "abc4def" into ["abc", "def"].

Comments

0

Below line of code will split the string whenever it finds number

String[] strArr= str.split("[0-9]");

3 Comments

That's not Haskell?
Need to write a parser first :)
also it does not produce the list as in the given example
0

The following should do it.

splitWithoutNumbers :: String -> [String]
splitWithoutNumbers = map removeNumbers.words
                      where 
                      removeNumbers ""     = ""
                      removeNumbers (s:ss) | isNumber s = removeNumbers ss
                                           | otherwise  = s : removeNumbers ss

*Main> splitWithoutNumbers "I go 25abc tomorrow 100!"
["I","go","abc","tomorrow","!"]

Comments

0
import Data.Char
import Control.Monad
remove = words . filter (liftM2 (||) isAlpha isSpace)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.