Arc Forumnew | comments | leaders | submit | sacado's commentslogin
1 point by sacado 6425 days ago | link | parent | on: arc2c update

Yep, I did not try to handle sharedvars at all with my GC. I will eventuelly need to give them a type tag, but I deferred that to the next release :) Splitting polymorphic functions is something that has to be done too.

As for variadic +, I don't know : if we transform (+ a b c d) in (+ a (+ b (+ c d))) during the first phase of compilation, we obviously handle variadic +, but at the end (in the generated code), the ADD() primitives only handles + with 2 args, which I guess would be more efficient ? And, that way, we could translate (+ a 1 b 2) into (+ a (+ 3 b)), and (+) to 0, which will eventually have to be done.

-----

3 points by almkglor 6425 days ago | link

Then someone redefines '+ so that it doesn't just add numbers or concatenate strings. Say (+ 1 2 "asdf") becomes "3asdf".

Further, what if we need to pass '+ as a function to another function?

  (def hmm (op)
    (op 1 2 3 4 5))

-----

1 point by sacado 6425 days ago | link

Oh, yes, so that wasn't really a good idea...

-----

1 point by sacado 6425 days ago | link | parent | on: arc2c update

Except if someone pushed it soon, arc2 on Anarki is not the current version. The up-to-date version is sacado/arc2c.

-----

3 points by kens 6425 days ago | link

