[Haskell-cafe] returning lists
sebastian.sylvan at gmail.com
Thu Dec 1 18:33:30 EST 2005
On 12/1/05, raptor <raptor at tvskat.net> wrote:
> |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])
| 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?
More information about the Haskell-Cafe