unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Weird Guile Scheme Behaviour
@ 2019-09-13  9:43 Philip K.
  2019-09-13 12:43 ` Neil Jerram
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Philip K. @ 2019-09-13  9:43 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 1902 bytes --]


Hi,

I was reading a thread on an imageboard[0] the other day, then I came
across a most peculiar "bug", if it even is one. Since the original
example was a bit dense (it tried to solve a problem someone else had
posted, that's not relevant here), I tried to construct a minimal
working example to discuss here.

Compare

    (define (reverse-iota-1 max)
      (let ((numbers '(start)))
        (let loop ((val 0))
          (append! numbers
                   (if (< max val)
                       '()
                       (begin
                         (loop (1+ val))
                         (list val)))))
        numbers))

and

    (define (reverse-iota-2 max)
      (let ((numbers '(start)))
        (let loop ((val 0))
          (append! numbers
                   (if (< max val)
                       '()
                       (begin
                         (loop (1+ val))
                         (list val)))))
        (cdr numbers)))

(I know, the style is horrible, but that's not the point. Also, both
have an internal state, so you have to re-eval the function every time
before starting the function itself.)

The only difference is in the last line. The first function returns the
entire list (with the start symbol), and the second tries to chop it
off.

But what happens is that (reverse-iota-1 4) evals to '(start 3 2 1 0)
while (reverse-iota-2 4) just returns '()!

This seems weird, since my intuition, and that of the poster above, was
that all that should change in reverse-iota-2 is that the "start" symbol
should fall away.

It's obvious that this has something to do with the destructive
"append!", but I'm not quite sure what leads to this unexpected result.
Is it maybe a optimisation error? Any opinions?

[0]: https://lainchan.org/%CE%BB/res/12185.html#15066 (SFW)

-- 
	With kind regards,
	Philip K.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

end of thread, other threads:[~2019-10-16  7:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-09-13  9:43 Weird Guile Scheme Behaviour Philip K.
2019-09-13 12:43 ` Neil Jerram
2019-09-13 17:22 ` Vladimir Zhbanov
2019-10-16  6:52 ` Mark H Weaver
2019-10-16  7:48   ` Philip K.

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