[Haskell-beginners] list splitting - nice implementation?

KC kc1956 at gmail.com
Sun Nov 18 20:10:53 CET 2012


For efficiency considerations, look at the "core" code generated.

On Sun, Nov 18, 2012 at 2:06 AM, Emmanuel Touzery <etouzery at gmail.com> wrote:
> That is EXACTLY the kind of answer i was hoping for!
> Great implementation, I'll be sure to reuse that trick of zipping with the
> tail of the list.. really really good.
>
> I'm relieved it's not trivial (for me) to write since i could not come up
> with it, and happy i understand it :-)
>
> Intuitively it's more expensive than what an imperative language would do
> (new list creations, going several times over the list -zip, spam, map -
> still O(n) though).

See first remark.

>
> If it was in your project you'd use that, or would you use a more
> "straightforward" implementation and you came up with this one just because
> i asked explicitely for such a way?
>
> On 18 Nov 2012 10:47, "Tobias Brandt" <tob.brandt at googlemail.com> wrote:
>>
>> Here is a possible solution:
>>
>> import Data.List
>>
>> split xs = getSnds $ span (uncurry (<)) $ zip xs (tail xs)
>>     where getSnds (as, bs) = (map snd as, map snd bs)
>>
>> firstPart xs = fst $ split xs
>>
>> sndPart xs = snd $ split xs
>>
>> This is a one pass algorithm, it works for infinite lists.
>>
>> On 18 November 2012 08:45, Emmanuel Touzery <etouzery at gmail.com> wrote:
>>>


-- 
--
Regards,
KC



More information about the Beginners mailing list