При запуске lein run выдает:
Syntax error (ArityException) compiling at (/tmp/form-init16101050078466523252.clj:1:74).
Wrong number of args (1) passed to: math-translator.core/lpt/if-branch--165
Full report at:
/tmp/clojure-8291314938907423064.edn
До того как переделал часть кода в макрос, вроде все работало.
Сам фрагмент кода:
(ns math-translator.core (:gen-class))
;; This is simple translator lisp formula's to LibreOffice formula's module Math.
;; Simple examples:
;; (lpt '(setq mu (+ 1 (* (/ 1 h) (+ (* m1 l1 ) (* m2 l2) ) )))) =>
;; => "MU = (1 + {1 over H} times (M1 times L1 + M2 times L2))"
;;(let ((h 8))
;; (lpt `(setq mu (+ 1 (* (/ 1 ,h) (+ (* m1 l1 ) (* m2 l2) ) ))))) =>
;; => "MU = (1 + {1 over 8} times (M1 times L1 + M2 times L2))"
(defn lpt [expr]
(if (= (type expr) clojure.lang.PersistentList) ;; if argument is list
(let [f (first expr) ;; based on the first argument, select the parsing rule
r (rest expr)] ;; the "digital" part of an expression
(defmacro if_branch [payload]
(if (> (count r) 1) ; more than 1 parameters of the function "/" ~payload (lpt (first r)))) (cond ;; here are the rules for converting tokens in constructions of another language (= f 'setq) (str (lpt (second expr)) " = " (lpt (nth expr 2))) (= f '<=) (if_branch (str (lpt (first r)) " leslant " (lpt(~'<= ~@(rest r)))))
(= f '/)
(if_branch
(str "{" (lpt (first r)) " over {" (lpt (~'* ~@(rest r))) "}}")) ; more than 1 parameters of the function "/" (= f '*) (if_branch (str (lpt (first r)) " times " (lpt(~'* ~@(rest r))))) ; more than 1 parameters of the function "*"
(= f '+)
;; brackets are missing - if the expression is complex (LispMathTranslator is called recursively again ) -- parentheses are added
;;; decoding - if a parenthesis with summation is multiplied (the multiplication operator is higher in the tree) - parentheses are added
;;; so far, they are added anyway during the summation operation - and unnecessary ones are manually removed in the report
(if_branch
(str "(" (lpt (first r)) " + " (lpt (~'+ ~@(rest r))) ")")) (= f '-) (if_branch (str "(" (lpt (first r)) " - " (lpt(~'- ~@(rest r))) ")"))
(= f 'sqrt)
(str "SQRT{" (lpt (second expr)) "}")
(= f 'expt)
(str "{" (lpt (second expr)) "}^{" (lpt (nth expr 2)) "}")
:else ;; if no one rule works, then I output the atom - expr
(str expr)))
(str expr))) ;; if the expression is not a list, I just output it
(defn LispMathTranslator [expr]
(lpt expr))
(defn -main []
(println (lpt '(<= N (* mg fi R A)))))