unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Interesting Behavior of 'append!' In Local Context
@ 2009-10-17 22:28 Eric McDonald
  2009-10-17 23:09 ` Stephen Compall
  0 siblings, 1 reply; 6+ messages in thread
From: Eric McDonald @ 2009-10-17 22:28 UTC (permalink / raw)
  To: guile-user

Hi,

Earlier today I ran into an interesting problem, involving the 'append!'
statement modifying a local variable in Guile. I'm tempted to call it a
bug, but I'm definitely not a Scheme expert and so am hoping someone can
provide some enlightenment....

Suppose that I have two function definitions:

guile> (define foo (lambda (p1) (let ((v1 '(-1))) (begin (append! v1 p1)))))
guile> (define bar (lambda (p1) (let ((v1 (list -1))) (begin (append! v1
p1)))))

I then use 'foo' as follows:

guile> (foo (list "abc" "def"))
(-1 "abc" "def")
guile> (foo (list "abc" "def"))
(-1 "abc" "def" "abc" "def")

Notice that 'v1' does not seem to be re-initialized in the second
invocation of 'foo'. Interestingly, if I run 'bar' with the same data,
the problem does not manifest itself:

guile> (bar (list "abc" "def"))
(-1 "abc" "def")
guile> (bar (list "abc" "def"))
(-1 "abc" "def")

Now, if I change the definitions of the functions to use 'set!' in
conjunction with an 'append' rather than just an 'append!', then both
behave as I would expect (i.e., the same as 'bar' above):

guile> (define foo (lambda (p1) (let ((v1 '(-1))) (begin (set! v1
(append v1 p1)) v1))))
guile> (define bar (lambda (p1) (let ((v1 (list -1))) (begin (set! v1
(append v1 p1)) v1))))
guile> (foo (list "abc" "def"))
(-1 "abc" "def")
guile> (foo (list "abc" "def"))
(-1 "abc" "def")
guile> (bar (list "abc" "def"))
(-1 "abc" "def")
guile> (bar (list "abc" "def"))
(-1 "abc" "def")

I tried comparing the behavior with mzScheme, but it doesn't seem to
have an 'append!' function. However, its output does match the tests
using the set!/append versions of my functions, as expected.

Any thoughts?

Thanks,
  Eric

P.S. Tested with Guile 1.8.5 and 1.8.7. Could not test with Guile from
Git repo, because the built executable had a fatal error on startup.




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-10-18 13:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-17 22:28 Interesting Behavior of 'append!' In Local Context Eric McDonald
2009-10-17 23:09 ` Stephen Compall
2009-10-18  0:36   ` Eric McDonald
2009-10-18  1:45     ` Stephen Compall
2009-10-18 13:53       ` Eric McDonald
2009-10-18 10:25     ` Andy Wingo

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).