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

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Mon Aug 25 17:41:37 2025
    From Newsgroup: comp.lang.lisp

    John Thingstad wrote:

    It is the trivial to check for duplicates and lookup key

    (defun duplicate-value-check ()
    (let* ((values (loop for v in (cdr *enum*) by #'cddr collect v))
    (sorted-values (sort values #'<))
    (position (mismatch sorted-values
    (remove-duplicates sorted-values))))
    (when position
    (let ((index (nth position sorted-values)))
    (error "key ~A has a value ~D duplicated"
    (nth (* index 2) *enum*) (nth index sorted-values))))))

    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.
    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2
  • From tpeplt@tpeplt@gmail.com to comp.lang.lisp on Tue Aug 26 14:37:11 2025
    From Newsgroup: comp.lang.lisp

    "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 LisprCOs rCylooprCO macro supports iterating over
    the CDRs of lists and destructuring, the CL version of
    rCydup-checkrCO 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.
    --- Synchronet 3.21a-Linux NewsLink 1.2