[Haskell-cafe] list -> sublists
Ariel J. Birnbaum
valgarv at gmx.net
Tue Oct 20 13:25:38 EDT 2009
> 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])
>
> g [2,4,1,6,3,6,2,3,5,2,5,2,1,6,4]
> returns:
> ([2,4,1,6,3],[6,2,3,5,2,5,2,1,6,4])
>
> and so on ? Then you should be able to build what you wanted by using
> g in a modified version of f.
For this kind of questions, Hoogle[1] is your friend. You have a list of
type [a], and you want to split it into a pair of (the list up to some
point, the rest of the list), whose type is then ([a],[a]).
So you search in Hoogle: [a] -> ([a],[a])
and the following results come up:
Prelude.break :: (a -> Bool) -> [a] -> ([a], [a])
Prelude.span :: (a -> Bool) -> [a] -> ([a], [a])
(... a few more...)
A quick review of the descriptions will show that one of these functions
is /very/ close to what you want to achieve. You may use it to implement
your solution and/or look at its source code to understand how it works.
In fact, you can do better---look at your /original/ problem and figure
out what type to search for in Hoogle. One of the results there should
turn your problem into a one-liner. Again, you can take a look at its
code for additional insight.
References: [1] http://www.haskell.org/hoogle/
Have fun!
--
Ariel J. Birnbaum
More information about the Haskell-Cafe
mailing list