Subject: bug#32841: assoc-set fails with dot notation association list

From: Mark H Weaver <[email-masked]>
Date: Wed, 26 Sep 2018 23:02:17 -0400

Hi Christopher,

"Hood, Christopher L." <[email-masked]> writes:
> This fails with code pulled straight out of the Guile manual example
> (section 6.6.20.6).

Indeed, the example code in the manual is bad.  Thanks for bringing this
to our attention.

> (define capitals '(("New York" . "Albany")
>                    ("Oregon"   . "Salem")
>                    ("Florida"  . "Miami")))

As John correctly pointed out, it's an error to mutate a program
literal.  It's analogous to trying to modify a string literal in C.
'assoc-set!' mutates the existing list structure in some cases, and so
it cannot be used on literals such as the one above.

To initialize an alist that will be mutated, you must instead do
something like this:

  (define capitals (list (cons "New York" "Albany")
                         (cons "Oregon"   "Salem")
                         (cons "Florida"  "Miami")))

> I’ll note that if you define the alist so its initial contents are
> defined using a quasiquote and the cons form instead of dot notation,
> this error is not reached.

Yes, that accomplishes essentially the same thing, although note that
quasiquote makes an effort to use program literals for parts of the
resulting list structure, e.g.:

  `(,a ,b c d)

expands to:

  (cons a (cons b '(c d)))

which means that the first two cons cells can be mutated, but the last
two are part of an immutable program literal.

> I’m not sure if the error is valid or not, but in any case, the code
> that produces is listed as an valid example in the manual, so that
> doesn’t seem right.

Indeed, the manual needs to be fixed.  Sorry for the confusion, and
thanks again for the bug report.

     Mark