2

I am trying to learn Common Lisp with the book Common Lisp: A gentle introduction to Symbolic Computation. In addition, I am using SBCL, Emacs, and Slime.

In chapter 14, the last one, the author covers macros. The following problem is presented:

Write a macro called VARIABLE-CHAIN that accepts any number of inputs. The expression (VARIABLE-CHAIN A B C D) should expand into an expression that sets A to ’B, B to ’C, and C to ’D.

The answer sheet is:

enter image description here

Copying from the pdf and pasting it here:

(defmacro variable-chain (&rest vars)
 ‘(progn 
    ,@(do ((v vars (rest v))
           (res nil))
          ((null (rest v)) (reverse res))
        (push ‘(setf ,(first v) ’,(second v))
               res))))

In Emacs, I used this hack to remove smart-quotes. Pasting it in Emacs, I get:

(defmacro variable-chain (&rest vars)
  '(progn
     ,@(do ((v vars (rest v))
            (res nil))
           ((null (rest v)) (reverse res))
         (push '(setf ,(first v)
                      ',(second v))
                res))))

Unfortunately, I cannot compile it to the Slime's REPL, it throws an error:

> READ error during COMPILE-FILE: Comma not inside a backquote.

I tried changing '(progn to:

`(progn

But it also did not work: "comma not inside a backquote".

Did I do something wrong? Or, is the answer sheet incorrect?

Thanks.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Pedro Delfino
  • 2,421
  • 1
  • 15
  • 30
  • I did that, @Rob. I put the code. But I **also** put the image. Since the problem could be related to **how** the original information in a pdf file was stored, I thought it could be relevant to the problem. – Pedro Delfino Aug 20 '21 at 19:39
  • The question in the problem statement is relevant. It just seems to me that is quite indifferent if the question in the book is copied/pasted or print as an image. Anyway, I will change the question from the book to text. – Pedro Delfino Aug 20 '21 at 19:41
  • 1
    @Rob turns out the posted image is essential here. not posting it here would've made the question unclear. its posting could not have been avoided and should not be penalized. :) – Will Ness Aug 20 '21 at 20:18
  • 1
    @Rob please re-read my comment. :) the **image** is important. the image _is_ the (essential part of the) question here. a PDF is _seen_ correctly, a text is copied out with some non-ASCII chars instead, which are then improperly handled by some external too. – Will Ness Aug 20 '21 at 20:54
  • 1
    @Rob I've expanded my comment above ,but you could also read the question and the answer more closely to get the full and correct perspective here. if it were just re-typed by a human, who's to say they didn't introduce some other aberration there? – Will Ness Aug 20 '21 at 20:56
  • 1
    @Rob unless the question _is_ about the image. – Will Ness Aug 20 '21 at 20:58
  • 1
    @Rob is the image still in the question? yes, as it is intrinsic to it. as well as the both textual code snippets. – Will Ness Aug 20 '21 at 21:00
  • @WillNess Ah! NOW I get it. The problem is about his copying and pasting the pdf into emacs! I figured it out on my own and NOT from what was posted or even your answer. The question needs clearing up or I'm sure someone else will make the same mistake. – Rob Aug 20 '21 at 21:02

1 Answers1

1

You need to change the other one as well:

(defmacro variable-chain (&rest vars)
  `(progn
 ;; this one you did change
     ,@(do ((v vars (rest v))
            (res nil))
           ((null (rest v)) (reverse res))
         (push `(setf ,(first v)
           ;; ^^^ also need to change this one
                      ',(second v))
                res))))

The is the backquote, whereas is the regular quote, but your "hack" turned both of them into the regular quote ' chars erroneously:

(defmacro variable-chain (&rest vars)
 ‘(progn 
 ;; backquote
    ,@(do ((v vars (rest v))
           (res nil))
          ((null (rest v)) (reverse res))
        (push ‘(setf ,(first v) ’,(second v))
          ;; backquote         quote
               res))))
Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • 1
    and actually, you see them correctly in the image. it is the copying from pdf that produced them as non-ASCII chars, and "hack" translation that introduced the error. – Will Ness Aug 20 '21 at 20:08
  • Thank you, @Will. One doubt, as a more experienced lisper, do you have a better hack to deal with copying lisp code from pdfs? Just using common sense? – Pedro Delfino Aug 20 '21 at 20:12