Arc Forumnew | comments | leaders | submitlogin
Dotted lists.
2 points by shader 5673 days ago | 5 comments
I have been working on some code that requires dotted lists, and it is interesting how many list oriented functions do not handle the dotted list case. Is this for efficiency reasons, or oversight?

All I really needed was to be able to get the length of the dotted list, but it would be nice if some of the other list functions worked as well.



2 points by shader 5673 days ago | link

Here's a new arc-level implementation of len, that works for dotted lists as well, using the mz/ac-tunnel patch:

  (def len (x (o c 0))
    (if (isa x 'string) (mz:string-length x)
        (isa x 'vec) (mz:vector-length x)
        (isa x 'table) (mz:hash-table-count x)
        (and atom.x x) (+ c 1)
        acons.x (len cdr.x (+ c 1))
        c))

-----

1 point by Adlai 5673 days ago | link

I don't think Arc has vectors.

I think the reason that lists are usually flat lists is that a flat list is homogenous -- each car is data, and each cdr is the next element, or nil. Dotted lists add another case which you'd have to handle. It's just simpler to deal with flat lists.

If you're just dealing with two- or three-element lists, then it might pay off to use dotted lists. In such short lists, you'd be getting a space saving of 33% or 25% per list.

-----

1 point by shader 5672 days ago | link

Arc uses vectors to implement tagged procedures. Type the name of a macro in on the repl, and it displays a vector:

  arc> def
  #3(tagged mac #<procedure:.../arc/arc.arc.scm:151:11>)
That's a vector with 3 elements: tagged, mac, and a procedure.

Arc doesn't currently let you construct or manipulate your own vectors, but they could be used to provide transparent meta-data attached to functions and variables, such as where they were defined, and what the source code was that did so.

-----

1 point by conanite 5673 days ago | link

I think vectors are available in anarki. I guess in the interest of building a minimal foundation, they're excluded from official arc.

-----

2 points by conanite 5673 days ago | link

There's a comment at the end of arc.arc that might be relevant:

  ; solution to the "problem" of improper lists: allow any atom as a list
  ;  terminator, not just nil.  means list recursion should terminate on
  ;  atom rather than nil, (def empty (x) (or (atom x) (is x "")))
But I think this means (len '(a b c . d)) would be 3, which may not be what you wanted.

-----