Arc Forumnew | comments | leaders | submitlogin
4 points by bogomipz 5066 days ago | link | parent

Ever since I first started learning the fundamentals of Lisps, I always wondered why dot notation couldn't be used interchangibly with 'apply, eg:

  (def foo (x . xs)
    (bar x . xs))
instead of:

  (def foo (x . xs)
    (apply bar x xs))
The first snippet above seems much more intuitive to me than the second.


5 points by aw 5066 days ago | link

Assuming you don't change the syntax of dotted lists, a problem is that you wouldn't be able to use an expression for "xs".

The dot notation creates a cons for you, and a cons creates a regular list if its second argument is nil or another cons (which itself is a regular list).

Thus

  '(a . (b))
works like

  (cons 'a '(b))
which in turn is like

  '(a b)
which you can see for yourself:

  arc> '(a . (b))
  (a b)
So an expression like

  (bar x . (foo))
that we'd want to work like

  (apply bar x (foo))
is parsed by the reader like this:

  arc> '(bar x . (foo))
  (bar x foo)
and by the time the compiler sees the expression there's nothing there to tell it that a dot was used.

Of course, if you change the reader so that a dot doesn't produce a dotted list but instead creates a token for the compiler to see, then you can do whatever you want... though you'd also need to extend the compiler to handle the dot notation in the other places which is currently handled by the reader.

-----

2 points by rocketnia 5066 days ago | link

Great point.

For what it's worth, 'apply could be moved to arc.arc:

  (def apply (self . args)
    (zap rev args)
    (zap [rev:+ (rev:car _) cdr._] args)
    (self . args))
I think the number of times I use 'apply with a variable in the final position is enough for the syntax to be relevant to me... but then I'd be likely to refactor (a b c . d) into (apply a b c (something-else)) and vice versa all the time, which would be a bit of a pain. Then again, it's balanced against the cost of refactoring (a b c) into (a b c . d) and vice versa....

by the time the compiler sees the expression there's nothing there to tell it that a dot was used.

In Racket's syntax there is a way, I think. It makes syntax more complicated than just its conses, though. You'd have to use an Arc variant of syntax-quasiquote (or just syntax-quasiquote) to construct it in macros.

Meanwhile, the (cdr `(foo . ,<scheme-expr>)) quirk would become more of a bug than it already is. But then I assume whatever Arc hack implements this syntax would also have '$ built in, so there's probably no point to keeping the quirk around.

-----

1 point by evanrmurphy 5066 days ago | link

I like that idea.

I don't see a way you could use it if xs were the only argument, i.e. a way to rewrite `(apply bar xs)`, but in that case apply is more paletable anyway:

  (def foo xs
    (apply bar xs))

-----

3 points by rocketnia 5066 days ago | link

  (def foo xs
    (bar . xs))

-----

1 point by evanrmurphy 5066 days ago | link

I see, so `'(bar . xs)` is a cons cell and `(bar . xs)` is equivalent to `(apply bar xs)`.

-----

1 point by rocketnia 5066 days ago | link

I've wondered that too. XD

-----