Path: csiph.com!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: tpeplt Newsgroups: comp.lang.lisp Subject: Re: Am I missing something about (loop ... maximizing ...) ? Date: Mon, 25 Aug 2025 18:03:38 -0400 Organization: A noiseless patient Spider Lines: 81 Message-ID: <87tt1vys79.fsf@gmail.com> References: <102u3to$32kl5$1@dont-email.me> <108h6qk$3aant$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 25 Aug 2025 22:03:43 +0000 (UTC) Injection-Info: dont-email.me; posting-host="e487bec5e0116d3d4dde57ed2528d8f7"; logging-data="3887094"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19RGYs0W4WMFdc5SdaNnjlMVVlV0sm1ruI=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:vCiE/4hixDIGQsjx9D2hWxggNKA= sha1:CznRegMc+hZ3yZky+UVuELaesKo= Xref: csiph.com comp.lang.lisp:60671 > 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.