Lisp Scheme Part36

このエントリーをはてなブックマークに追加
926デフォルトの名無しさん
(defun infix-compiler (lst)
(cond ((atom lst) (if (symbolp lst) (symbol-value lst) lst))
((= (length lst) 3) (list (second lst) (infix-compiler (first lst)) (infix-compiler (third lst))))
(t (list (car lst) (mapcar #'infix-compiler (cdr lst))))))

昔書いた中置記法コンパイラだぜ
4行だぜ

(setq exp '((3 - 2) + ((8 + 4) / (8 - 4))))
(infix-compiler exp)
;=> (+ (- 3 2) (/ (+ 8 4) (- 8 4)))
;=> 4