2

I'm having trouble getting this started, as in, I'm not sure what is exactly happening with this function. What it's supposed to do is take in 2 parameters, a character, and a string, then output a list of strings that are separated by that character.

For example,

break ',' "abc,def,ghi"

should output ["abc","def","ghi"]

I am given the code, and I'm supposed to fill in the "?"s

break ? [] = ?
break n xs = brk n xs ?
 where
 brk n xs acc
  | xs == [] = ?
  | n == ? xs = ? ++ break n (? xs)
  | ? = brk n (? xs) (acc ++ ?)

I'm not sure how to approach this or how the acc helps with this problem. The only idea I have is that the first line should be

break _ [] = []

If anyone can help me get started with this problem, that would be great! Thanks!

1
  • 1
    I assume this is homework... FYI this kind of tail recursion isn't necessarily as great in Haskell as you might think, you can generally get away with guarded recursion. Commented Sep 18, 2012 at 5:17

1 Answer 1

6

acc is an abbreviation for “accumulator”—you use it to accumulate your results as you recurse. Look at the types of break and brk for some clues:

break :: (Eq a) => a -> [a] -> [[a]]
brk   :: (Eq a) => a -> [a] -> [[a]] -> [[a]]

Or more concretely:

break :: Char -> String -> [String]
brk   :: Char -> String -> [String] -> [String]

Here are some questions to help you figure out how to implement brk:

  • What should the initial set of results be?

  • What should you get when you split an empty list?

  • What should you do when the splitting character appears at the start of the string?

  • What should you do otherwise?

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

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.