Re: sorted?

Mikael Djurfeldt <[email protected]>
Newsgroups gmane.lisp.guile.user
Message-ID <CAA2XvwLJwONingQ-of0HLOphsbBoZpL=fNrM=HWPNh5NqKgpEw@mail.gmail.com>
On Mon, Dec 9, 2024 at 1:37 PM <[email protected]> wrote:

> On Mon, Dec 09, 2024 at 01:20:48PM +0100, Mikael Djurfeldt wrote:
> > On Mon, Dec 9, 2024 at 12:43 PM <[email protected]> wrote:
> >
> > > On Mon, Dec 09, 2024 at 11:37:33AM +0000, Ricardo G. Herdt wrote:
> > > > Hi Jeremy,
> > > >
> > > > Am 09.12.2024 11:21 schrieb Jeremy Korwin-Zmijowski:
> > > > > The reference says :
> > > > >
> > > > >    Scheme Procedure: *sorted?* items less
> > > > >    C Function: *scm_sorted_p* (items, less)
> > > > >
> > > > >        Return |#t| if items is a list or vector such that, for each
> > > > >        element x and the next element y of items, |(less y x)|
> returns
> > > > >        |#f|. Otherwise return |#f|.
> > > > >
> > > > > I think the description should be :
> > > > >
> > > > >    Return |#t| if items is a list or vector such that, for each
> element
> > > > >    x and the next element y of items, |(less y x)| returns |#t|.
> > > > >    Otherwise return |#f|.
> > > >
> > > > Actually no, since less is applied to y and x in that order. This way
> > > > (sorted? '(1 1) <) correctly returns #t as your experiments show.
> > >
> > > I don't get it. (< 1 1) is /always/ #f, regardless of the order of the
> > > ones?
> > >
> > > I'm as confused as Jeremy is.
> > >
> >
> > sorted? checks whether *a list* has its elements sorted. It does not
> apply
> > a predicate to all successive pairs like some fold operation. Two
> > consecutive equal elements are in sorted order since it wouldn't matter
> if
> > you switched them. The less operation is used to sort the list (or check
> > it).
>
> Thanks for your explanation. I must admit that I'm still confused. Perhaps
> I'll have to have a look at the implementation.
>
> I'm sometimes slow :-)
>

No problem---I'm too.

Think about it this way:

How would you sort this list of numbers: 7 1 3 8 2 1 4 ?

It's 1 1 2 3 4 7 8, right? That is what we want (sort '(7 1 3 8 2 1 4)) to
output (+ the parentheses of course).

Now, `sorted?' returns true if its input is what `sort' would have produced
as output, otherwise false.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.