[plt-scheme] rnrs/base-6 and flatten

From: Carl Eastlund (carl.eastlund at gmail.com)
Date: Wed Jan 20 12:06:39 EST 2010

On Wed, Jan 20, 2010 at 11:16 AM, nik gaffney <nik at fo.am> wrote:
>
> Is the following  expected behaviour when using rnrs/base-6 ?
>
> (module test scheme
>  (require  rnrs/base-6)
>  (flatten '((a) b (c (d) . e) ())))
>
> returns ({{a} b {c {d} . e} ()})
>
> whereas
>
> (module test scheme
>  (require  #;rnrs/base-6)
>  (flatten '((a) b (c (d) . e) ())))
>
> returns (a b c d e)

Yes, that is the expected behavior.  PLT Scheme uses immutable pairs
by default, whereas R6RS uses mutable pairs.  In our implementation,
these are different datatypes.  I believe what is happening is that
you have imported 'quote' from rnrs/base-6, so your quoted
s-expression is producing mutable pairs instead of immutable pairs.
The 'flatten' function, however, comes from PLT Scheme, so it operates
on immutable pairs, and does not do anything with its argument.

PLT Scheme (the scheme language, without any rnrs requires) does have
primitives for dealing with mutable pairs (mcons, mcar, mcdr, etc.),
though not every function has an equivalent (there is no mflatten, for
instance).  You may want to use the mutable pair functions explicitly;
alternately, you may want to import the r6rs bindings with a prefix so
that you use the PLT bindings until you explicitly say not to.  For
instance:

(module test scheme
  (require (prefix-in r6rs: rnrs/base-6))

  ;; immutable pair:
  (cons 'a '())

  ;; mutable pair:
  (mcons 'a '())

  ;; another mutable pair:
  (r6rs:cons 'a '())

  ;; immutable pairs, both input and output:
  (flatten '((a) b (c (d) . e) ())))

This fact about PLT/R6RS interoperability is documented here:

http://docs.plt-scheme.org/r6rs/Scheme_Interoperability.html

In case you are curious, the rationale for this design decision is
described in a PLT blog post by Matthew Flatt:

http://blog.plt-scheme.org/2007/11/getting-rid-of-set-car-and-set-cdr.html

--Carl


Posted on the users mailing list.