Arc Forumnew | comments | leaders | submitlogin
3 points by waterhouse 5421 days ago | link | parent

I learned to use 'defset by looking at the documentation and demonstration in arc.arc.

  (defset aref (x . indices)
   (w/uniq (gx gi)
           (list `(,gx ,x ,gi (list ,@indices))
                 `(,gx ,gi)
                 `(fn (val) (= (,gx ,gi) val)))))
And this works fine:

  arc> (= x (table))
  #hash()
  arc> (= (aref x 1 2) 3)
  3
  arc> x
  #hash(((1 2 . nil) . 3))
  arc> (x '(1 2))
  3
However, strangely, my definition of aref, as:

  (def aref (x . indices)
    (x indices))
does not work as intended. If x, a table, has a value for the key (list 1 2), then (aref x 1 2) will not find that value. I investigated and, basically, it seems that the list created by (list 1 2) is nil-terminated, while the list constructed from a rest parameter is ()-terminated:

  arc> (def ach (x . rest) rest)
  #<procedure: ach>
  arc> (ach x 1 2)
  (1 2)
  arc> (list 1 2)
  (1 2)
  arc> (cddr (ach x 1 2))
  ()
  arc> (cddr (list 1 2))
  nil
I imagine that's why the hash-table doesn't recognize the 'indices rest parameter as the key (1 2 . nil). Oh well, here's a stupid hack that makes 'aref work properly by constructing an Arc nil-terminated list:

  (def aref (x . indices)
    (x (apply list indices)))