Groups | Search | Server Info | Login | Register


Groups > comp.lang.lisp > #60671

Re: Am I missing something about (loop ... maximizing ...) ?

From tpeplt <tpeplt@gmail.com>
Newsgroups comp.lang.lisp
Subject Re: Am I missing something about (loop ... maximizing ...) ?
Date 2025-08-25 18:03 -0400
Organization A noiseless patient Spider
Message-ID <87tt1vys79.fsf@gmail.com> (permalink)
References <102u3to$32kl5$1@dont-email.me> <108h6qk$3aant$1@dont-email.me>

Show all headers | View raw


> B. Pym wrote:
>
>> Raffael Cavallaro wrote:
>> 
>> > Indeed. Here's rob Warnock's version with the full boat of loop
>> > keywords using triplets instead of an association list:
>> > 
>> > CL-USER 22 > (defun find-maximizing-item (list &key (key #'identity)
>> > (test #'<))
>> >                (loop for item in list
>> >                      for value = (funcall key item)
>> >                      and position from 0
>> >                      for current-triplet = (list item value position)
>> >                      as max-triplet = current-triplet
>> >                      then (if (funcall test (second max-triplet)
>> > (second current-triplet))
>> >                             current-triplet
>> >                             max-triplet)
>> >                      finally return
>> >                      (values (first max-triplet) (second max-triplet)
>> > (third max-triplet))))
>> 
>> 
>> Rejected by ABCL:
>> 
>> Debugger invoked on condition of type PROGRAM-ERROR:
>> 
>> Current LOOP context: "A compound form was expected, but ~S found.".
>> 
>> 
>> Rejected by SBCL:
>> 

The function definition is missing a single pair of
parentheses in the ‘finally’ clause.

    finally return
            (values (first max-triplet) (second max-triplet)
                    (third max-triplet))

should be changed to:

    finally (return
              (values (first max-triplet)
                      (second max-triplet)
                      (third max-triplet))

See:
http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_6-1-1-6.html

   * The code for any FINALLY clauses is collected into one
     PROGN in the order in which the clauses appear in the
     source. The collected code is executed once in the loop
     epilogue before any implicit values from the accumulation
     clauses are returned.  Explicit returns anywhere in the
     source, however, will exit the loop without executing the
     epilogue code.

Since the code in the FINALLY clause is collected into an
implicit PROGN, the code is required to be Lisp expressions,
instead of LOOP clauses, which rules out the use of the LOOP
keyword RETURN.

With the change described above, the function works as
expected on the tests below:

(find-maximizing-item 
 (list "one" "two" "three" "fifteen" "four" "five") :key #'length)
;;=> "fifteen"
;;=> 7
;;=> 3

(find-maximizing-item '("bells" "usher" "ullalume" "poe") :key #'length)
;;=> "ullalume"
;;=> 8
;;=> 2

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.

Back to comp.lang.lisp | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Am I missing something about (loop ... maximizing ...) ? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-06-18 10:20 +0000
  Re: Am I missing something about (loop ... maximizing ...) ? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-25 08:28 +0000
    Re: Am I missing something about (loop ... maximizing ...) ? tpeplt <tpeplt@gmail.com> - 2025-08-25 18:03 -0400
  Re: Am I missing something about (loop ... maximizing ...) ? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-25 08:38 +0000

csiph-web