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

    From B. Pym@21:1/5 to John Thingstad on Mon Aug 25 17:41:37 2025
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)