Groups | Search | Server Info | Login | Register


Groups > comp.lang.lisp > #60675

Re: How to use constants in a case key form ?

From tpeplt <tpeplt@gmail.com>
Newsgroups comp.lang.lisp
Subject Re: How to use constants in a case key form ?
Date 2025-08-26 14:37 -0400
Organization A noiseless patient Spider
Message-ID <87plciylns.fsf@gmail.com> (permalink)
References <108i78e$3ig75$1@dont-email.me>

Show all headers | View raw


"B. Pym" <Nobody447095@here-nor-there.org> writes:

>
> Testing with ABCL:
>
> (setq *enum* '(a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 2))
> (duplicate-value-check)
>
> CL-USER(3): Debugger invoked on condition of type SIMPLE-ERROR:
>   key C has a value 3 duplicated
>
> It should have been:
>   key i has a value 2 duplicated
>
> Gauche Scheme
>
> (define (dup-check plist)
>   (let1 seen '()
>     (do-plist ((k v) plist)
>       (if (member v seen)
>         (errorf "Key ~a has dup. value ~a." k v)
>         (push! seen v)))))
>
> (dup-check '(a 2 b 3 c 4 d 3 e 6 f 7 g 8 h 3 i 42))
>
> *** ERROR: Key d has dup. value 3.

Because Common Lisp’s ‘loop’ macro supports iterating over
the CDRs of lists and destructuring, the CL version of
‘dup-check’ is similar:

(defvar *enum* '(a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9 i 2))

(defun dup-check (plist)
  "Verify that property list PLIST has no duplicate values.  An error
is issued if any are found."
  (loop
    for (k v) on plist by #'cddr
    if (member v unique) do
      (error "key ~A has a value ~D duplicated" k v)
    else
      collect v into unique
    finally (return 'no-duplicates)))

(dup-check *enum*)
;;=> Run-time error: "key I has a value 2 duplicated"

(dup-check '(a 2 b 3 c 4 d 3 e 6 f 7 g 8 h 3 i 42))
;;=> Run-time error: "key D has a value 3 duplicated"

(dup-check
 '(a 2 b 3 c 4 d 5 e 6 f 7 g 8 h 9))
;;=> NO-DUPLICATES

See "The for-as-on-list subclause":
http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/sec_6-1-2-1-3.html

-- 
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 | Find similar


Thread

Re: How to use constants in a case key form ? "B. Pym" <Nobody447095@here-nor-there.org> - 2025-08-25 17:41 +0000
  Re: How to use constants in a case key form ? tpeplt <tpeplt@gmail.com> - 2025-08-26 14:37 -0400

csiph-web