[Haskell-cafe] Basic list exercise

Anthony Clayden anthony.d.clayden at gmail.com
Sat Mar 25 01:10:56 UTC 2023


(A word of counsel to the O.P.: the cafe is a place your q is liable to get
sidetracked into all sorts of places you didn't ask about. When you get to
a certain level of comfort with Haskell, that can be a learning experience.
Meantime you might find it better to ask on the Beginners' list, or
Stackoverflow -- as @Jerzy links to.)

On Sat, 25 Mar 2023 at 11:05, David Feuer <david.feuer at gmail.com> wrote:

> The correctness argument he was referring to was about understanding code
> in an educational context. The relevance is that Data.List.sort breaks up
> the input list into maximal "runs" of ascending or descending elements, to
> take advantage of any pre-existing organization. It then merges up runs
> rather than individual elements.
>
>
Thank you David. Let me take this opportunity to demonstrate why the
Haskell community is so off-putting to learners ...

The first thing the learner will come across at Johannes' link is

>    sortBy
<https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html#sortBy>
:: (a
<https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html#local-6989586621679636970>
-> a
<https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html#local-6989586621679636970>
-> Ordering
<https://hackage.haskell.org/package/base-4.18.0.0/docs/https://hackage.haskell.org/package/ghc-prim-0.10.0/docs/src/GHC.Types.html#Ordering/GHC.Types.html#Ordering>
) -> [a
<https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html#local-6989586621679636970>
] -> [a
<https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html#local-6989586621679636970>
]

So now they have to find out about `Ordering` and generic compare functions.

The next thing they might know is that sorting can't emit anything until
it's read the whole input -- just in case the least element is also the
last. So the gentle work @Viktor's answer did in extolling the benefits of
laziness is undone.

Now the O.P.'s concern was "constantly building and breaking apart pairs".
This is a rather naieve q, showing over-much procedural thinking IMO; but
the way to allay those fears is exactly _not_ what's going on inside `sort`.

* `ascending` is using a short-circuited form of `append` -- famous for
being inefficient.
* `descending` is using a short-circuited form of `reverse` -- also famous
for being inefficient.

Now the short-circuiting allays some of those inefficiencies (maybe, for a
smart compiler); but at first reading the code is doing far more "building
and breaking apart".

So a) Johannes' post hasn't answered the sub-text of the O.P.; b) it has
sent the learner on a wild goose chase.

Now, I don't know if this style of response is typical from Haskell
instructors. It is certainly not the warm and sympathetic welcome I
experienced when learning Haskell 12~15 years ago (before there was
StackOverflow). So if this is what @DavidF means by the
"educational context" for Haskell, I as a learner would have been inclined
to abandon Haskell as soon as my course allowed it.

I can only take it that "an educational context" means for those on track
to a PhD in advanced type theory and convoluted data structuring; and
exclude others from the ivory tower at the earliest opportunity.

AntC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230325/7f0653cb/attachment.html>


More information about the Haskell-Cafe mailing list