How do you organize your directories for arc2c? In particular, where does Anarki go relative to arc2c? Do you overlap the git repositories? And then what directory do you start arc in? (I can't get the paths to work for the loads.)

-----

1 point by sacado 6425 days ago | link

I'm not sure I fully understand your question, but you are supposed to put all the arc2c files at the root level, where arc.sh lives, then load arc2c.sh and finally call (compile-file "foo.arc"). Tell me if I didn't answer your question or if you still get something wrong.

-----

2 points by almkglor 6424 days ago | link

minor correction: s/arc2c\.sh/arc2c\.arc/

To be specific:

  $ ls
  arc2c/ arc-wiki/
  $ cd arc-wiki/
  $ cp -r ../arc2c/* .
  $ ./arc.sh
  Use (quit) to quit, (tl) to return here after an interrupt.
  arc> (load "arc2c.arc")
  nil
  arc> (compile-file "t.arc")
   <lots of stuff>

-----

2 points by kens 6423 days ago | link

Yes, that's what I was looking for: where to put arc2c in the tree.

My next arc2c problems are a) gc.h is missing; do I need to download it somewhere? and b) ‘QUOTE_CONSTANTS’ undeclared - I can't figure out where it gets declared. I'm trying to compile simply "(+ 1 1)".

-----

1 point by almkglor 6423 days ago | link

a) Yes, you need the Boehm GC. What I do is, I comment out gc.h and add #define GC_MALLOC malloc #define GC_INIT()

b) That's a bug

-----

2 points by kens 6423 days ago | link

How do I push changes to the git? "git push" gives me "fatal: The remote end hung up unexpectedly". I set up a ssh public key. Do I need to get authorization from you guys? (I've never done a git push before, so assume I may be doing something stupid.)

-----

1 point by sacado 6422 days ago | link

You're on the collaborators' list now. Just push again, and everything should be working.

-----

1 point by stefano 6422 days ago | link

You'll have to ask sacado to add you to the list of committers.

-----


I found it was more looking like K or something close.

-----

2 points by bOR_ 6425 days ago | link

K++ perhaps. I'm sort of making unjust fun of the language, but I seriously prefer ((())()()) to this.

  #import std
  #import nat
  
  #comment -[
  solves the general case of the 8 queens problem;
  invoke as queens -n, where n is a number > 3]-
  
  #executable <'par',''>
  #optimize+
  
  queens =
  
  %np+~command.options.&h.keyword.&iNC; -+
     ~&iNC+ file$[contents: --<''>+ %nLP*=; * '<'%='[ '+          ','%=', '+ '>'%=' ]']+ ~&rSSs+ nleq-<&l*rFlhthPXPSPS,
   ~&i&& ~&lNrNCXX; ~&rr->rl %tLnLtXLLWXMk+ ^/~&l ~&lrrhrSiF4E?/~&rrlPlCrtPX ~&r; ^|/~& ^|T\~& -+
      -<&l^|*DlrTS/~& ~&iiDlSzyCK9hlPNNXXtCS,
      ^jrX/~& ~&rZK20lrpblPOlrEkPK13lhPK2; ~&i&& nleq$-&lh+-,
   ^/~&NNXS+iota -<&l+ ~&plll2llr2lrPrNCCCCNXS*=irSxPSp+   ^H/block iota; *iiK0 ^/~& sum+-

-----

1 point by sacado 6426 days ago | link | parent | on: fork() in Arc

Interesting. Should be added to ffi.arc...

-----

2 points by eds 6426 days ago | link

Yeah, because currently if you attempt

  (w/ffi #f
    (cdef cfork "fork" cint ()))
arc will convert #f to nil, which mzcheme thinks is an unbound variable. There are work-arounds, like

  (w/ffi (read "#f")
    (cdef cfork "fork" cint ()))
but this is rather ugly.

-----


Not tested, but it should work and is tail-recursive :

  (def power (nbr pow res)
    (if (is pow 0)
	    res
        (> pow 0)
            (power nbr (- pow 1) (* nbr res))
	(< pow 0)
            (power nbr (* -1 pow) (/ 1 res))))
Not sure for the case where (< pow 0), though. Just run the tests, but I think the tail-recursive function is at least as fast as the iterative one. Actually, in fine, iterative code is transformed into its tail-recursive equivalent : code involving for is transformed as a bunch of tail-recursive functions for example.

-----

5 points by sacado 6426 days ago | link | parent | on: Superfactorial, noobhelp plz

  (def sufac (n) (let y 1 (for x 1 (+ n 1) (= y (* y (fac n)))) y))
The for syntax is (for x first last), not (for x first (+ last 1)) (unlike python). So you have to write :

  (def sufac (n) (let y 1 (for x 1 n (= y (* y (fac n)))) y))
You've also got another bug (a typo I guess) : it's not (fac n) but (fac x) :

  (def sufac (n) (let y 1 (for x 1 n (= y (* y (fac x)))) y))

-----


Macros have to be defined before they are used in a function, because they are expanded during the definition of the function.

Example :

  (def hello () (blankpage (pr "Hello world")))
  => def is a macro, evaluated as (set hello (fn () (blankpage (pr "Hello world"))))
Once the function is "compiled", no macro expansion will ever happen. So, when you call 'hello, it's looking for a function named 'blankpage and cannot find it.

  (mac blankpage body `(tag html (tag body ,@body)))
  => new macro saved

  (def hello () (blankpage (pr "Hello world")))
  => def and blankpage are macros, expansion performed, evaluated as 
  => (set hello (fn () (tag html (tag body (pr "Hello world")))))
  => tag is a macro, so another expansion has to be performed, etc.
Here, all macros are evaluated, only function calls are left in the compiled form. Thus, if you redefine 'blankpage or any other macro, don't forget to redefine all associated functions too.

But it does not apply to functions. They can be defined in any order.

-----

2 points by croach 6426 days ago | link

That's quite interesting. After reading through your explanation it makes perfect sense why I am running into this problem. Thanks for the answer and for such a fast reply to my query.

-----

3 points by sacado 6426 days ago | link | parent | on: OOP in Arc

If you implement your objects like hash tables (the way it is done in Lua, Javascript, Perl or -- secretly -- in Python), you can easily implement anything you need, including duck typing and inheritance : http://arclanguage.com/item?id=3155.

But not strong encapsulation, sorry. This can be done with closures however, if you really need it (but I don't think Arc is the right language if you want to constrain the developer at all).

You can also make any function doing dynamic dispatch based on the type of objects. For example, you can redefine the + function to perform a special operation when called with a foo object as a parameter, or behave the usual way in the other cases. That's a method-calling mechanism (even more powerful actually). Use any object representation you need (hash table, list, cons cell, integer, ...) and 'annotate, and you're done !

-----


Yep. if actually behaves likes the ?: operator in C

  printf ("%d\n", x == 0 ? 1 : 2);

  (prn (if (is x 0) 1 2)

-----


(= 1 1) is written (is 1 1) in Arc. The equal sign is used for affectation. You get an error as 1 cannot be set. So, for your first question,

  if x == 0 return 1 else return 2
is translated as

  (if (is x 0) 1 2)
As for DrScheme, I don't know, sorry :)

-----

4 points by dreish 6427 days ago | link

Although this does bring up an unfortunate characteristic of 'is':

  arc> (is 0 0.0)
  nil
It might be nice to have an == that does a numeric comparison, if floats and ints are always going to be considered unequal by 'is'.

-----

2 points by tokipin 6427 days ago | link

in the meantime i have devised a clever scheme i call "natural coercion":

  arc> (with (x 0 y 0.0)
          (is (+ x 0.0) (+ y 0.0)))
  t
it can be toolified as:

  (def eq args
      (apply is (map [+ _ 0.0] args)))

-----

2 points by eds 6427 days ago | link

I would have expected 'iso to work for that, but it also returns nil.

  arc> (iso 0 0.0)
  nil

-----

1 point by sacado 6426 days ago | link

You're right. This is really strange as 0.0 is considered as an 'int.

-----

3 points by rincewind 6426 days ago | link

This may be related to http://arclanguage.org/item?id=5720

-----

1 point by sacado 6426 days ago | link

Hmmm... very possible...

-----

More