[Haskell-cafe] Re: list -> sublists

Luke Palmer lrpalmer at gmail.com
Tue Oct 20 12:03:54 EDT 2009

On Tue, Oct 20, 2009 at 9:27 AM, satorisanitarium
<satorisanitarium at gmail.com> wrote:
>> Now back to your original problem. Can you write a function g such that
>> g [1,2,3,2,4,1,6,3,6,2,3,5,2,5,2,1,6,4]
>> returns
>> ([1,2,3],[2,4,1,6,3,6,2,3,5,2,5,2,1,6,4])
> I know the diference between x and [x], my problem lies in going from
> [1,2,3,4,5,6,7] to [[1,2,3,4],[5,6,7]], I don't know how to write code
> that would group some elements together.

Here is an implementation of splitAt from the prelude (though it is
not hte implementation given in the prelude) for guidance.  For
example, splitAt 3 ['a','b','c','d','e','f'] =

splitAt :: Int -> [a] -> ([a],[a])
splitAt 0 xs = ([], xs)
splitAt n [] = ([],[])
splitAt n (x:xs) =
   let (rfst, rsnd) = splitAt (n-1) xs
   in (x:rfst, rsnd)

See if you can use that pattern to make the function that splits at a
particular element (as opposed to index).


More information about the Haskell-Cafe mailing list