(defn infix-to-prefix
"Convert from infix notation to prefix notation"
([col]
(cond
;; handle term only
(not (seq? col)) col
;; handle sequence containing one term (i.e. handle parens)
(= (count col) 1) (infix-to-prefix (first col))
;; handle all other cases
true (let [lowest (find-lowest-precedence col)]
(if (nil? lowest) ;; nothing to split
col
;; (a b c) bind a to hd, c to tl, and b to op
(let [[hd [op & tl]] (split-at lowest col)]
;; recurse
(list (translate-op op)
(infix-to-prefix hd)
(infix-to-prefix tl))))))))
Used in 0 other vars
Comments top
No comments for infix-to-prefix. Log in to add a comment.