Haskell: Splitting list into tuple of two new lists?

Your split function returns a pair, but in the last case you are using on the result of split That will be a type error, since works on lists, not pairs. There is also a type error because fst and snd are functions to pick out the elements of a pair, but you are using them is a strange way Furthermore, use pattern matching instead of using length. Also, the case where you test if the length is 2 is not needed, since the general case removes 2 elements which takes you down to the base case of the empty list You can also make your function more general by using a type variable a instead of Int in the type Edit: Added code split :: a -> (a, a) split = (, ) split x = (x, ) split (x:y:xys) = (x:xs, y:ys) where (xs, ys) = split xys.

Your split function returns a pair, but in the last case you are using ++ on the result of split. That will be a type error, since ++ works on lists, not pairs. There is also a type error because fst and snd are functions to pick out the elements of a pair, but you are using them is a strange way.

Furthermore, use pattern matching instead of using length. Also, the case where you test if the length is 2 is not needed, since the general case removes 2 elements which takes you down to the base case of the empty list. You can also make your function more general by using a type variable a instead of Int in the type.

Edit: Added code split :: a -> (a, a) split = (, ) split x = (x, ) split (x:y:xys) = (x:xs, y:ys) where (xs, ys) = split xys.

Another way to do this is with mutual recursion. It comes out very easy to read: split xs = (odds xs, evens xs) odds (x:xs) = x : evens xs odds xs = evens xs = odds (drop 1 xs).

Split :: a -> (a, a) split xs | null xs = (, ) | otherwise = (head xs : snd pair, fst pair) where pair = split (tail xs) But you should be using a fold: split :: a -> (a, a) split = foldr (\x (ys, zs) -> (x : zs, ys)) (, ).

That code doesn't do what he asked for, and it is also bad because it uses head and tail instead of pattern matching. – augustss Sep 14 at 6:28 Could you give an example of an input where my code gives the wrong result, please. (And are you referring to the recursion-and-guards version or the foldr version?) I agree that pattern matching would be better than head and tail, but the OP seems to want to use guards, which --- in this case --- precludes pattern matching (there is nothing left to guard after the pattern match).

– dave4420 Sep 14 at 7:06 1 Sorry, I take that back about it being wrong. Not enough coffee. :) – augustss Sep 14 at 7:31.

There is a mistake in the last clause. You have to get results from recursive call and then add first and second elements to them. Split :: Int -> (Int,Int) split xs | length(xs) == 0 = (,) | length(xs) == 1 = (xs!0 : ,) | length(xs) == 2 = (xs!0 : , xs!1 : ) | otherwise = let (fst, snd) = split(drop 2 xs) in (xs!0 : fst, xs!1 : snd).

This is still a horrible way to write this function. – augustss Sep 14 at 3:52 Yes, you are absolutely right. But I'm trying to follow question 'to accomplish this recursively(with guards) and only using the single argument xs' – bravit Sep 14 at 3:55 You can still do that and get O(n) complexity instead of O(n^2).

– augustss Sep 14 at 4:32.

In case you are looking for some alternate way to do this, below is one such implementation: split xs = let (a,b) = partition (odd . Snd) (zip xs 1..) in ( (map fst a), (map fst b)).

Two alternative versions: split = conv . Map (map snd) . GroupWith (even.

Fst) . Zip 0.. where conv xs,ys = (xs,ys) split xs = (ti even xs, ti odd xs) where ti f = map snd . Filter (f.

Fst) . Zip 0..

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send