Arc Forumnew | comments | leaders | submitlogin
Possible bug in using + for lists
4 points by dido 6299 days ago | 7 comments
I've observed some weird behavior in using + to concatenate lists together. Apparently, it seems to make a difference if you use () rather than nil in this case. For example, this works, and has the expected behavior:

arc> (+ '(a b c) nil)

(a b c)

However, this does not:

arc> (+ '(a b c) ())

Error: "+: expects type <number> as 1st argument, given: (a b c . nil); other arguments were: ()"

What's going on here? I always thought that nil and the empty list were exactly the same.



5 points by drcode 6299 days ago | link

In Common Lisp '() and () should be identical and are made so through a compiler hack, from my understanding.

PG has indicated in the past (in the ANSI CL book, I believe) that he doesn't like this behavior, because the missing quote is a bit confusing.

It sounds to me though like this the error is a bug, since in arc they otherwise seem to be treated as identical.

-----

6 points by pg 6298 days ago | link

Yes, that looks like a bug.

-----

6 points by pg 6297 days ago | link

Just fixed it. Will go out in next release (soon).

-----

3 points by sacado 6299 days ago | link

nil is not equivalent to () but to '() : if you type (+ '(a b c) '()) everything is ok.

-----

5 points by dido 6299 days ago | link

arc> (iso nil ())

t

arc> (iso nil '())

t

arc> (iso () '())

t

How are they not supposed to be equivalent?

-----

4 points by sacado 6299 days ago | link

from ac.scm :

  ; rather strictly excludes ()
  (define (arc-list? x) (or (pair? x) (eqv? x 'nil)))

  ; generic +: strings, lists, numbers.
  (xdef '+ (lambda args
           (cond ((null? args) 0)
                 ((all string? args)
                  (apply string-append args))
                 ((all arc-list? args)
                  (ac-niltree (apply append (map ar-nil-terminate args))))
                 (#t (apply + args)))))
Obviously, () is not an arc-list (as defined above). Thus, the test (all arc-list? args) fails with (+ '(a b c) ()) : finally, (apply + args), which is the numerical + of scheme is applied, hence the error message.

You were right, this might be a bug...

-----

2 points by mec 6299 days ago | link

I believe that '() is nil and works correctly in your example, but I'm not entirely sure what the difference is between the two.

-----