[Haskell-cafe] Never-ending discussion... Was: Basic list exercise

Jerzy Karczmarczuk jerzy.karczmarczuk at unicaen.fr
Sat Mar 25 00:04:26 UTC 2023


We continue the separation of ascending subequences...

Le 24/03/2023 à 22:10, Johannes Waldmann a écrit :
> @Jerzy : I like your plan to use combinators.
> Then let's remove (visible) recursion completely:
>
> *map (map fst ) $ groupBy (const snd)
>                $ zip xs ( True : zipWith (<=) xs (tail xs)) *

Hmmm, I didn't find it... Perhaps at that time I didn't acquire yet a 
sufficient level of perversion...

Well, the truth is that I had problems with groupBy! People discovered 
that the "standard" groupBy, which was sometimes thought of as an 
iterator which compared neighbours, didn't. If it behaved as some people 
wished, then we could simply write

*groupBy (<) [3,4,5,6,2,3,4,1,2,1]*

and get : *[[3,4,5,6],[2,3,4],[1,2],[1]] *

Actually ... It works!! But unfortunately this is accidental. The 
following example doesn't:

*groupBy (<) [5,3,1,2,8,6,3,7,2,5,9]
[[5],[3],[1,2,8,6,3,7,2,5,9]]*

This was discovered apparently more than once. Look up

*http://brandon.si/code/an-alternative-definition-for-datalistgroupby/*

Brandon Simmons gives his alternative definition, which defines its own 
version of span:

*groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy' c [] = []
groupBy' c (a:as) = (a:ys) : groupBy' c zs
     where (ys,zs) = spanC a as
           spanC _ [] = ([], [])
           spanC a' (x:xs)
             | a' `c` x = let (ps,qs) = spanC x xs
                           in (x:ps,qs)
             | otherwise = ([], x:xs)*

which yields the following result:

*groupBy' (<) [5,3,1,2,8,6,3,7,2,5,9]
[[5],[3],[1,2,8],[6],[3,7],[2,5,9]]*

Look also:

*https://hackage.haskell.org/package/groupBy-0.1.0.0/docs/Data-List-GroupBy.html*

by Donnacha Oisín Kidney (c) 2018; but I didn't verify his variant.

==

Jerzy Karczmarczuk



-- 
Cet e-mail a été vérifié par le logiciel antivirus d'Avast.
www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230325/dc4921e6/attachment.html>


More information about the Haskell-Cafe mailing list