
hi, i'm having this problem, tring to implement split (for learning purposes). Here is one sample of my tries : split _ [] = [] split char (x:xs) | x /= char = x : split char xs | otherwise = [] it is ok i.e. it returns the first of the splits, but I want to get all of the results. str = "testingxsplitxtoxseexhowxitxwork" *Main> split 'x' str "testing" I want : ["testing","split",...] How do i do this.

raptor
split _ [] = [] split char (x:xs) | x /= char = x : split char xs | otherwise = []
*Main> split 'x' str "testing"
I want : ["testing","split",...]
How do i do this.
Collect the read chars in a temp, when the separator is detected, return the temp (probably reversing it) and recurse. -k -- If I haven't seen further, it is by standing in the footprints of giants

On 12/1/05, raptor
hi,
i'm having this problem, tring to implement split (for learning purposes). Here is one sample of my tries :
split _ [] = [] split char (x:xs) | x /= char = x : split char xs | otherwise = []
it is ok i.e. it returns the first of the splits, but I want to get all of the results.
str = "testingxsplitxtoxseexhowxitxwork"
*Main> split 'x' str "testing"
I want : ["testing","split",...]
How do i do this.
A good start is to write a function which returns the first split, as well as the remainder of the string as a tuple. Then you can use this function over and over on the remainder of each call until it is "". /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862

| |A good start is to write a function which returns the first split, as |well as the remainder of the string as a tuple. |Then you can use this function over and over on the remainder of each |call until it is "". ]- that is the problem :"), 'cause such functions should accept Char and String, but return a Tuple of Strings, but it is recursive (so that it fetch next,next,next char :)) i.e. it is own "consumer" and should return String --this is wrong, just tring grab char (x:xs) | x /= char = (x : grab char xs, xs) | otherwise = ([],[])

On 12/1/05, raptor
| |A good start is to write a function which returns the first split, as |well as the remainder of the string as a tuple. |Then you can use this function over and over on the remainder of each |call until it is "".
]- that is the problem :"), 'cause such functions should accept Char and String, but return a Tuple of Strings, but it is recursive (so that it fetch next,next,next char :)) i.e. it is own "consumer" and should return String
--this is wrong, just tring grab char (x:xs) | x /= char = (x : grab char xs, xs) | otherwise = ([],[])
I'm not going to give you a solution, since I suspect this is homework. But consider this hypothetical (and utterly nonsensical) function foo :: [Int] -> ([Int],[Int]) foo (x:xs) | x < 5 = ([x] , ys) | otherwise ( 5 : ys, rest) where (ys, rest) = foo xs I mean, this function doesn't do anything useful at all, so don't bother trying to figure out what it does :-) But the point is that you can still use recursion even if the result of the function is a tuple. Just put the recursive call in a where-statement (it won't get evaluated unless needed, laziness buys us that), and pattern match against the contents of this tuple. Of course, this can be written using the higher order functions dropWhile and takeWhile, as well, but maybe you're required to write it from scratch? /S -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
participants (3)
-
Ketil Malde
-
raptor
-
Sebastian Sylvan