Why do these following functions not work the same ? The first one is doing a proper string split but the second one seems to keep adding “” forever, creating an infinite list
Right code:
my_split :: [Char]->Char->[[Char]]
my_split [] _ = [[]]
my_split lista y
| notElem y lista=[lista]
| otherwise=isMatch:(my_split rest y)
where
isMatch=takeWhile (/=y) lista
rest=tail $ dropWhile (/=y) lista
Bad Code :
my_split :: [Char]->Char->[[Char]]
my_split [] _ = [[]]
my_split lista y
| notElem y lista=[lista]
| otherwise=isMatch:(my_split rest y)
where
(isMatch,rest)=break (==y) lista
The only part that’s different is the break condition and it really seems to me it should do the same thing…plus the first function form should ensure i don’t get to add empty lists to my result forever…Sorry about the noobish question and thanks in advance
breakdoesn’t strip off the character it matches.