summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/lisp-on-tex/lisp-util.sty
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/lisp-on-tex/lisp-util.sty
Initial commit
Diffstat (limited to 'macros/latex/contrib/lisp-on-tex/lisp-util.sty')
-rw-r--r--macros/latex/contrib/lisp-on-tex/lisp-util.sty64
1 files changed, 64 insertions, 0 deletions
diff --git a/macros/latex/contrib/lisp-on-tex/lisp-util.sty b/macros/latex/contrib/lisp-on-tex/lisp-util.sty
new file mode 100644
index 0000000000..17681f7f54
--- /dev/null
+++ b/macros/latex/contrib/lisp-on-tex/lisp-util.sty
@@ -0,0 +1,64 @@
+%%================================
+%% Definition of Utility Functions
+%%================================
+\makeatletter
+\lispinterp{%
+(\define \listQ (\lambda (\x) (\or (\pairQ \x) (\= () \x))))
+(\define \procedureQ (\lambda (\x)
+ (\or (\funcQ \x) (\closureQ \x) (\envfuncQ \x) (\macroQ \x))))
+(\define \atomQ (\lambda (\x)
+ (\and (\not (\pairQ \x)) (\not (\= () \x)) )))
+(\define \map (\lambda (\f.\args)
+ (\lispif (\= () (\car \args)) ()
+ (\let ((\cars (\@inner@map \car \args)) (\cdrs (\@inner@map \cdr \args)))
+ (\cons (\apply \f \cars)
+ (\apply \map (\cons \f \cdrs)))))))
+%% @inner@map : ('a -> 'b) -> 'a list -> 'b list
+(\define \@inner@map
+ (\lambda (\f \l)
+ (\lispif (\= () \l) () (\cons (\f (\car \l)) (\@inner@map \f (\cdr \l))))))
+%% not : bool -> bool
+(\define \not (\lambda (\b) (\lispif \b /f /t)))
+%% and : bool... -> bool
+(\define \and
+ (\lambda \l
+ (\lispif (\= () \l)
+ /t
+ (\lispif (\car \l) (\apply \and (\cdr \l)) /f))))
+%% or bool... -> bool
+(\define \or
+ (\lambda \l
+ (\lispif (\= () \l)
+ /f
+ (\lispif (\car \l) /t (\apply \or (\cdr \l))))))
+%% let bindings
+(\defmacro \let
+ (\lambda (\binds \body)
+ (\lispif (\= \binds ())
+ \body
+ (\list \let (\cdr \binds) (\list \@let (\car (\car \binds)) (\car (\cdr (\car \binds))) \body)))))
+(\defmacro \letM
+ (\lambda (\binds \body)
+ (\lispif (\= \binds ())
+ \body
+ (\list \letM (\cdr \binds) (\list \@mlet (\car (\car \binds)) (\car (\cdr (\car \binds))) \body)))))
+% letrec
+(\defmacro \letrec (\lambda (\binds \body) (\list \@letrec \binds \binds \body)))
+(\defmacro \@letrec
+ (\lambda (\binds \save \body)
+ (\lispif (\= \binds ())
+ (\list \@@letrec \save \body)
+ (\list \@letrec (\cdr \binds) \save
+ (\list \begin
+ (\list \setB (\car (\car \binds)) (\car (\cdr (\car \binds))))
+ \body)))))
+(\defmacro \@@letrec
+ (\lambda (\binds \body)
+ (\lispif (\= \binds ())
+ \body
+ (\list \@@letrec (\cdr \binds) (\list \@mlet (\car (\car \binds)) () \body)))))
+% nth
+(\define \nth (\lambda (\lst \n)
+ (\lispif (\= \n :0) (\car \lst) (\nth (\cdr \lst) (\- \n :1)))))
+}
+\makeatother \ No newline at end of file