Friday, 15 May 2015

haskell - split function 'no instance for' error -



haskell - split function 'no instance for' error -

this function should tokenize string:

split s c xs j = if head s == c split s c (substr s j : xs) j (j + 1) else if j == length s (substr s j) : xs else split s c xs j + 1 substr s j = take j(drop s)

however next error msg: no instance (num [[char]]) arising utilize of 'split' possible fix: add together instance declaration @ (num [[char]])

thanks.

ok function now:

split s c xs j = if j == length s (substr s j) : xs else if head (drop j s) == c split s c (substr s j : xs) (j + 1) (j + 1) else split s c xs (j + 1) substr s j = take j(drop s)

now when apply function next args: split "123,456,789" ',' [] 0 0 result ["789", "456,789", "123"] what's happening here?

the problem on line else split s c xs j + 1. seems you're trying add together 1 result of split. forgot parens around (j + 1)

i'll assume split returns [string], yes?

edit: function hard follow. unusual ordering result of prepending substrings on 3rd line (substr s j) : xs.

try rewriting function using takewhile, dropwhile :: (a -> bool) -> [a] -> [a], or improve yet, proper string processing library bytestring provides

data.bytestring.char8.split :: (char -> bool) -> bytestring -> [bytestring]

haskell split tokenize

No comments:

Post a Comment