-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinfix-demo.el
38 lines (31 loc) · 1.19 KB
/
infix-demo.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;;; infix-demo.el -*- lexical-binding: t -*-
(defun nic-prefix->infix (prefix-expr separators)
"Converts a prefix expression to infix"
;;; this is from http://folk.uio.no/jornv/infpre/infpre.html
(noflet ((remove-brackets (lst)
"Reduses lists with just one item to the item itself"
(do ((result lst (car result)))
((or (not (consp result))
(not (null (cdr result)))) result)))
(insert-between (lst sep)
(if (or (not (consp lst))
(not (rest lst)))
lst
(cons (first lst)
(mapcan
(lambda (x) (list sep x))
(rest lst))))))
(let ((in-expr
(mapcar
(lambda (x)
(remove-brackets
(if (listp x)
(nic-prefix->infix x separators)
x)))
prefix-expr)))
(if (or (not (listp in-expr))
(not (member (first in-expr) separators)))
in-expr
(insert-between (rest in-expr) (first in-expr))))))
;; (nic-prefix->infix '(+ 1 2) '(+ - * /))
;;; infix-demo.el ends here