A small Clojure/ClojureScript library for representing LISP expressions in infix rather than prefix notation... sometimes it's easier to rely on operator precedence, instead of LISP's insistence on parentheses – this is especially true when dealing with mathematical equations.
An infix expression is rewritten as a prefix expression using a macro. The operator precedence rules were taken from Wikipedia. Any function calls should generally have parens around the arguments and not the function name. Aliased unary operators (as outlined below) do not need parens however.
You will need Leiningen 2.6.1 or above installed.
To build and install the library locally, run:
$ cd infix
$ lein test
$ lein install
There is a version hosted at Clojars. For leiningen include a dependency:
[rm-hull/infix "0.2.2"]
For maven-based projects, add the following to your pom.xml
:
<dependency>
<groupId>rm-hull</groupId>
<artifactId>infix</artifactId>
<version>0.2.2</version>
</dependency>
(refer 'infix.macros :only '[infix from-string base-env])
; => nil
(infix 3 + 5 * 8)
; => 43
(infix (3 + 5) * 8)
; => 64
Some Math
functions have been aliased (see below for full list), so single argument functions can be
used as follows:
(infix √(5 * 5))
; => 5.0
(infix √ 121)
; => 11.0
(infix 2 ** 6)
; => 64.0
(def t 0.324)
; => #'user/t
(infix sin(2 * t) + 3 * cos(4 * t))
; => 1.4176457261295824
(macroexpand-1 '(infix sin(2 * t) + 3 * cos(4 * t))
; => (+ (Math/sin (* 2 t)) (* 3 (Math/cos (* 4 t))))
A function can created at runtime from an expression held in a string as follows. When building from a string, a number of binding arguments should be supplied, corresponding to any variable that may be used in the string expression, for example:
(def hypot
(from-string [x y]
"sqrt(x**2 + y**2)"))
; => #'user/hypot
(hypot 3 4)
; => 5
from-string
is deliberately designed to look like an anonymous function
definition, mainly because that is more-or-less what it is. In effect, this is
equivalent to creating the following function:
(def hypot
(fn [x y]
(infix sqrt(x ** 2 + y ** 2))))
However, it does so without recourse to eval
and read-string
- instead it is built using our
old
friend, the
monadic parser-combinator, with an EBNF grammar (implementing the infix notation)
and a restricted base environment of math functions, as outlined in the next
section.
The base-env
may be extended with any number of key/value pairs (where keys
are keywords) and values may either be values or functions, to provide
the required extensions. When referenced in the string it is not necessary
to prefix the name with a colon.
(def extended-env
(merge
base-env
{:rad (fn [deg] (infix deg * π / 180))
:hypot hypot}))
; => user/extended-env
(def rhs-triangle-height
(from-string [base angle]
extended-env
"tan(rad(angle)) * base"))
; => user/rhs-triangle-height
(rhs-triangle-height 10 45)
; => 9.9999999999998
Obviously, a function that was previously created from a string can also referenced in a subsequent function definition:
(def hypot2
(from-string [x y]
extended-env
"hypot(x, y) ** 2"))
; => user/hypot2
(hypot2 5 12)
; => 169.0
Alias | Operator | Alias | Operator | Alias | Operator | ||
---|---|---|---|---|---|---|---|
&& | and | abs | Math/abs | sin | Math/sin | ||
|| | or | signum | Math/signum | cos | Math/cos | ||
== | = | ** | Math/pow | tan | Math/tan | ||
!= | not= | exp | Math/exp | asin | Math/asin | ||
% | mod | log | Math/log | acos | Math/acos | ||
<< | bit-shift-left | e | Math/E | atan | Math/atan | ||
>> | bit-shift-right | π | Math/PI | sinh | Math/sinh | ||
! | not | sqrt | Math/sqrt | cosh | Math/cosh | ||
& | bit-and | √ | Math/sqrt | tanh | Math/tanh | ||
| | bit-or | root | b √ a | sec | Secant | ||
φ | Golden ratio | csc | Cosecant | ||||
gcd | Greatest common divsor | fact | Factorial | cot | Cotangent | ||
lcm | Least common multiple | ∑ | Sum | asec | Arcsecant | ||
∏ | Product | acsc | Arccosecant | ||||
acot | Arccotangent |
TODO - needs documenting
The MIT License (MIT)
Copyright (c) 2016 Richard Hull
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.