Arc Forumnew | comments | leaders | submitlogin
Two arithmetic ideas
4 points by rkts 5793 days ago | 2 comments
1. If 'a' is a number,

  (a x b c d ...) == (x a (b c d ...))
So, e.g.,

  (1 + 2 - 3 * 4) == (+ 1 (- 2 (* 3 4)))
This can be implemented in Arc as follows:

  (defcall int (n . args)
    (if no.args
      n
      (car.args n (apply cadr.args cddr.args))))
(Similarly for 'num.)

In effect, this makes all arithmetic operators right-associative with the same precedence. It may not be consistent with convention, but it makes code readable and that's all I really care about. You'd just have to get used to things being a little different from math.

2. Repurpose the pipe character so that, instead of allowing symbols to contain more characters, it forbids all characters except letters and numbers, and places the result in parentheses. So, e.g.

  |a+b-c| == (a + b - c)
For a complete example, let's take the quadratic formula:

  (-b + sqrt(b^2 - 4ac)) / 2a
In Lisp, this is

  (/ (+ (- b) (sqrt (- (* b b) (* 4 a c)))) (* 2 a))
Applying (1), we get

  (((- b) + sqrt ((b * b) - 4 * a * c)) / 2 * a)
Applying (2), we get

  ((|-b| + sqrt (|b*b|-|4*a*c|)) / |2*a|)
Better, no?


2 points by vsingh 5787 days ago | link

This seems very complicated, and confusing. Unless your code is heavily math-oriented, it doesn't seem like a win. Even in that case, you would lose the ability to easily move and factor subexpressions, one of the things I prize most in Lisp.

I also foresee problems with code that looks like infix but doesn't actually have the operator precedence rules of infix, especially with the interaction between (1) and (2). We really don't need to lose more spaceships. ;)

-----

2 points by eds 5787 days ago | link

(1) is available on Anarki via lib/infix.arc.

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

One of the biggest problems is performance (about a 10x slowdown if you want to do infix analysis (i.e. order of operations) at runtime).

-----