Arc Forumnew | comments | leaders | submitlogin
2 points by waterhouse 4850 days ago | link | parent

I think there are only two conceivable, allowed-by-the-language cases where you could have dotted lists in code: (a) literal dotted lists, as in '(1 . 2), and (b) parameter lists. [Edit: Oh man, I forgot about destructuring, as rocketnia points out. I do sometimes destructure with dots. They are instances of parameter lists, though, and can be handled as such.]

(b) can be easily handled in other ways (e.g. in CL, you say "&rest args" instead of ". args").

(a) is extremely rare, and it's never necessary in code--you could replace it with (cons 1 2). There is the fact that, inside the body of a function, (cons 1 2) will create a new cons cell every time the function is called, whereas '(1 . 2) will always refer to the same cons cell[1]; however, you could fix this by wrapping it in (let u (cons 1 2) ...) and referring to u instead of '(1 . 2).

The only potential problem I see (with dropping dotted lists) is that you might need to read (or print) dotted s-expressions as data. That does come up now and then; you might represent some compound data structure (like a rational number) with a cons cell, which is slightly more efficient than using a two-element list (two cons cells) and perhaps nicer. Also, Racket prints hash tables with dots--I'm sure you routinely see things like #hash((a . 1) (b . 2)). And someone might create some kind of tree structure with conses.

You could dotted lists for now, then, when you find them necessary, create a private notation for non-proper-list trees (say, using ^ in place of . in trees). Then there would be no problem except dealing with tree output from other Lisp implementations, which is rare enough that you can probably afford to ignore it for now, and if it comes up, just write something to translate back and forth. That would be my advice.

As for unquote-splicing: Most of the time I use it in macro definitions, most of the time for "body" arguments. Here is a handy terminal command to illustrate:

  $ egrep -o ',@[^ )]+' a | sort | uniq -c
     2 ,@(apply
     1 ,@(explode
     1 ,@(flat1
     2 ,@(if
     5 ,@(map
     1 ,@(mappend
     5 ,@args
     1 ,@binds
    16 ,@body
     1 ,@indices
     1 ,@leads.expr
     2 ,@vars
If what you're wondering is whether I use unquote-splicing to make dotted lists, the answer is no, and I would consider it bad practice (if you're making something as unusual as that, you should use cons or something).

[1] For example:

  arc> (def meh (x) '(1 . 2))
  #<procedure: meh>
  arc> (meh 2)
  (1 . 2)
  arc> (= (car (meh 2)) 5)
  5
  arc> (meh 3)
  (5 . 2)
  arc> (def uch (x) (cons 1 2))
  #<procedure: uch>
  arc> (= (car (uch 2)) 5)
  5
  arc> (uch 3)
  (1 . 2)