Highlighting a Conformity Clause in CASE

Update

I think I understand what's going on. Lisp only accepts '

part of the match clauses '(1 2 3)

and '(41 42 43)

as a key (because they both expand to (Quote

... and from this point of view the second '

= (Quote

... in the third code snippet is a "duplicate key" (although in fact it is not a key at all. It's just syntax error on my part).

I think my main problem was that I didn't understand the compiler warnings correctly.

Original post

I don't understand the following behavior case

(or sbcl).

In this SO posting, R. Joswig points out that form matching clauses are case

not evaluated and are not treated as literals. Consequently, bids for proposals should not be indicated.

Not knowing it, I quoted one match proposal, but forgot to quote another. I don't understand why the compiler doesn't complain if I quote one sentence, but complain if I quote both of them. In particular:

> (let ((x 42))
     (case x
       ((1 2 3) 'first-branch)
       ('(41 42 43) 'second-branch)))      ; <=  second match clause quoted
NIL

      

=> No warnings.

> (let ((x 42))
     (case x
       ('(1 2 3) 'first-branch)            ; <=  first match clause quoted
       ((41 42 43) 'second-branch)))
NIL

      

=> No warning.

> (let ((x 42))
     (case x
       ('(1 2 3) 'first-branch)            ; <=  both quoted
       ('(41 42 43) 'second-branch)))      ; <=  
NIL

      

=> style warning

 ; caught STYLE-WARNING:
 ;  Duplicate key QUOTE in CASE form, occurring in the first clause:
 ;     ('(1 2 3) 'FIRST-BRANCH), and the second clause:
 ;     ('(41 42 43) 'SECOND-BRANCH).

      

Why does the compiler only complain if I am quoting two correspondence sentences, but not if I am quoting only one?

+3


source to share


2 answers


Because it is case

not evaluated, so when you are quote

both cases, the code is equivalent:

(let ((x 2 ))
 (case x
   ((QUOTE (1 2 3)) 'first-branch)           
   ((QUOTE (41 42 43)) 'second-branch)))

      



which means you have literal quote

in both forms, which triggered the warning.

+2


source


Just to be clear, the quote symbol is just another symbol without any specific meaning. Common Lisp expects in the head of an expression expression case

either a constant atom like foo

, or a list of constant atoms like (foo bar baz)

. Also note that Lisp uses eql

both comparison - so it is compared for identification or value in the case of numbers and symbols, but not for content.

This example shows what quote

is another character in the case of sentences case

:

CL-USER> (case 2
           ((quote foo) 'foo)
           ((quote bar) 'bar))
WARNING: Duplicate key QUOTE in CASE form, occurring in the first clause:
  ('FOO 'FOO), and the second clause:
  ('BAR 'BAR).
NIL

      



If we write baz

instead quote

, we get the same warning:

CL-USER> (case 2
           ((baz foo) 'foo)
           ((baz bar) 'bar))
WARNING: Duplicate key BAZ in CASE form, occurring in the first clause:
  ((BAZ FOO) 'FOO), and the second clause:
  ((BAZ BAR) 'BAR).
NIL

      

+3


source







All Articles