Re: [Haskell-cafe] Basic list exercise

* On 24 Mar 2023, at 18:44, Johannes Waldmann
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe> wrote: *> >* ... *>* Does the implementation in Data.List do better? *>
Data.List's sort is doing a different thing. So I'm not seeing the relevance. Specifically, sort doesn't pay any attention to the incoming list's ordering.
* Since this is about "exercise" - I would expect my students *>* to find that code, so perhaps I'd point them to it right away, *>* and give the task to "understand it" (= argue that it is correct). *>* Arguing about cost seems harder. *>
This (ex-)student would wonder why you'd sent me to that code. (Perhaps the lesson is: first read the question.) Why does the correctness (or otherwise) of a sort bear any relation to preserving list sequence and chopping up a list into ascending sublists? AntC

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.
On Fri, Mar 24, 2023, 5:12 PM Anthony Clayden
* On 24 Mar 2023, at 18:44, Johannes Waldmann
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe> wrote: *> >* ... *>* Does the implementation in Data.List do better? *> Data.List's sort is doing a different thing. So I'm not seeing the relevance. Specifically, sort doesn't pay any attention to the incoming list's ordering.
* Since this is about "exercise" - I would expect my students *>* to find that code, so perhaps I'd point them to it right away, *>* and give the task to "understand it" (= argue that it is correct). *>* Arguing about cost seems harder. *>
This (ex-)student would wonder why you'd sent me to that code. (Perhaps the lesson is: first read the question.)
Why does the correctness (or otherwise) of a sort bear any relation to preserving list sequence and chopping up a list into ascending sublists?
AntC
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

(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
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... :: (a https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html... -> a https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html... -> Ordering https://hackage.haskell.org/package/base-4.18.0.0/docs/https://hackage.haske... ) -> [a https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html... ] -> [a https://hackage.haskell.org/package/base-4.18.0.0/docs/src/Data.OldList.html... ]
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
participants (2)
-
Anthony Clayden
-
David Feuer