Arc Forumnew | comments | leaders | submitlogin
Hash element as lvalue
5 points by zhtw 6097 days ago | 8 comments
This is possible:

  (= t (table))
  (= (t 'asdf) 10)
But when try to access value returned from procedure I get this:

  arc> (def f (t name) (t name))
  #<procedure: f>
  arc> (= (f t 'asdf) 10)
  Error: "procedure .../avl/arc2/ac.scm:1062:12: expects 3 arguments, given 4: #<procedure: func> 10 #hash() asdf"
Is it possible to return from procedure somthing like lvalue. In other words is it possible to implement the same as built in 'table'?

Alexey Lebedev



5 points by absz 6097 days ago | link

Yes, it is possible. But first, a formatting tip: surround your code with blank lines and indent each line of code with two spaces to get it to look like a real code block.

What you want is defset, defined in "arc.arc". The idea with defset is that you define what will happen when = sees something of the form (name ...) as its first argument (called a "place"). The best tutorial for defset is http://arcfn.com/doc/setforms.html -- it should answer your questions. Good luck!

-----

2 points by zhtw 6097 days ago | link

Thanks for the tip. That way my first post, I didn't know how would it look.

Anyway looks like defset is exactly what I need. Thanks a lot!

-----

2 points by absz 6096 days ago | link

It occurs to me that there's another problem with this code: your use of t as a variable name. This is outright forbidden globally:

  arc> (= t (table))
  Error: "Can't rebind t"
. Though it does work locally, it is nevertheless inadvisable: t is the global "true" value (equivalent to 't). It's probably better to use tb as the name for your table.

Glad to be of assistance.

-----

2 points by sacado 6096 days ago | link

And don't ever use tab, particularily for a table : it is a macro's name so it will get you in trouble.

-----

1 point by absz 6096 days ago | link

Or we could fix that part of the language ;)

Seriously, that's one of my biggest standing irritations. If I have some time, I might look at fixing that in the Anarki, actually.

-----

2 points by zhtw 6095 days ago | link

I also had a problem with tab and also think that it should be fixed.

Btw what do I need to load to get the base language without any web stuff. Are there an official "base" part? As.scm loads all the libraries for me. Do I need to load arc.arc only to get the base language.

-----

1 point by absz 6095 days ago | link

Yes, that would be the way to do it, but I don't know a simple way to load only arc.arc.

-----

3 points by almkglor 6097 days ago | link

If you're really interested in creating your own collection objects, such as building your own table-like structure, please see my series of "Create your own collection", listed in order of posting:

http://arclanguage.org/item?id=3595

http://arclanguage.org/item?id=3698

http://arclanguage.org/item?id=3762

http://arclanguage.org/item?id=3858

-----