0

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!

3
  • 5
    This seems like homework and your user name seems like a throw-away account. Commented Nov 30, 2010 at 20:26
  • 2
    is that meant to be "Some sentence1,...? Commented Nov 30, 2010 at 20:30
  • 3
    have you googled for string split haskell? Because I found an answer that way. Commented Nov 30, 2010 at 20:35

3 Answers 3

3

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.

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

Comments

2

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.)

Comments

0
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]

1 Comment

Wow, I got downvoted for an answer that's not even totally correct. :-)

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.