Arc Forumnew | comments | leaders | submitlogin
2 points by greatness 6132 days ago | link | parent

I'm not sure if I quite understand you, "(A C)" In your Arc code suggests A is a function. Going by your first posted code, you can achieve this in Arc with:

   (aif (> (+ A C) B) (= A it))


2 points by sjs 6132 days ago | link

When that is true it evaluates to t.

  arc> (= a 2 b 5 c 3)
  3
  arc> (aif (> (+ a c) b) (= a it))
  nil
  arc> a
  3
  arc> (aif (>= (+ a c) b) (= a it))
  t
  arc> a
  t

-----

3 points by greatness 6132 days ago | link

good point, I should have thought that one through. Perhaps we should consider changing the return value of ">" so I'm correct? :P

It seems more useful to have the return value be the greater number if it is true, and since all non-nil values are true, it wont break any existing code. For a > b, consider the following:

  (> a b) => a
  (> b a) => nil
We can make a new > procedure which does this:

  (def >? args
     (if (apply > args) (car args) nil))
repl:

  arc> (>? 5 2)
  5
  arc> (>? 2 5)
  nil
  arc> (>? 7 5 2 3)
  nil
Then, using my definition from before:

  (aif (>? (+ a c) b) (= a it))
It shall now work. Now I'm not stupid anymore. :p

-----

1 point by sjs 6132 days ago | link

Makes sense to me. It's a trivial change in ac.scm for < and >. What about (<= 1 2 3) and (>= 3 2 1)? The easiest way to do the same for those two is to return the last value compared, which would be 3 and 1 respectively. That doesn't exactly jibe with < and > though so I hesitate to make such a change. Thoughts?

It seems logical that <= and >= should also return some meaningful value if < and > do. If we come up with something good I'll push it to the anarki repo (git wiki). Although, we could always just ignore <= and >= for now.

-----

1 point by greatness 6129 days ago | link

Why not follow the same idea as < and >:

  (def <=? args 
    (if (apply <= args) (car args)))

-----

1 point by sjs 6127 days ago | link

I should have been clearer. I agree the semantics should be the same, I just didn't see an immediately clean way to add that to the current, recursive <= and >= functions. A thin wrapper would work just fine.

-----

1 point by greatness 6123 days ago | link

Yeah, either that or use a lambda function inside their definitions:

  (def >= args
    (let f (afn (ar) (or no.ar
                       (no:cdr ar)
                       (and (no:< car.ar cadr.ar)
                       (self:cdr ar))))
      (if (f args) car.args)))

-----

1 point by bOR_ 6132 days ago | link

just ment A B and C to be numbers.

Got it.. thanks for introducing me to anaphoric macros :). Now the question is whether writing macros and having several aifs aands awhile aif2s for it is the way to go, or whether it is something that might be core-worthy, in a way that gosub (see post below) suggested.

-----