user525668
user525668

Reputation: 21

haskell splitting a string

I have a string like this:

"Some sentence1. Some sentence2, Some sentence3, Some sentence4....."

and I would like to turn it into a list of string

["Some sentence1", "Some sentence2", "Some sentence3.".......]

so far this is what I have:

foo :: String -> [String]
foor [] = []
| x /= ',' = [[x]] ++ foo xs
| otherwise = [[x] ++ foo xs]

which doesn't compile, much less work.

Help!

Upvotes: 0

Views: 826

Answers (3)

ephemient
ephemient

Reputation: 204974

import Data.Char
import Data.List

sentences :: String -> [String]
sentences str =
    case break isPunctuation str of
      (s1, _:s2) -> s1 : sentences (dropWhile isSpace s2)
      (s1, _) -> take 1 s1 >> [s1]

Upvotes: 0

stonemetal
stonemetal

Reputation: 6208

Your function doesn't define x or xs. That is why it won't compile.

foo [] = []
foo(x:xs) = something 

will get you a function that will iterate over the characters of the list. Though you probably want to look at the standard library some more there is a function in there that makes this trivial.(note sorry for being vague this seems like homework so I am trying to not give it away.)

Upvotes: 2

Chuck
Chuck

Reputation: 237110

There is no x in your pattern match. That is in large part why it neither compiles nor works. But your code also does not handle periods, so it won't work correctly even once it compiles until your fix that.

Upvotes: 3

Related Questions