Arc Forumnew | comments | leaders | submitlogin
1 point by shader 5429 days ago | link | parent

in is just a input-port that I would already have bound, or passed in. If you'll notice, that was just a standalone expression, with no context, to illustrate what I was trying to do.

Thanks for the code, that was exactly what I was thinking of. Too bad there isn't a macro or function that just returns the value of a variable in such a way that it can be assigned to. Can anyone think of how to implement such a thing?

i.e.:

  (= v 'a)
  (= (val v) 6) ;the variable a is now assigned the value 6.
Basically an (unquote) without the wrapping quote ;)


1 point by Adlai 5429 days ago | link

Well, there's always 'eval, but it has issues and stigmas, and I've read that other Lispers look at you funny when you use it your code...

'in is also a built-in macro in arc.arc. Since Arc is a Lisp-1 (grr...), you can't bind it to an input stream without potentially breaking some other code...

  (mac in (x . choices)
    (w/uniq g
      `(let ,g ,x
         (or ,@(map1 (fn (c) `(is ,g ,c)) choices)))))
Back to the symbol dereferencing question -- I think one reason that this kind of thing is shied away from is that it starts to smell an awful lot like pointers. However, the control over evaluating that you get with macros is more than enough to take care of symbol dereferencing problems like this.

-----

1 point by shader 5429 days ago | link

Right. I forgot to check the symbol before I used it like I sometimes do, via 'help or 'src (Anarki).

Is there anything wrong with "pointers"? As in this case, they can often be quite useful, and make the code (at least to me) simpler. Maybe I just think in pointers, and need to learn to use destructuring binding more. That won't work unless you know all of the variable names in advance, but I guess that's what a hash table is for :)

-----

2 points by almkglor 5427 days ago | link

Here's a simple Arc-F package (which is intended to work on hl in the future) that provides pointers.

Pointers are dereferenced by (ptr), so you use (ptr) to get the contents and (= (ptr) v) to assign.

  (in-package pointer)
  (using <arc>v3) ; replace with <hl>v1
  (interface v1
    pointer)
  ; replace with (mac (pointer e) ...)
  (mac pointer (e)
    (if (acons e)
        (let vars (map [uniq] e)
          `(with ,(mappend (fn (var val) `(,var ,val)) vars e)
             ; replace with (tag...)
             (annotate 'pointer
               (cons (fn () ,vars)
                     (fn (v) (= ,vars v))))))
        (w/uniq v
          `(annotate 'pointer
             (cons (fn () ,e)
                   (fn (,v) (= ,e ,v)))))))
  ; replace with (defm (sref (t p pointer)... ) ...)
  (defm sref ((t p pointer) v)
    ((cdr:rep p) v))
  (defcall pointer (p)
    ((car:rep p)))
Usage:

  (= stuff (list (pointer a)
                 (pointer (car b))
                 (pointer c!x)))
  (each p stuff
    (pr (p))
    (= (p) (something)))
If someone's ported nex3's 'defcall and 'defm onto Anarki-on-arc3, the above can be made to work by just removing the package stuff.

-----