unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Mark H Weaver <mhw@netris.org>
To: "Hood\, Christopher L." <Christopher.Hood@gtri.gatech.edu>
Cc: 32841@debbugs.gnu.org
Subject: bug#32841: assoc-set fails with dot notation association list
Date: Wed, 26 Sep 2018 23:02:17 -0400	[thread overview]
Message-ID: <87zhw3d4na.fsf@netris.org> (raw)
In-Reply-To: <d61a791aaa734245ac705bb5196ff18e@gtri.gatech.edu> (Christopher L. Hood's message of "Tue, 25 Sep 2018 20:33:41 +0000")

Hi Christopher,

"Hood, Christopher L." <Christopher.Hood@gtri.gatech.edu> 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





  parent reply	other threads:[~2018-09-27  3:02 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-25 20:33 bug#32841: assoc-set fails with dot notation association list Hood, Christopher L.
2018-09-25 22:04 ` John Cowan
2018-09-27  3:02 ` Mark H Weaver [this message]
2018-09-27 12:09   ` John Cowan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zhw3d4na.fsf@netris.org \
    --to=mhw@netris.org \
    --cc=32841@debbugs.gnu.org \
    --cc=Christopher.Hood@gtri.gatech.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).