< Back to IRCAM Forum

Order of first number

Hello, OMians!

I wonder whether there is a simple and efficient way to perform the following test: in a list of lists of numbers, in each list I would like to test that a larger number does not appear before a lower one (the first time only). For example: (1 2 3 2 1) is ok, but (1 3 2 1 2) not, because 3 appears before 2 appears the first time. Other examples of not good lists are: (2 1 3 1 2 3 2), first 2 before 1, (1 2 4 3 2 5 1), 4 before 3, (1 2 1 1 3 1 5 1 1 4 2), 5 before 4.
What I managed to write looks terribly complicated to me, but, perhaps, there is no easy solution to this.
Thank you very much in advance.

Marco

Hi Marco!

Not sure what you consider sequence manipulations as efficient, but if you #'remove-duplicates on your list, you can check whether the result is ordered. Perhaps something like this:

(let ((seq (remove-duplicates '(1 2 1 1 3 1 5 1 1 4 2) :from-end t)))  
    (every #'< seq (cdr seq)))

Hi Marco!

Not sure whether you consider sequence manipulations as efficient, but if you #'remove-duplicates on your list, you can check whether the result is ordered. Perhaps something like this:

(let ((seq (remove-duplicates '(1 2 1 1 3 1 5 1 1 4 2) :from-end t)))  
    (every #'< seq (cdr seq)))

Thank you very much for your proposal. Actually, it’s great!! I was doing loops of loops, really not efficient at all. I will try it immediately.

Marco