<?xml version="1.0" encoding="UTF-8"?>

<rss version="2.0"
     xmlns:content="http://purl.org/rss/1.0/modules/content/"
     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:dc="http://purl.org/dc/elements/1.1/"
     xmlns:wfw="http://wellformedweb.org/CommentAPI/"
     >
  <channel>
    <atom:link href="http://kitchingroup.cheme.cmu.edu/blog/feed/index.xml" rel="self" type="application/rss+xml" />
    <title>The Kitchin Research Group</title>
    <link>https://kitchingroup.cheme.cmu.edu/blog</link>
    <description>Chemical Engineering at Carnegie Mellon University</description>
    <pubDate>Sat, 01 Nov 2025 13:47:46 GMT</pubDate>
    <generator>Blogofile</generator>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    
    <item>
      <title>A new and improved Emacs gnuplot DSL</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/05/05/A-new-and-improved-Emacs-gnuplot-DSL</link>
      <pubDate>Fri, 05 May 2017 10:26:00 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[plotting]]></category>
      <guid isPermaLink="false">SKkqBSazd8jxa4md_x_2Xt31v8A=</guid>
      <description>A new and improved Emacs gnuplot DSL</description>
      <content:encoded><![CDATA[


&lt;p&gt;
A significant limitation of the	&lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2017/05/04/An-emacs-lisp-dsl-for-gnuplot/"&gt;previous&lt;/a&gt; DSL I wrote is that all the plotting commands have to go in one macro. It would be nice to accumulate them in different forms, and when you want to run them all. A classic way to do that in Emacs lisp is to make a global variable, e.g. &lt;code&gt;*gnuplot-cmds*&lt;/code&gt; and append commands to it. Then when you want to, run the commands.
&lt;/p&gt;

&lt;p&gt;
A more modern approach is to use a closure to encapsulate the commands. Here is a &lt;a href="http://letoverlambda.com"&gt;"let over lambda"&lt;/a&gt; that defines a few functions that encapsulate an enclosed variable gnuplot-commands. We define one function to add commands to the list of commands, one to clear the commands, one to generate the gnuplot script as a string, and one to run the program. The enclosed variable &lt;code&gt;gnuplot-commands&lt;/code&gt; is basically only accessible by these functions. It is encapsulated, similar to if we defined a class in Python then made an instance of it with an attribute that was accessible only be instance methods. On one hand, this "protects" the variable, and keeps it out of the global namespace. On the other hand, we lose the documentation that would have come with a defvar, and we have to define a function to access the contents of that variable.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((gnuplot-commands '(&lt;span style="color: #008000;"&gt;"set terminal qt"&lt;/span&gt;)))

  (&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot-add-cmd&lt;/span&gt; (s)
    &lt;span style="color: #036A07;"&gt;"Append the command S to gnuplot-cmds."&lt;/span&gt;
    (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; gnuplot-commands (append gnuplot-commands (list s))))

  (&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot-clear&lt;/span&gt; ()
    (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; gnuplot-commands '(&lt;span style="color: #008000;"&gt;"set terminal qt"&lt;/span&gt;)))

  (&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot-script&lt;/span&gt; ()
    (s-join &lt;span style="color: #008000;"&gt;"\n"&lt;/span&gt; gnuplot-commands)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
gnuplot-script

&lt;/pre&gt;

&lt;p&gt;
To run the commands, we define this function. It does not need to be in the closure because it only accesses the commands through functions we defined in the closure.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot-show&lt;/span&gt; ()
    (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((temporary-file-directory &lt;span style="color: #008000;"&gt;"."&lt;/span&gt;)
           (cmdfile (make-temp-file &lt;span style="color: #008000;"&gt;"gnuplot-cmds-"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;".gpl"&lt;/span&gt;))
           (shellcmd (format &lt;span style="color: #008000;"&gt;"gnuplot --persist -c \"%s\""&lt;/span&gt; cmdfile))
           (cmds (gnuplot-script)))
      (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; cmdfile
        (insert cmds))
      (shell-command shellcmd)
      (delete-file cmdfile)
      cmds))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
gnuplot-show

&lt;/pre&gt;

&lt;p&gt;
Last time I noted I had a new idea for the DSL syntax that would give us more flexibility to inject variables and code into the DSL. The idea is to use keywords, symbols that start with :, to indicate they should be replaced by the value of the non-keyword symbol in the environment, and for any form that starts with : to evaluate that form. So, (: - 5 4) would get replaced by 1. Here is the new macro for that.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;kargs&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #036A07;"&gt;"Convert symbols to strings, quote strings, and (expr) to what they evaluate to."&lt;/span&gt;
  `(s-join &lt;span style="color: #008000;"&gt;" "&lt;/span&gt; (list ,@(cl-mapcan
                        (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (s)
                          (list
                           (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
                            ((keywordp s)
                             (format &lt;span style="color: #008000;"&gt;"%s"&lt;/span&gt;
                                     (symbol-value (intern (substring (symbol-name s) 1)))))
                            ((symbolp s)
                             (symbol-name s))
                            ((stringp s)
                             (format &lt;span style="color: #008000;"&gt;"\"%s\""&lt;/span&gt; s))
                            ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (listp s) (eq : (car s)))
                             `(&lt;span style="color: #0000FF;"&gt;with-output-to-string&lt;/span&gt;
                                (princ ,(cdr s))))
                            (t
                             (format &lt;span style="color: #008000;"&gt;"%s"&lt;/span&gt; s)))))
                        args))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
kargs

&lt;/pre&gt;

&lt;p&gt;
Now, our gnuplot macro is simpler, since all it does is add commands to the list. If the form is a string, we add it as is, if the form starts with (: stuff) we evaluate the cdr of the form, and otherwise, we pass the form contents to the kargs macro for processing.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; forms)
  `(&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for s in (list ,@(mapcar (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x)
                                    (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
                                     ((stringp x)
                                      x)
                                     ((&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; (listp x) (eq : (car x)))
                                      `,(cdr x))
                                     (t
                                      `(kargs ,@x))))
                                  forms))
         do (gnuplot-add-cmd s)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
gnuplot

&lt;/pre&gt;

&lt;p&gt;
What did that gain us? First, we can break up a script so we can talk about it, maybe do some calculations, etc&amp;#x2026; Let's look at the example at &lt;a href="http://gnuplot.sourceforge.net/demo/linkedaxes.html"&gt;http://gnuplot.sourceforge.net/demo/linkedaxes.html&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
We can start with the basic settings.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(gnuplot-clear)

(gnuplot
 (set terminal png)
 (set output &lt;span style="color: #008000;"&gt;"linkedaxes.png"&lt;/span&gt;)
 (set encoding utf8)
 (set key outside Left)
 (set bmargin 5)
 (set tmargin 6)
 (set style data lines)
 (set tics in)
 (set ticslevel 0.5)
 (set xlabel  &lt;span style="color: #008000;"&gt;"X-ray energy in eV"&lt;/span&gt;)

 (set format y  \'%5.1fe\')
 (set title &lt;span style="color: #008000;"&gt;" Anomalous scattering factors "&lt;/span&gt;)
 (set xrange  [9000:14400])
 (set offset 0\,0\,1.0\,0)
 (set xtics nomirror)
 (set link x via 12398./x inverse 12398./x)

 (set x2label  &lt;span style="color: #008000;"&gt;"X-ray wavelength in &amp;#197;"&lt;/span&gt;)
 (set x2tics 0.1  format &lt;span style="color: #008000;"&gt;"%.1f &amp;#197;"&lt;/span&gt; nomirror))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
We need to download some data files. We can do that, and add another line to the gnuplot script. The escaping on the quotes and commas is especially tedious in this one ;) but, we don't need those pesky line-continuations here.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(shell-command &lt;span style="color: #008000;"&gt;"wget http://skuld.bmsc.washington.edu/scatter/data/Br.dat"&lt;/span&gt;)
(shell-command &lt;span style="color: #008000;"&gt;"wget http://skuld.bmsc.washington.edu/scatter/data/Ta.dat"&lt;/span&gt;)


(gnuplot
 (plot &lt;span style="color: #008000;"&gt;"Br.dat"&lt;/span&gt; volatile using 1:3 title \'Br f\"\'  lt 1 lw 3\, \'\' volatile using 1:2 title &lt;span style="color: #008000;"&gt;"Br f'"&lt;/span&gt;  lt 1 lw 1\,
       &lt;span style="color: #008000;"&gt;"Ta.dat"&lt;/span&gt; volatile using 1:3 title \'Ta f\"\' lt 2 lw 3\, \'\' volatile using 1:2 title \"Ta f\'\"  lt 2 lw 1))

(gnuplot-script)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
set terminal qt
set terminal png
set output "linkedaxes.png"
set encoding utf8
set key outside Left
set bmargin 5
set tmargin 6
set style data lines
set tics in
set ticslevel 0.5
set xlabel "X-ray energy in eV"
set format y '%5.1fe'
set title " Anomalous scattering factors "
set xrange [9000:14400]
set offset 0,0,1.0,0
set xtics nomirror
set link x via 12398./x inverse 12398./x
set x2label "X-ray wavelength in Å"
set x2tics 0.1 format "%.1f Å" nomirror
plot "Br.dat" volatile using 1:3 title 'Br f"' lt 1 lw 3, '' volatile using 1:2 title "Br f'" lt 1 lw 1, "Ta.dat" volatile using 1:3 title 'Ta f"' lt 2 lw 3, '' volatile using 1:2 title "Ta f'" lt 2 lw 1
&lt;/pre&gt;

&lt;p&gt;
Finally, we can set the output to png, and run our program.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(gnuplot-show)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Looks good.
&lt;/p&gt;



&lt;p&gt;
&lt;img src="/media/linkedaxes.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
What about the fancy keyword formatting? Here is an example of that in action. :term gets replaced by the term variable, :png gets replaced by the filename, and :x is replaced by 4.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(gnuplot-clear)
(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((x 4)
      (term &lt;span style="color: #008000;"&gt;"png"&lt;/span&gt;)
      (png &lt;span style="color: #008000;"&gt;"\"polar.png\""&lt;/span&gt;))
  (gnuplot
   (set terminal &lt;span style="color: #006FE0;"&gt;:term&lt;/span&gt;)
   (set output &lt;span style="color: #006FE0;"&gt;:png&lt;/span&gt;)
   (set polar)
   (set dummy t)
   (plot sin\( &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; *t\) \,cos\( &lt;span style="color: #006FE0;"&gt;:x&lt;/span&gt; *t\))
   (set offset 0\,0\,0\,0)))

(gnuplot-show)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
set terminal qt
set terminal png
set output "polar.png"
set polar
set dummy t
plot sin( 4 *t) ,cos( 4 *t)
set offset 0,0,0,0

&lt;/pre&gt;

&lt;p&gt;
&lt;img src="/media/polar.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
There are a few nuances I didn't expect. First, you have to escape the parentheses in this case because otherwise it looks like a form that will be ignored. Second, you have to quote the string to get quotes into the gnuplot script. Third, there has to be a space before and after the keywords for emacs to parse it correctly and do the substitution.
&lt;/p&gt;

&lt;p&gt;
Let's look at one last example that uses the (: form). We reproduce a figure from &lt;a href="http://gnuplot.sourceforge.net/demo/transparent_solids.html"&gt;http://gnuplot.sourceforge.net/demo/transparent_solids.html&lt;/a&gt; here.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(gnuplot-clear)
(gnuplot
 (set terminal pngcairo  background &lt;span style="color: #008000;"&gt;"#ffffff"&lt;/span&gt; enhanced font &lt;span style="color: #008000;"&gt;"arial,9"&lt;/span&gt; fontscale 1.0 size 512\, 384 )
 (set output &lt;span style="color: #008000;"&gt;"transparent-solids.png"&lt;/span&gt;)
 &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;construct the title&lt;/span&gt;
 (set title (: format &lt;span style="color: #008000;"&gt;"\"%s\""&lt;/span&gt; (concat &lt;span style="color: #008000;"&gt;"Interlocking Tori - PM3D surface"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"with depth sorting and transparency"&lt;/span&gt;)))

 &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;use lisp code to create a gnuplot command&lt;/span&gt;
 (: concat &lt;span style="color: #008000;"&gt;"unset"&lt;/span&gt; &lt;span style="color: #008000;"&gt;" "&lt;/span&gt; &lt;span style="color: #008000;"&gt;"border"&lt;/span&gt;)

 (unset key)
 (set object 1 rect from screen 0\, 0\, 0 to screen 1\, 1\, 0 behind)
 (set object 1 rect fc  rgb \"gray\"  fillstyle solid 1.0  border -1)
 (set view 64\, 345\, 1.24375\, 0.995902)
 (set isosamples 50\, 20)
 (unset xtics)
 (unset ytics)
 (unset ztics)
 (set dummy u\,v)
 (set parametric)
 (set urange [ -pi : pi ])
 (set vrange [ -pi : pi ])

 (set style fill  transparent solid 0.30 border)
 (set pm3d depthorder)
 (set palette rgbformulae 8\, 9\, 7)
 (set pm3d interpolate 1\,1 flush begin noftriangles border lt black linewidth 0.500 dashtype solid corners2color mean)
 (set colorbox vertical origin screen 0.9\, 0.2\, 0 size screen 0.05\, 0.6\, 0 front  noinvert bdefault)

 (splot (: concat &lt;span style="color: #008000;"&gt;"cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v) with pm3d,"&lt;/span&gt;
           &lt;span style="color: #008000;"&gt;"1+cos(u)+.5*cos(u)*cos(v),.5*sin(v),sin(u)+.5*sin(u)*cos(v) with pm3d"&lt;/span&gt;)))
(gnuplot-show)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
set terminal qt
set terminal pngcairo background "#ffffff" enhanced font "arial,9" fontscale 1.0 size 512, 384
set output "transparent-solids.png"
set title "Interlocking Tori - PM3D surfacewith depth sorting and transparency"
unset border
unset key
set object 1 rect from screen 0, 0, 0 to screen 1, 1, 0 behind
set object 1 rect fc rgb "gray" fillstyle solid 1.0 border -1
set view 64, 345, 1.24375, 0.995902
set isosamples 50, 20
unset xtics
unset ytics
unset ztics
set dummy u,v
set parametric
set urange [-pi : pi]
set vrange [-pi : pi]
set style fill transparent solid 0.3 border
set pm3d depthorder
set palette rgbformulae 8, 9, 7
set pm3d interpolate 1,1 flush begin noftriangles border lt black linewidth 0.5 dashtype solid corners2color mean
set colorbox vertical origin screen 0.9, 0.2, 0 size screen 0.05, 0.6, 0 front noinvert bdefault
splot cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v) with pm3d,1+cos(u)+.5*cos(u)*cos(v),.5*sin(v),sin(u)+.5*sin(u)*cos(v) with pm3d
&lt;/pre&gt;


&lt;p&gt;
&lt;img src="/media/transparent-solids.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
Overall this seems like an improvement to the DSL. I didn't invent the idea of reusing keywords this way out of the blue. In On Lisp, Paul graham uses "special" variable names in Chapter 18, where he shows how to use gensyms for special purposes, and also variables with special names like ?x. Even Emacs is using a variation of this idea. Check out this &lt;a href="http://endlessparentheses.com/new-on-elpa-and-in-emacs-25-1-let-alist.html"&gt;new let-alist&lt;/a&gt; macro:
&lt;/p&gt;


&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let-alist&lt;/span&gt; '((x . 5))
  (+ 1 .x))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
6

&lt;/pre&gt;

&lt;p&gt;
There is a special variable inside the body that is a dot-name. The macro expands to provide a value for that symbol. I wonder if I should have tried to use an approach like this instead. Maybe another day. After I read and study the four defuns and single defmacro that make this possible!
&lt;/p&gt;

&lt;p&gt;
You can see here what happens:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(macroexpand '(let-alist '((x . 5))
  (+ 1 .x)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt;
    ((alist
      '((x . 5))))
  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((\.x (cdr (assq 'x alist))))
    (+ 1 \.x)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
The macro builds up an internal alist for the dot-names.
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2018 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/05/05/A-new-and-improved-Emacs-gnuplot-DSL.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.1.6&lt;/p&gt;
]]></content:encoded>
    </item>
    <item>
      <title>An emacs-lisp dsl for gnuplot</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/05/04/An-emacs-lisp-dsl-for-gnuplot</link>
      <pubDate>Thu, 04 May 2017 19:33:55 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[plotting]]></category>
      <guid isPermaLink="false">0UZjXdfBOloIK7bnw4odvEI2ep0=</guid>
      <description>An emacs-lisp dsl for gnuplot</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#orge24f7e9"&gt;1. Embedding Python or gnuplot&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#orgfe7800b"&gt;2. An alternative approach using a DSL&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#orga773a9d"&gt;3. Summary&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;
Plotting is a pretty general feature we need in scientific work. In this post we examine a way we could get at least minimal plotting into Emacs-lisp with as lispy a syntax as reasonable.
&lt;/p&gt;

&lt;div id="outline-container-orge24f7e9" class="outline-2"&gt;
&lt;h2 id="orge24f7e9"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Embedding Python or gnuplot&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
With org-mode we can fluidly integrate many languages in one document. That is not the goal here, where I want to integrate plotting into a program. You certainly could go this route to embed python programs in your lisp programs for plotting.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;python&lt;/span&gt; (code)
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((temporary-file-directory &lt;span style="color: #008000;"&gt;"."&lt;/span&gt;)
        (tmpfile (make-temp-file &lt;span style="color: #008000;"&gt;"py-"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;".py"&lt;/span&gt;)))
    (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; tmpfile
      (insert code))
    (shell-command-to-string (format &lt;span style="color: #008000;"&gt;"python %s"&lt;/span&gt; tmpfile))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is that function in action.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(python &lt;span style="color: #008000;"&gt;"import matplotlib.pyplot as plt&lt;/span&gt;
&lt;span style="color: #008000;"&gt;import numpy as np&lt;/span&gt;
&lt;span style="color: #008000;"&gt;x = np.linspace(0, 1)&lt;/span&gt;
&lt;span style="color: #008000;"&gt;y = np.exp(x)&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.plot(x, y, label='data')&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.title('A Title')&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.xlim([0, 1])&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.ylim([1, 2.75])&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.xlabel('x')&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.ylabel('y')&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.legend()&lt;/span&gt;
&lt;span style="color: #008000;"&gt;plt.savefig('figpy.png')"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
And the corresponding figure:
&lt;img src="/media/figpy.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
This is irritating for a few reasons. One is it is annoying to write python programs in string form; you don't get much editor support for indentation or syntax highlighting, and you have to be careful with quotes. It is not that easy to switch that snippet to Python mode either. You are pretty limited in writing programs that expand and modify the code too. Basically you have to do that all by string manipulation.
&lt;/p&gt;

&lt;p&gt;
Along these lines, you could imagine a gnuplot function. It ends up not being much better.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot&lt;/span&gt; (cmds)
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((temporary-file-directory &lt;span style="color: #008000;"&gt;"."&lt;/span&gt;)
         (cmdfile (make-temp-file &lt;span style="color: #008000;"&gt;"gnuplot-cmds-"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;".gpl"&lt;/span&gt;))
         (shellcmd (format &lt;span style="color: #008000;"&gt;"gnuplot --persist -c \"%s\""&lt;/span&gt; cmdfile)))
    (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; cmdfile
      (insert cmds))
    (shell-command shellcmd)
    (delete-file cmdfile)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
You use this the same way.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;gnuplot&lt;/span&gt; &lt;span style="color: #008000;"&gt;"set title \"Simple Plots\" font \",20\"&lt;/span&gt;
&lt;span style="color: #008000;"&gt;set key left box&lt;/span&gt;
&lt;span style="color: #008000;"&gt;set samples 50&lt;/span&gt;
&lt;span style="color: #008000;"&gt;set style data points&lt;/span&gt;
&lt;span style="color: #008000;"&gt;set terminal png&lt;/span&gt;
&lt;span style="color: #008000;"&gt;set output \"gnuplot.png\"&lt;/span&gt;

&lt;span style="color: #008000;"&gt;plot [-10:10] sin(x),atan(x),cos(atan(x))"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;img src="/media/gnuplot.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
It has the same limitations as our string-based Python solution. The benefit of them is the native command structure for Python or gnuplot is used, so anything they can do you can too.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-orgfe7800b" class="outline-2"&gt;
&lt;h2 id="orgfe7800b"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; An alternative approach using a DSL&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;p&gt;
As an alternative, we consider here a domain specific language (DSL) that maps onto gnuplot. Suppose we could do this instead.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;gnuplot&lt;/span&gt;
 (set terminal png)
 (set output &lt;span style="color: #008000;"&gt;"test.png"&lt;/span&gt;)
 (set title &lt;span style="color: #008000;"&gt;"Simple Plots"&lt;/span&gt; font &lt;span style="color: #008000;"&gt;",20"&lt;/span&gt;)
 (set key left box)
 (set samples 50)
 (set style data points)

 (&lt;span style="color: #0000FF;"&gt;plot&lt;/span&gt; [-10:10] sin\(x\) \,atan\(x\) \,cos\(atan\(x\)\)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is the figure from that code. The most annoying part of this is in the plot function we have to escape all the parentheses and commas, but otherwise it looks pretty lispy. The output of that program is the gnuplot commands that were generated for making the plot.
&lt;img src="/media/test.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
This retains a lot of benefits of programming in lisp. &lt;code&gt;gnuplot&lt;/code&gt; has to be a macro though because we do not want to evaluate the s-expressions inside as lisp. For starters they just look lispy, I don't actually use them as lisp at all. Instead we transform them to the gnuplot code.
&lt;/p&gt;

&lt;p&gt;
In the following code, I will develop the gnuplot macro. It has some sticky and tricky points, and it is not obvious it will support all the features of gnuplot, but I learned a lot doing it that I will share here.
&lt;/p&gt;

&lt;p&gt;
Starting with a simple form inside the macro, I wanted to convert (set output "test.png") to "set output \"test.png\"". For this DSL, I want to treat every symbol in the form as if it should be turned into a string, anything that is a string should be quoted, and anything that is in parentheses (i.e. it passes listp) should be evaluated and converted to a string. Then all those strings should be joined by spaces. Here is a macro that does that (adapted from a solution at &lt;a href="https://emacs.stackexchange.com/questions/32558/eval-some-arguments-in-a-macro/32570?noredirect=1#comment50186_32570"&gt;https://emacs.stackexchange.com/questions/32558/eval-some-arguments-in-a-macro/32570?noredirect=1#comment50186_32570&lt;/a&gt;).
&lt;/p&gt;

&lt;p&gt;
There are a couple of corner cases that are handled here. If the arg is a string, we quote it.  If the arg is not a symbol or string, then it is evaluated and converted to a string. Importantly, this is done in the run environment though, so we can inject variables into the gnuplot code.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;gargs&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #036A07;"&gt;"Convert symbols to strings, quote strings, and (expr) to what they evaluate to."&lt;/span&gt;
  `(s-join &lt;span style="color: #008000;"&gt;" "&lt;/span&gt; (list ,@(cl-mapcan
                        (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (s)
                          (list
                           (&lt;span style="color: #0000FF;"&gt;cond&lt;/span&gt;
                            ((symbolp s)
                             (symbol-name s))
                            ((stringp s)
                             (format &lt;span style="color: #008000;"&gt;"\"%s\""&lt;/span&gt; s))
                            (t
                             `(&lt;span style="color: #0000FF;"&gt;with-output-to-string&lt;/span&gt;
                                (princ ,s))))))
                        args))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here are a few examples of how it works. The loop is just to get a vertical table in org-mode for the blog post.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for s in
      (list (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; set key title &lt;span style="color: #008000;"&gt;"before fit"&lt;/span&gt; size \, (+ 5 5))
            (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; set title &lt;span style="color: #008000;"&gt;"red"&lt;/span&gt;)
            (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; set yrange [0:*])
            (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; &lt;span style="color: #008000;"&gt;"5"&lt;/span&gt;)
            (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((x 6)) (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; (identity x)))
            (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; 'x)
            (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; '(x))
            (&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; set label 1 &lt;span style="color: #008000;"&gt;"plot for [n=2:10] sin(x*n)/n"&lt;/span&gt; at graph .95\, graph .92 right))
      collect
      (list s))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
A limitation of this is that we either have quote things like parentheses, commas, semi-colons and sometimes square brackets:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; plot for [n=2:10] sin\(x*n\)/n notitle lw \(13-n\)/2)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Or we have to use the string form instead; we can always fall back to that.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; &lt;span style="color: #008000;"&gt;"plot for [n=2:10] sin(x*n)/n notitle lw (13-n)/2"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
The macro above will do the grunt work on each form in the gnuplot macro. Finally, for the gnuplot macro, I want to take all the forms, convert them to gnuplot commands, write them to a temporary file, and then run gnuplot on the file, and finally delete the temp file. I assume we start with a gui terminal so graphs pop up unless you change it in your macro body. Here is that macro. It returns the generated code so it easy to see if you got the right program.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;gnuplot&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; forms)
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((temporary-file-directory &lt;span style="color: #008000;"&gt;"."&lt;/span&gt;)
         (cmdfile (make-temp-file &lt;span style="color: #008000;"&gt;"gnuplot-cmds-"&lt;/span&gt; nil &lt;span style="color: #008000;"&gt;".gpl"&lt;/span&gt;))
         (shellcmd (format &lt;span style="color: #008000;"&gt;"gnuplot --persist -c \"%s\""&lt;/span&gt; cmdfile))
         (cmd-string))
    `(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((cmd-string (s-join &lt;span style="color: #008000;"&gt;"\n"&lt;/span&gt; (list ,@(mapcar (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x)
                                                      (&lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; (stringp x)
                                                          x
                                                        `(&lt;span style="color: #0000FF;"&gt;gargs&lt;/span&gt; ,@x)))
                                                    forms)))))
       (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; ,cmdfile
         (insert &lt;span style="color: #008000;"&gt;"set terminal qt\n"&lt;/span&gt;)
         (insert cmd-string)
         (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; cmd-string (buffer-string)))
       (shell-command ,shellcmd)
       (delete-file ,cmdfile)
       cmd-string)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is a figure adapted from &lt;a href="http://gnuplot.sourceforge.net/demo/iterate.html"&gt;http://gnuplot.sourceforge.net/demo/iterate.html&lt;/a&gt;. I use the string form for the last line to avoid escaping all the special characters.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;gnuplot&lt;/span&gt;
 (set terminal png)
 (set output &lt;span style="color: #008000;"&gt;"iteration.png"&lt;/span&gt;)
 (set title &lt;span style="color: #008000;"&gt;"Iteration within plot command"&lt;/span&gt;)
 (set xrange [0:3])
 (set label 1 &lt;span style="color: #008000;"&gt;"plot for [n=2:10] sin(x*n)/n"&lt;/span&gt; at graph .95\, graph .92 right)
 &lt;span style="color: #008000;"&gt;"plot for [n=2:10] sin(x*n)/n notitle lw (13-n)/2"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is the resulting figure.
&lt;/p&gt;

&lt;p&gt;
&lt;img src="/media/iteration.png"&gt; 
&lt;/p&gt;

&lt;p&gt;
That is overall pretty sweet. There is a little dissonance between the strings, escaped comma, etc.., and it is not terribly ideal for integrating with regular lisp code inside the macro yet. That seems to be a feature of my choice to use (expr) as the syntax to evaluate a form. It means you have to do some gymnastics to get some s-expressions into the graphs. For example below I use a couple of variables to inject values. To get a string I have to use format to add the extra quotes, and to get the number I have to use the identity function. I also used escaped characters in the last line to see the difference.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((ts &lt;span style="color: #008000;"&gt;"Iteration and substitution"&lt;/span&gt;)
      (x0 0)
      (xf 3)
      (g1 0.95)
      (g2 0.92))
  (&lt;span style="color: #0000FF;"&gt;gnuplot&lt;/span&gt;
   (set terminal png)
   (set output &lt;span style="color: #008000;"&gt;"iteration-2.png"&lt;/span&gt;)
   (set title (format &lt;span style="color: #008000;"&gt;"\"%s\""&lt;/span&gt; ts))
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Note the escaped square brackets&lt;/span&gt;
   (set xrange \[ (identity x0) : (identity xf) \])
   (set label 1 &lt;span style="color: #008000;"&gt;"plot for [n=2:10] sin(x*n)/n"&lt;/span&gt; at graph (identity g1) \, graph (identity g2) right)
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;note here I escaped the parentheses!&lt;/span&gt;
   (&lt;span style="color: #0000FF;"&gt;plot&lt;/span&gt; for [n=2:10] sin\(x*n\)/n notitle lw \(13-n\)/2)))
&lt;/pre&gt;
&lt;/div&gt;


&lt;p&gt;
&lt;img src="/media/iteration-2.png"&gt; 
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;



&lt;div id="outline-container-orga773a9d" class="outline-2"&gt;
&lt;h2 id="orga773a9d"&gt;&lt;span class="section-number-2"&gt;3&lt;/span&gt; Summary&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;
&lt;p&gt;
For the simple plots here, my DSL worked ok. There is a tradeoff in the syntax I chose that has some consequences. We cannot use the values of symbols in this DSL without resorting to hackery like (identity sym). We also cannot use the infix notation for sin(x) without quoting it as "sin(x)" or escaping the parentheses, e.g. &lt;code&gt;sin\(x\)&lt;/code&gt;, likewise square brackets which lisp will read  as a vector. Commas have to be escaped, which is probably an emacs-lisp issue. To address that would require a reader macro which emacs-lisp does not have great support for. I am calling this experiment done for now. I have another syntax idea to try out another day.
&lt;/p&gt;

&lt;p&gt;
Here is a preview of what it might look like. It is basically the same but I reuse keywords to indicate that :x0 should be replaced by whatever x0 evaluates to, and (: - 1 0.05) should be evaluated. The special character escaping is still there of course, since that is a limitation of the emacs lisp reader I think. I might try using x0? and (? - 1 0.05) instead. That might be less confusing. I like that the keywords are syntax highlighted for free though, and you can't use them for anything else.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((ts &lt;span style="color: #008000;"&gt;"Iteration and substitution"&lt;/span&gt;)
      (x0 0)
      (xf 3)
      (g2 0.92))
  (&lt;span style="color: #0000FF;"&gt;gnuplot&lt;/span&gt;
   (set terminal png)
   (set output &lt;span style="color: #008000;"&gt;"iteration-2.png"&lt;/span&gt;)
   (set title &lt;span style="color: #006FE0;"&gt;:ts&lt;/span&gt;)
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;Note the escaped square brackets&lt;/span&gt;
   (set xrange \[ &lt;span style="color: #006FE0;"&gt;:x0&lt;/span&gt; : &lt;span style="color: #006FE0;"&gt;:xf&lt;/span&gt; \])
   (set label 1 &lt;span style="color: #008000;"&gt;"plot for [n=2:10] sin(x*n)/n"&lt;/span&gt; at graph (&lt;span style="color: #0000FF;"&gt;:&lt;/span&gt; - 1 0.05) \, graph &lt;span style="color: #006FE0;"&gt;:g2&lt;/span&gt; right)
   &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;note here I escaped the parentheses!&lt;/span&gt;
   (&lt;span style="color: #0000FF;"&gt;plot&lt;/span&gt; for [n=2:10] sin(x*n)/n notitle lw (13-n)/2)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
This has the benefit of a little cleaner injection of variables and selective execution of parenthetical expressions, we will just ignore any that don't pass (= (car expr) :). That May not work for sin((: + 1 1) x) though, unless I escape the outer parentheses too.&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2017 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/05/04/An-emacs-lisp-dsl-for-gnuplot.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0.5&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Emulating Sparql queries in emacs-lisp with pattern matching</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2017/04/30/Emulating-Sparql-queries-in-emacs-lisp-with-pattern-matching</link>
      <pubDate>Sun, 30 Apr 2017 13:46:47 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[emacs]]></category>
      <guid isPermaLink="false">R3y1c_-hnfEQdQtr5ieB1MNVUk8=</guid>
      <description>Emulating Sparql queries in emacs-lisp with pattern matching</description>
      <content:encoded><![CDATA[


&lt;p&gt;
Sqarql is a query language for RDF triples. A triple is a data structure that consists of a (subject predicate object). Sparql lets you query the triples to extract data from them. I have been interested in using these to augment the &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2017/01/03/Find-stuff-in-org-mode-anywhere/"&gt;SQL databases I generate&lt;/a&gt; from my org-files to be able to infer relationships between subjects and objects. For example, I could encode relationships into the contact database I use, and then infer new information that is not encoded explicitly. So far though I haven't found a good Sparql database that I can easily integrate into Emacs (or even play around with). I am reading On Lisp these days and chapters 18 and 19 talk about destructuring and pattern matching, and I realized these can be used to implement something like Sparql queries on simple lisp data structures. In this post I explore what it looks like and how to do it.
&lt;/p&gt;

&lt;p&gt;
Let's consider a small database of triples that codify relationships between two people. For example, we can codify that Ann is Bob's mother with (Bob mother Ann). Here is our little database.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; triples '((Bob mother Ann)
                (Bill father Bob)
                (Lucy mother Jane)
                (Bob wife Jane)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
We can filter out facts from the database with a -filter. Here we filter out triples about Bob. Emacs has nice pattern matching in the pcase macro (see &lt;a href="http://www.wilfred.me.uk/blog/2017/03/19/pattern-matching-in-emacs-lisp/"&gt;http://www.wilfred.me.uk/blog/2017/03/19/pattern-matching-in-emacs-lisp/&lt;/a&gt; and &lt;a href="http://newartisans.com/2016/01/pattern-matching-with-pcase/"&gt;http://newartisans.com/2016/01/pattern-matching-with-pcase/&lt;/a&gt; for example). It turns out this is an amazing way to solve this problem. Here we look at triples with the pattern that they start with Bob.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(-filter (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (triple) (&lt;span style="color: #0000FF;"&gt;pcase&lt;/span&gt; triple (`(Bob ,_ ,_) t))) triples)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
And here we get all the mothers.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(-filter (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (triple) (&lt;span style="color: #0000FF;"&gt;pcase&lt;/span&gt; triple (`(,_ mother ,_) t))) triples)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
We can infer some facts about these people from the database by using some "rules". For example, there is not an entry that tells us directly who Bill's grandmother is. If we assume that the mother of a person's father is their grandmother, then we can infer Bill's grandmother is Ann. In this post, we examine how to write code that can find that answer. We will use pattern matching on pairs of triples to do it.
&lt;/p&gt;

&lt;p&gt;
We can enumerate pairs of triples, and use pattern matching to find the pair of triples that meet the criteria we specify. The criteria we want to match is (in pseudo-sparql):
&lt;/p&gt;

&lt;pre class="example"&gt;
(Bill father ?dad) (?dad mother ?grandmother)
&lt;/pre&gt;

&lt;p&gt;
In other words, we want to find a triple that contains Bill as the subject, father as the predication, and then his father will be the object, and then find another triple that matches a predicate of mother with the subject equal to the father object we found in the first triple, and the object of the second triple will be Bill's grandmother. We enumerate pairs of triples for the comparison. Here is a way to do that. It is not a very efficient way to do it; it would be better to first filter out the triples that match (Bill father &lt;i&gt;something&lt;/i&gt;) and then filter out the triples that match (&lt;i&gt;anything&lt;/i&gt; mother &lt;i&gt;any other thing&lt;/i&gt;) &lt;i&gt;and&lt;/i&gt; then consider the pairs of those triples. I will save that for another day; efficiency is not the point today ;)
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i below (length triples)
      append
      (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt;
       for j below (length triples)
       if (not (= i j))
       collect
       (list (nth i triples) (nth j triples))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
You can see the pair that matches is the fourth one down (actually the first one matches too, but not exactly in the order of the pattern we specified). Next, we use pcase for the pattern matching. This amazing macro allows you to specify a pattern in terms of reusable variables so we can specify that the same value exists in multiple places. We will use this pattern (in pcase syntax):
&lt;/p&gt;

&lt;pre class="example"&gt;
`((Bill father ,dad) (,dad mother ,grandmother))
&lt;/pre&gt;

&lt;p&gt;
That means match a list that has the first element of (Bill father &lt;i&gt;something&lt;/i&gt;) and store the value of &lt;i&gt;something&lt;/i&gt; in the variable dad. The second element of the list must match (&lt;i&gt;something&lt;/i&gt; mother &lt;i&gt;another thing&lt;/i&gt;) and store the value of &lt;i&gt;another thing&lt;/i&gt; in the variable grandmother. The two variables dad and grandmother are then available in the body of the pcase statement. Here is the code to loop over the triples and return the result when we find a match.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;catch&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;result&lt;/span&gt;
  (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i below (length triples)
        do
        (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt;
         for j below (length triples)
         if (not (= i j))
         collect
         (&lt;span style="color: #0000FF;"&gt;pcase&lt;/span&gt; (list (nth i triples) (nth j triples))
           (`((Bill father ,dad) (,dad mother ,grandmother))
            (&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;result&lt;/span&gt; (format &lt;span style="color: #008000;"&gt;"Bill's dad is %s and his grandmother is %s"&lt;/span&gt; dad grandmother)))))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Not bad. It would be worthwhile to encapsulate that into a macro perhaps, so you could just write something like this:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;select&lt;/span&gt; (dad grandmother) from triples where `((Bill father ,dad) (,dad mother ,grandmother)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
For fun I implemented a limited version of this below. It is fairly limited, and lightly tested. The biggest limitation is we hard-coded the search over pairs of triples. This version searches by brute force too, because I don't know how to build in filtering yet. It is another exercise for another day to remove these limitations. Here I just want to try out the macro with the syntactic sugar of "from" and "where" (which are totally ignored) as well at the backquoted query.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;select&lt;/span&gt; (&lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; args)
  (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((values (first args))
        (db (third args))
        (query (fifth args)))
    `&lt;span style="color: #D0372D;"&gt;(catch &lt;/span&gt;'&lt;span style="color: #D0372D;"&gt;result&lt;/span&gt;
       (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for i below (length ,db)
             do
             (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt;
              for j below (length ,db)
              if (not (= i j))
              do
              (&lt;span style="color: #0000FF;"&gt;pcase&lt;/span&gt; (list (nth i triples) (nth j triples))
                (,query
                 (&lt;span style="color: #0000FF;"&gt;throw&lt;/span&gt; '&lt;span style="color: #D0372D;"&gt;result&lt;/span&gt; (list ,@values)))))))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is a fun way to write the query that finds the grandmother of the person named Bill with variable capture.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;select&lt;/span&gt; (person dad grandmother) from triples
        where `((,(&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; person (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; Bill person)) father ,dad) (,dad mother ,grandmother)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
We can see the grandmother is Ann, as we found before.
&lt;/p&gt;

&lt;p&gt;
Let's have a look at the macro expansion. Clearly our macro hides a lot of work from us!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(macroexpand '(select (person dad grandmother) from triples
        where `((,(&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; person (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; Bill person)) father ,dad) (,dad mother ,grandmother))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;

&lt;col  class="org-left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="org-left"&gt;Bill&lt;/td&gt;
&lt;td class="org-left"&gt;Bob&lt;/td&gt;
&lt;td class="org-left"&gt;Ann&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
How about another example query. Who is Lucy's dad? The most direct query would be `(Lucy father ,dad), but a) that fact is not in the database, and b) our select macro won't search a single query anyway. So, let's examine how to find the answer by inference.
&lt;/p&gt;

&lt;p&gt;
Let's assume that Lucy's dad is also the husband of her mother. Let's also assume that we can infer that if we know Jane is the wife of Bob, then Bob is the husband of Jane, and so we can infer from our database that Bob is Lucy's dad. This results in a query on a pair of triples that matches a pattern like:
&lt;/p&gt;

&lt;pre class="example"&gt;
(Lucy mother ?mom) (?dad wife ?mom)
&lt;/pre&gt;

&lt;p&gt;
Here is that query in our select macro.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;
&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;select&lt;/span&gt; (person mom dad) from triples
        where `((,(&lt;span style="color: #0000FF;"&gt;and&lt;/span&gt; person (&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; Lucy person)) mother ,mom) (,dad wife ,mom)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Pretty cool! Clearly there is still a lot to do to make this practical. The implementation I used here wouldn't scale well with large numbers of triples, and its limited to a single kind of query. Chapters 18 and 19 in On Lisp address the query limitation (and they are not even limited to triples) and a different syntax style that is more Sparql like. When I get through them, I will probably add a new post on it. There are a lot of interesting problems to solve here including what to do if there are multiple matches, or inconsistent data? The Sparql select command allows you to group, order and limit the results which would be increasingly useful with larger triple stores. That would definitely add a lot of code to the macro!
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2017 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2017/04/30/Emulating-Sparql-queries-in-emacs-lisp-with-pattern-matching.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 9.0.5&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Writing lisp code from Python</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2016/05/30/Writing-lisp-code-from-Python</link>
      <pubDate>Mon, 30 May 2016 09:26:05 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[python]]></category>
      <guid isPermaLink="false">8fLtRl77T5lq3mDjRbl_tQb10IE=</guid>
      <description>Writing lisp code from Python</description>
      <content:encoded><![CDATA[



&lt;p&gt;
Some time ago I &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2015/05/16/Python-data-structures-to-lisp/"&gt;wrote about converting python data structures to lisp&lt;/a&gt; . I have expanded on that idea to writing lisp programs from Python! The newly expanded code that makes this possible can be found at &lt;a href="https://github.com/jkitchin/pycse/blob/master/pycse/lisp.py"&gt;https://github.com/jkitchin/pycse/blob/master/pycse/lisp.py&lt;/a&gt; .
&lt;/p&gt;

&lt;p&gt;
Here are the simple data types known to pycse.lisp:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pycse.lisp
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(&lt;span style="color: #008000;"&gt;"a string"&lt;/span&gt;.lisp)
&lt;span style="color: #BA36A5;"&gt;a&lt;/span&gt; = 5
&lt;span style="color: #BA36A5;"&gt;b&lt;/span&gt; = 5.0
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(a.lisp)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(b.lisp)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;([1, 2, 3].lisp)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;((1, 2, 3).lisp)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;({&lt;span style="color: #008000;"&gt;'a'&lt;/span&gt;: 4}.lisp)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(np.array([1, 2, 3]).lisp)
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(np.array([1.0, 2.0, 3.0]).lisp)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
"a string"
5
5.0
(1 2 3)
(1 2 3)
(:a 4)
(1 2 3)
(1.0 2.0 3.0)
&lt;/pre&gt;

&lt;p&gt;
There are also some more complex types.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pycse.lisp &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; pl

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Symbol(&lt;span style="color: #008000;"&gt;'lambda'&lt;/span&gt;))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Quote(&lt;span style="color: #008000;"&gt;'lambda'&lt;/span&gt;))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.SharpQuote(&lt;span style="color: #008000;"&gt;'lambda'&lt;/span&gt;))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Cons(&lt;span style="color: #008000;"&gt;"a"&lt;/span&gt;, 5))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Alist([&lt;span style="color: #008000;"&gt;"a"&lt;/span&gt;, 2, &lt;span style="color: #008000;"&gt;"b"&lt;/span&gt;, 5]))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Vector([1, 2, 3]))

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Backquote([]))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Comma([1, 2, 3]))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Splice([1, 2, 3]))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
lambda
'lambda
#'lambda
("a" . 5)
(("a" . 2) ("b" . 5))
[1 2 3]
`()
,(1 2 3)
,@(1 2 3)
&lt;/pre&gt;

&lt;p&gt;
You can nest these too.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pycse.lisp &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; pl
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Quote(pl.Alist([&lt;span style="color: #008000;"&gt;"a"&lt;/span&gt;, 2, &lt;span style="color: #008000;"&gt;"b"&lt;/span&gt;, 5])))
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(pl.Backquote([pl.Symbol(&lt;span style="color: #008000;"&gt;'+'&lt;/span&gt;), pl.Comma(pl.Symbol(&lt;span style="color: #008000;"&gt;'b'&lt;/span&gt;)), 5]))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
'(("a" . 2) ("b" . 5))
`(+ ,b 5)
&lt;/pre&gt;

&lt;p&gt;
All that means we can use Python &lt;i&gt;code&lt;/i&gt; to generate lisp programs. Here is an example where we make two sub-programs, and combine them into an overall program, then add one more subprogram to it. We wrap the results in an emacs-lisp block, then actually run the block!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pycse.lisp &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; pl

&lt;span style="color: #BA36A5;"&gt;p1&lt;/span&gt; = [pl.Symbol(&lt;span style="color: #008000;"&gt;'mapcar'&lt;/span&gt;),
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt; [pl.Symbol(&lt;span style="color: #008000;"&gt;'lambda'&lt;/span&gt;),
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;  [pl.Symbol(&lt;span style="color: #008000;"&gt;'x'&lt;/span&gt;)],
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;  [pl.Symbol(&lt;span style="color: #008000;"&gt;'*'&lt;/span&gt;),
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   pl.Symbol(&lt;span style="color: #008000;"&gt;'x'&lt;/span&gt;),
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   pl.Symbol(&lt;span style="color: #008000;"&gt;'x'&lt;/span&gt;)]],
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt; pl.Quote([1, 2, 3, 4])]

&lt;span style="color: #BA36A5;"&gt;p2&lt;/span&gt; = [pl.Symbol(&lt;span style="color: #008000;"&gt;'princ'&lt;/span&gt;), &lt;span style="color: #008000;"&gt;"Hello world"&lt;/span&gt;]

&lt;span style="color: #BA36A5;"&gt;p&lt;/span&gt; = [pl.Symbol(&lt;span style="color: #008000;"&gt;'list'&lt;/span&gt;), p1, p2]
p.append([pl.Symbol(&lt;span style="color: #008000;"&gt;'+'&lt;/span&gt;), 5, 5])

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(p.lisp)
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(list (mapcar (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (x) (* x x)) '(1 2 3 4)) (princ &lt;span style="color: #008000;"&gt;"Hello world"&lt;/span&gt;) (+ 5 5))
&lt;/pre&gt;
&lt;/div&gt;

&lt;table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides"&gt;


&lt;colgroup&gt;
&lt;col  class="left" /&gt;

&lt;col  class="left" /&gt;

&lt;col  class="right" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;(1 4 9 16)&lt;/td&gt;
&lt;td class="left"&gt;Hello world&lt;/td&gt;
&lt;td class="right"&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Wow, it worked! Here is another example of setting up a macro and then running it.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; pycse.lisp &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; pl
&lt;span style="color: #BA36A5;"&gt;s&lt;/span&gt; = pl.Symbol
&lt;span style="color: #BA36A5;"&gt;bq&lt;/span&gt; = pl.Backquote
&lt;span style="color: #BA36A5;"&gt;c&lt;/span&gt; = pl.Comma

&lt;span style="color: #BA36A5;"&gt;p1&lt;/span&gt; = [s(&lt;span style="color: #008000;"&gt;'defmacro'&lt;/span&gt;), s(&lt;span style="color: #008000;"&gt;'f'&lt;/span&gt;), [s(&lt;span style="color: #008000;"&gt;'x'&lt;/span&gt;)],
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt; &lt;span style="color: #008000;"&gt;"A docstring"&lt;/span&gt;,
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt; bq([s(&lt;span style="color: #008000;"&gt;'*'&lt;/span&gt;), c(s(&lt;span style="color: #008000;"&gt;'x'&lt;/span&gt;)), 5])]


&lt;span style="color: #BA36A5;"&gt;p2&lt;/span&gt; = [s(&lt;span style="color: #008000;"&gt;'f'&lt;/span&gt;), 5]

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(p1.lisp)

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt;(p2.lisp)
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;f&lt;/span&gt; (x) &lt;span style="color: #036A07;"&gt;"A docstring"&lt;/span&gt; `(* ,x 5))
(&lt;span style="color: #0000FF;"&gt;f&lt;/span&gt; 5)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
25
&lt;/pre&gt;

&lt;p&gt;
I am not too sure where this will be super useful, but it is an interesting proof of concept. I haven't tested this much beyond the original post and this one. Let me know if you find issues with it.&lt;/p&gt;
&lt;p&gt;Copyright (C) 2016 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2016/05/30/Writing-lisp-code-from-Python.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>OMG A Lisp that runs python</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2016/03/30/OMG-A-Lisp-that-runs-python</link>
      <pubDate>Wed, 30 Mar 2016 17:10:17 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[python]]></category>
      <guid isPermaLink="false">dTe2pI6SbLTjMyWs8k1hagdJzL8=</guid>
      <description>OMG A Lisp that runs python</description>
      <content:encoded><![CDATA[



&lt;p&gt;
For a year now I have struggled with abandoning Python for Lisp. It's complicated, I have used Python for 15 years now, and have a lot of skill and knowledge in it. I have used emacs-lisp for about 5 years now, and have a far bit of skill with it too. They solve really different problems. Between the two, I find I &lt;i&gt;like&lt;/i&gt; writing and editing elisp &lt;b&gt;lots&lt;/b&gt; better than writing Python, except it lacks the scipy+numpy+matplotlib stack. I looked into Racket and Common Lisp, but they also don't really have that as nicely as Python does at the moment. It hit me earlier today that a Lisp that compiled to Python might be the right middle ground. I had seen this project Hy (&lt;a href="http://docs.hylang.org/en/latest/quickstart.html"&gt;http://docs.hylang.org/en/latest/quickstart.html&lt;/a&gt; ) earlier, but didn't connect the dots to this.
&lt;/p&gt;

&lt;p&gt;
Let me do that here. First, an obligatory execute function to run org-mode code blocks.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;org-babel-execute:hy&lt;/span&gt; (body params)
  (&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((temporary-file-directory &lt;span style="color: #008000;"&gt;"."&lt;/span&gt;)
         (tempfile (make-temp-file &lt;span style="color: #008000;"&gt;"hy-"&lt;/span&gt;)))
    (&lt;span style="color: #0000FF;"&gt;with-temp-file&lt;/span&gt; tempfile
      (insert body))
    (&lt;span style="color: #0000FF;"&gt;unwind-protect&lt;/span&gt;
        (shell-command-to-string
         (format &lt;span style="color: #008000;"&gt;"hy %s"&lt;/span&gt; tempfile))
      (delete-file tempfile))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
org-babel-execute:hy
&lt;/pre&gt;

&lt;p&gt;
Now the basic Hello world example. It looks like lisp.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-hy"&gt;(&lt;span style="color: #006FE0;"&gt;print&lt;/span&gt; &lt;span style="color: #008000;"&gt;"Hy world"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Hy world
&lt;/pre&gt;

&lt;p&gt;
Now for a use that looks like Python:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-hy"&gt;(&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; &lt;span style="color: #006699;"&gt;numpy&lt;/span&gt;)
(&lt;span style="color: #006FE0;"&gt;setv&lt;/span&gt; a (numpy.array [1 2 3]))
(&lt;span style="color: #006FE0;"&gt;setv&lt;/span&gt; b (numpy.array [1 2 3]))
(&lt;span style="color: #006FE0;"&gt;print&lt;/span&gt; (numpy.dot a b))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
14
&lt;/pre&gt;

&lt;p&gt;
WHAT!!!!
&lt;/p&gt;

&lt;p&gt;
A simple plot? Surely it can't be so easy&amp;#x2026;
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-hy"&gt;(import [matplotlib.pyplot &lt;span style="color: #D0372D;"&gt;:as&lt;/span&gt; plt])
(plt.plot [1 2 4 8])
(plt.xlabel &lt;span style="color: #008000;"&gt;"x"&lt;/span&gt;)
(plt.ylabel &lt;span style="color: #008000;"&gt;"y"&lt;/span&gt;)
(plt.savefig &lt;span style="color: #008000;"&gt;"hy-test.png"&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
2016-03-30 17:09:40.826 Python[94292:d13] CoreText performance note: Client called CTFontCreateWithName() using name "Lucida Grande" and got font with PostScript name "LucidaGrande". For best performance, only use PostScript names when calling this API.
2016-03-30 17:09:40.826 Python[94292:d13] CoreText performance note: Set a breakpoint on CTFontLogSuboptimalRequest to debug.
&lt;/pre&gt;


&lt;div class="figure"&gt;
&lt;p&gt;&lt;img src="/media/2016-03-30-OMG-A-Lisp-that-runs-python/hy-test.png"&gt; 
&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
Wow. I am not sure what the warnings are, I seem to get them on my Mac for some reason. How about solving an equation?
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-hy"&gt;(import [scipy.optimize [fsolve]])
(&lt;span style="color: #0000FF;"&gt;defn&lt;/span&gt; &lt;span style="color: #006699;"&gt;objective&lt;/span&gt; [x] (&lt;span style="color: #006FE0;"&gt;-&lt;/span&gt; 2 x))
(&lt;span style="color: #006FE0;"&gt;print&lt;/span&gt; (fsolve objective -1))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[ 2.]
&lt;/pre&gt;


&lt;pre class="example"&gt;
     _.-^^---....,,--
 _--                  --_
&amp;lt;                        &amp;gt;)
|                         |
 \._                   _./
    ```--. . , ; .--'''
          | |   |
       .-=||  | |=-.
       `-=#$%&amp;amp;%$#=-'
          | ;  :|
 _____.,-#%&amp;amp;$@%#&amp;amp;#~,._____
       _---~~(~~-_.
     _{        )   )
   ,   ) -~~- ( ,-' )_
  (  `-,_..`., )-- '_,)
 ( ` _)  (  -~( -_ `,  }
 (_-  _  ~_-~~~~`,  ,' )  &amp;lt;---- My brain right now...
   `~ -^(    __;-,((()))
         ~~~~ {_ -_(())
                `\  }
                  { }
&lt;/pre&gt;

&lt;p&gt;
I may not be able to sleep tonight&amp;#x2026;
&lt;/p&gt;

&lt;p&gt;
Ascii art courtesy of &lt;a href="http://chris.com/ascii/index.php?art=people/body%20parts/brains"&gt;http://chris.com/ascii/index.php?art=people/body%20parts/brains&lt;/a&gt; and &lt;a href="http://www.ascii-code.com/ascii-art/weapons/explosives.php"&gt;http://www.ascii-code.com/ascii-art/weapons/explosives.php&lt;/a&gt; .&lt;/p&gt;
&lt;p&gt;Copyright (C) 2016 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;
&lt;p&gt;&lt;a href="/org/2016/03/30/OMG-A-Lisp-that-runs-python.org"&gt;org-mode source&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>A sexp version of a bibtex entry</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2015/06/10/A-sexp-version-of-a-bibtex-entry</link>
      <pubDate>Wed, 10 Jun 2015 08:54:00 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[bibtex]]></category>
      <guid isPermaLink="false">BmILlGfMZSWZ6HJprHCTM4MR6zA=</guid>
      <description>A sexp version of a bibtex entry</description>
      <content:encoded><![CDATA[



&lt;p&gt;
Below you see a typical bibtex entry. Today we explore an alternate approach to represent the information (data) in that entry as s-expressions, i.e. as a lisp data structure. Why? because it seems like an interesting exploration!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-bibtex"&gt;&lt;span style="color: #006699;"&gt;@article&lt;/span&gt;{&lt;span style="color: #D0372D;"&gt;hallenbeck-2013-effec-o2&lt;/span&gt;,
  &lt;span style="color: #BA36A5;"&gt;author&lt;/span&gt; =       "Hallenbeck, Alexander P. and Kitchin, John R.",
  &lt;span style="color: #BA36A5;"&gt;title&lt;/span&gt; =        {Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a
                  primary-amine based polymeric \ce{CO_2} sorbent},
  &lt;span style="color: #BA36A5;"&gt;keywords&lt;/span&gt; =     {RUA, orgmode},
  &lt;span style="color: #BA36A5;"&gt;journal&lt;/span&gt; =      "Industrial \&amp;amp; Engineering Chemistry Research",
  &lt;span style="color: #BA36A5;"&gt;pages&lt;/span&gt; =        "10788-10794",
  &lt;span style="color: #BA36A5;"&gt;year&lt;/span&gt; =         2013,
  &lt;span style="color: #BA36A5;"&gt;volume&lt;/span&gt; =       {52},
  &lt;span style="color: #BA36A5;"&gt;number&lt;/span&gt; =       {31},
  &lt;span style="color: #BA36A5;"&gt;doi&lt;/span&gt; =          "&lt;span style="color: #006DAF; text-decoration: underline;"&gt;10.1021/ie400582a&lt;/span&gt;",
  &lt;span style="color: #BA36A5;"&gt;url&lt;/span&gt; =          "&lt;span style="color: #006DAF; text-decoration: underline;"&gt;http://pubs.acs.org/doi/abs/10.1021/ie400582a&lt;/span&gt;",
  &lt;span style="color: #BA36A5;"&gt;eprint&lt;/span&gt; =       "http://pubs.acs.org/doi/pdf/10.1021/ie400582a",
}
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Here is what that same data structure might look like as a sexp-based lisp data structure.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(article &lt;span style="color: #008000;"&gt;"hallenbeck-2013-effec-o2"&lt;/span&gt;
         (author &lt;span style="color: #008000;"&gt;"Hallenbeck, Alexander P. and Kitchin, John R."&lt;/span&gt;)
         (title &lt;span style="color: #008000;"&gt;"Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a primary-amine based polymeric \ce{CO_2} sorbent"&lt;/span&gt;)
         (journal &lt;span style="color: #008000;"&gt;"Industrial \&amp;amp; Engineering Chemistry Research"&lt;/span&gt;)
         (pages &lt;span style="color: #008000;"&gt;"10788-10794"&lt;/span&gt;)
         (year 2013)
         (number 31)
         (doi &lt;span style="color: #008000;"&gt;"10.1021/ie400582a"&lt;/span&gt;)
         (url &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/abs/10.1021/ie400582a"&lt;/span&gt;)
         (eprint &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/pdf/10.1021/ie400582a"&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
We can retrieve data from the sexp form pretty easily. Here we get the authors.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((art '(article &lt;span style="color: #008000;"&gt;"hallenbeck-2013-effec-o2"&lt;/span&gt;
                      (author &lt;span style="color: #008000;"&gt;"Hallenbeck, Alexander P. and Kitchin, John R."&lt;/span&gt;)
                      (title &lt;span style="color: #008000;"&gt;"Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a primary-amine based polymeric \ce{CO_2} sorbent"&lt;/span&gt;)
                      (journal &lt;span style="color: #008000;"&gt;"Industrial \&amp;amp; Engineering Chemistry Research"&lt;/span&gt;)
                      (pages &lt;span style="color: #008000;"&gt;"10788-10794"&lt;/span&gt;)
                      (year 2013)
                      (number 31)
                      (doi &lt;span style="color: #008000;"&gt;"10.1021/ie400582a"&lt;/span&gt;)
                      (url &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/abs/10.1021/ie400582a"&lt;/span&gt;)
                      (eprint &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/pdf/10.1021/ie400582a"&lt;/span&gt;)))
       (fields (cddr art)))
  (cadr (assoc 'author fields)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Hallenbeck, Alexander P. and Kitchin, John R.
&lt;/pre&gt;

&lt;p&gt;
That is simple enough you might just write a little function to streamline it like this, and return a formatted string.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;get-article-field&lt;/span&gt; (article field)
  &lt;span style="color: #036A07;"&gt;"Return value of FIELD in ARTICLE."&lt;/span&gt;
  (cadr (assoc field (cddr article))))

(&lt;span style="color: #0000FF;"&gt;let&lt;/span&gt; ((art '(article &lt;span style="color: #008000;"&gt;"hallenbeck-2013-effec-o2"&lt;/span&gt;
                     (author &lt;span style="color: #008000;"&gt;"Hallenbeck, Alexander P. and Kitchin, John R."&lt;/span&gt;)
                     (title &lt;span style="color: #008000;"&gt;"Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a primary-amine based polymeric \ce{CO_2} sorbent"&lt;/span&gt;)
                     (journal &lt;span style="color: #008000;"&gt;"Industrial \&amp;amp; Engineering Chemistry Research"&lt;/span&gt;)
                     (pages &lt;span style="color: #008000;"&gt;"10788-10794"&lt;/span&gt;)
                     (year 2013)
                     (number 31)
                     (doi &lt;span style="color: #008000;"&gt;"10.1021/ie400582a"&lt;/span&gt;)
                     (url &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/abs/10.1021/ie400582a"&lt;/span&gt;)
                     (eprint &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/pdf/10.1021/ie400582a"&lt;/span&gt;))))
  (format &lt;span style="color: #008000;"&gt;"%s, doi:%s (%s)"&lt;/span&gt;
          (get-article-field art 'author)
          (get-article-field art 'doi)
          (get-article-field art 'year)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Hallenbeck, Alexander P. and Kitchin, John R., doi:10.1021/ie400582a (2013)
&lt;/pre&gt;

&lt;p&gt;
You might be wondering, why is that even a little bit interesting? One reason is that it looks a little like what lisp returns after parsing an xml file. Another is, the data structure looks kind of like data, but it is also some code, if article was defined as a function!  Let us consider what this might look like. I use a macro to define the field functions since in this case they all do the same thing, and these simply return a string with the field-name and value in curly brackets. We eval the macro to make sure it defines the function. I define an article function that wraps the fields in @bibtex-key{fields}, which defines a bibtex entry.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #006699;"&gt;make-field&lt;/span&gt; (field-name)
  &lt;span style="color: #036A07;"&gt;"define a field that returns a string"&lt;/span&gt;
  `(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; ,(intern field-name) (content)
     (format &lt;span style="color: #008000;"&gt;"  %s = {%s}"&lt;/span&gt; ,field-name content)))

(&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for field in '(&lt;span style="color: #008000;"&gt;"author"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"title"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"journal"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"pages"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"number"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"doi"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"url"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"eprint"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"year"&lt;/span&gt;)
  do (eval `(make-field ,field)))

(&lt;span style="color: #0000FF;"&gt;defun&lt;/span&gt; &lt;span style="color: #006699;"&gt;article&lt;/span&gt; (bibtex-key &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; fields)
  (concat
   (format &lt;span style="color: #008000;"&gt;"@article{%s,\n"&lt;/span&gt; bibtex-key)
   (mapconcat (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (field) (eval field)) fields &lt;span style="color: #008000;"&gt;",\n"&lt;/span&gt;)
   &lt;span style="color: #008000;"&gt;"\n}\n"&lt;/span&gt;))

(article &lt;span style="color: #008000;"&gt;"hallenbeck-2013-effec-o2"&lt;/span&gt;
         (author &lt;span style="color: #008000;"&gt;"Hallenbeck, Alexander P. and Kitchin, John R."&lt;/span&gt;)
         (title &lt;span style="color: #008000;"&gt;"Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a primary-amine based polymeric \ce{CO_2} sorbent"&lt;/span&gt;)
         (journal &lt;span style="color: #008000;"&gt;"Industrial \&amp;amp; Engineering Chemistry Research"&lt;/span&gt;)
         (pages &lt;span style="color: #008000;"&gt;"10788-10794"&lt;/span&gt;)
         (number 31)
         (year 2013)
         (doi &lt;span style="color: #008000;"&gt;"10.1021/ie400582a"&lt;/span&gt;)
         (url &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/abs/10.1021/ie400582a"&lt;/span&gt;)
         (eprint &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/pdf/10.1021/ie400582a"&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
@article{hallenbeck-2013-effec-o2,
  author = {Hallenbeck, Alexander P. and Kitchin, John R.},
  title = {Effects of ce{O_2} and ce{SO_2} on the capture capacity of a primary-amine based polymeric ce{CO_2} sorbent},
  journal = {Industrial &amp;amp; Engineering Chemistry Research},
  pages = {10788-10794},
  number = {31},
  year = {2013},
  doi = {10.1021/ie400582a},
  url = {http://pubs.acs.org/doi/abs/10.1021/ie400582a},
  eprint = {http://pubs.acs.org/doi/pdf/10.1021/ie400582a}
}
&lt;/pre&gt;

&lt;p&gt;
Wow. We &lt;i&gt;executed&lt;/i&gt; our data structure, and got a bibtex entry! That seems moderately interesting to me. Next is an example of taking the same data structure and rendering it as xml. This is some lispy wizardry, rather than use a macro to define functions, I temporarily define functions within a cl-flet macro, which I have to collect as a list of code. Then, I eval the list. This feels pretty odd, but seems like a lispy kind of thing to do.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(eval
 (list 'cl-flet
       (append (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for field in '(&lt;span style="color: #008000;"&gt;"author"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"title"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"journal"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"pages"&lt;/span&gt;
                                      &lt;span style="color: #008000;"&gt;"number"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"doi"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"url"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"eprint"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"year"&lt;/span&gt;)
                       collect (list (intern field)
                                     '(content)
                                     `(format &lt;span style="color: #008000;"&gt;"  &amp;lt;%s&amp;gt;%s&amp;lt;/%s&amp;gt;"&lt;/span&gt; ,field content ,field)))
               '((article (bibtex-key &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; fields)
                          (concat
                           (format
                            &lt;span style="color: #008000;"&gt;"&amp;lt;article bibtex-key=\"%s\"&amp;gt;\n"&lt;/span&gt; bibtex-key)
                           (mapconcat (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (field) (eval field)) fields &lt;span style="color: #008000;"&gt;"\n"&lt;/span&gt;)
                           &lt;span style="color: #008000;"&gt;"\n&amp;lt;/article&amp;gt;"&lt;/span&gt;)))
               )
       &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;body of cl-flet&lt;/span&gt;
       '(article &lt;span style="color: #008000;"&gt;"hallenbeck-2013-effec-o2"&lt;/span&gt;
                (author &lt;span style="color: #008000;"&gt;"Hallenbeck, Alexander P. and Kitchin, John R."&lt;/span&gt;)
                (title &lt;span style="color: #008000;"&gt;"Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a primary-amine based polymeric \ce{CO_2} sorbent"&lt;/span&gt;)
                (journal &lt;span style="color: #008000;"&gt;"Industrial \&amp;amp; Engineering Chemistry Research"&lt;/span&gt;)
                (pages &lt;span style="color: #008000;"&gt;"10788-10794"&lt;/span&gt;)
                (number 31)
                (year 2013)
                (doi &lt;span style="color: #008000;"&gt;"10.1021/ie400582a"&lt;/span&gt;)
                (url &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/abs/10.1021/ie400582a"&lt;/span&gt;)
                (eprint &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/pdf/10.1021/ie400582a"&lt;/span&gt;))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
&amp;lt;article bibtex-key="hallenbeck-2013-effec-o2"&amp;gt;
  &amp;lt;author&amp;gt;Hallenbeck, Alexander P. and Kitchin, John R.&amp;lt;/author&amp;gt;
  &amp;lt;title&amp;gt;Effects of ce{O_2} and ce{SO_2} on the capture capacity of a primary-amine based polymeric ce{CO_2} sorbent&amp;lt;/title&amp;gt;
  &amp;lt;journal&amp;gt;Industrial &amp;amp; Engineering Chemistry Research&amp;lt;/journal&amp;gt;
  &amp;lt;pages&amp;gt;10788-10794&amp;lt;/pages&amp;gt;
  &amp;lt;number&amp;gt;31&amp;lt;/number&amp;gt;
  &amp;lt;year&amp;gt;2013&amp;lt;/year&amp;gt;
  &amp;lt;doi&amp;gt;10.1021/ie400582a&amp;lt;/doi&amp;gt;
  &amp;lt;url&amp;gt;http://pubs.acs.org/doi/abs/10.1021/ie400582a&amp;lt;/url&amp;gt;
  &amp;lt;eprint&amp;gt;http://pubs.acs.org/doi/pdf/10.1021/ie400582a&amp;lt;/eprint&amp;gt;
&amp;lt;/article&amp;gt;
&lt;/pre&gt;

&lt;p&gt;
Prefer json? No problem, just reformat the functions!
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(eval
 (list 'cl-flet
       (append (&lt;span style="color: #0000FF;"&gt;loop&lt;/span&gt; for field in '(&lt;span style="color: #008000;"&gt;"author"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"title"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"journal"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"pages"&lt;/span&gt;
                                      &lt;span style="color: #008000;"&gt;"number"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"doi"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"url"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"eprint"&lt;/span&gt; &lt;span style="color: #008000;"&gt;"year"&lt;/span&gt;)
                       collect (list (intern field)
                                     '(content)
                                     `(format &lt;span style="color: #008000;"&gt;"   \"%s\": \"%s\""&lt;/span&gt; ,field content)))
               '((article (bibtex-key &lt;span style="color: #6434A3;"&gt;&amp;amp;rest&lt;/span&gt; fields)
                          (concat
                           (format
                            &lt;span style="color: #008000;"&gt;"{\"article\":\n  {\"bibtex-key\": \"%s\",\n"&lt;/span&gt; bibtex-key)
                           (mapconcat (&lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; (field) (eval field)) fields &lt;span style="color: #008000;"&gt;",\n"&lt;/span&gt;)
                           &lt;span style="color: #008000;"&gt;"}\n}"&lt;/span&gt;))))
       &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;body of cl-flet&lt;/span&gt;
       '(article &lt;span style="color: #008000;"&gt;"hallenbeck-2013-effec-o2"&lt;/span&gt;
                (author &lt;span style="color: #008000;"&gt;"Hallenbeck, Alexander P. and Kitchin, John R."&lt;/span&gt;)
                (title &lt;span style="color: #008000;"&gt;"Effects of \ce{O_2} and \ce{SO_2} on the capture capacity of a primary-amine based polymeric \ce{CO_2} sorbent"&lt;/span&gt;)
                (journal &lt;span style="color: #008000;"&gt;"Industrial \&amp;amp; Engineering Chemistry Research"&lt;/span&gt;)
                (pages &lt;span style="color: #008000;"&gt;"10788-10794"&lt;/span&gt;)
                (number 31)
                (year 2013)
                (doi &lt;span style="color: #008000;"&gt;"10.1021/ie400582a"&lt;/span&gt;)
                (url &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/abs/10.1021/ie400582a"&lt;/span&gt;)
                (eprint &lt;span style="color: #008000;"&gt;"http://pubs.acs.org/doi/pdf/10.1021/ie400582a"&lt;/span&gt;))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
{"article":
  {"bibtex-key": "hallenbeck-2013-effec-o2",
   "author": "Hallenbeck, Alexander P. and Kitchin, John R.",
   "title": "Effects of ce{O_2} and ce{SO_2} on the capture capacity of a primary-amine based polymeric ce{CO_2} sorbent",
   "journal": "Industrial &amp;amp; Engineering Chemistry Research",
   "pages": "10788-10794",
   "number": "31",
   "year": "2013",
   "doi": "10.1021/ie400582a",
   "url": "http://pubs.acs.org/doi/abs/10.1021/ie400582a",
   "eprint": "http://pubs.acs.org/doi/pdf/10.1021/ie400582a"}
}
&lt;/pre&gt;

&lt;p&gt;
Is this useful? Great question. I don't plan to convert by bibtex files to sexp format anytime soon ;) The format I used above is just a simple one. It might be desirable to include individual authors instead of an author string, and maybe support attributes to establish an author order. An author structure might be more complex to include scientific ids like an orcid, alternative names, etc&amp;#x2026; Finally, the s-exp data structure is super easy to use in lisp, but other languages would have parse it into some native structure the way they parse json or xml. There is limited support for s-expressions in most other non-lispy languages.
&lt;/p&gt;

&lt;p&gt;
I like the idea of data representation as code, and its conversion to some other kind of format. It is subtle here, but notice we &lt;i&gt;never&lt;/i&gt; had to write a parser for the sexp notation. That &lt;i&gt;already exists as the lisp interpreter&lt;/i&gt;. We did write code to use the data, and convert the data. The sexp notation is pretty easy to write, in contrast to the xml or json representations. Some interesting issues might be what to do with fields that are not defined, perhaps a macro would be used on the fly, or in the cl-flet definition. It is hard to imagine doing these things in another language than lisp!
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2015 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2015/06/10/A-sexp-version-of-a-bibtex-entry.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Python data structures to lisp</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2015/05/16/Python-data-structures-to-lisp</link>
      <pubDate>Sat, 16 May 2015 10:47:59 EDT</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[python]]></category>
      <guid isPermaLink="false">W0S7IVT_nQDetPEbwbEN0vag2-U=</guid>
      <description>Python data structures to lisp</description>
      <content:encoded><![CDATA[


&lt;p&gt;
I have an idea in mind that would use the output of python scripts in lisp functions. Xah Lee posted an &lt;a href="http://ergoemacs.org/emacs/elisp_perl_wrapper.html"&gt;idea for writing emacs commands in scripting languages&lt;/a&gt; . In this post I want to explore an extension of the idea, where a Python script will return output that can be read in Lisp, e.g. we can convert a Python list to a lisp list, or a dictionary to an a-list or p-list. I can already see that simple data structures will be "simple", and arbitrary data structures will offer a lot of challenges, e.g. nested lists or dictionaries&amp;#x2026;
&lt;/p&gt;

&lt;p&gt;
If I could add some custom functions to the basic builtin types in Python, then I could use another approach to format python objects as lisp data types. This isn't recommended by Pythonistas, but I guess they don't want to use lisp as much as I do ;) I found this approach to modifying builtins:
&lt;/p&gt;

&lt;p&gt;
&lt;a href="http://stackoverflow.com/questions/2444680/how-do-i-add-my-own-custom-attributes-to-existing-built-in-python-types-like-a"&gt;http://stackoverflow.com/questions/2444680/how-do-i-add-my-own-custom-attributes-to-existing-built-in-python-types-like-a&lt;/a&gt; 
&lt;/p&gt;

&lt;p&gt;
We use that almost verbatim here to get what I want. This is a super low level way to add functions to the builtins. I add some simple formatting to floats, ints and strings. I add a more complex recursive formatting function to lists, tuples and dictionaries. A dictionary can be represented as an alist or plist. Both examples are shown, but I leave the alist version commented out. Finally, we add a lispify function to numpy arrays.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; ctypes &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; c

&lt;span style="color: #0000FF;"&gt;class&lt;/span&gt; &lt;span style="color: #6434A3;"&gt;PyObject_HEAD&lt;/span&gt;(c.Structure):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;_fields_&lt;/span&gt; = [(&lt;span style="color: #008000;"&gt;'HEAD'&lt;/span&gt;, c.c_ubyte * (&lt;span style="color: #006FE0;"&gt;object&lt;/span&gt;.__basicsize__ -
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;     c.sizeof(c.c_void_p))),
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   (&lt;span style="color: #008000;"&gt;'ob_type'&lt;/span&gt;, c.c_void_p)]

&lt;span style="color: #BA36A5;"&gt;_get_dict&lt;/span&gt; = c.pythonapi._PyObject_GetDictPtr
&lt;span style="color: #BA36A5;"&gt;_get_dict.restype&lt;/span&gt; = c.POINTER(c.py_object)
&lt;span style="color: #BA36A5;"&gt;_get_dict.argtypes&lt;/span&gt; = [c.py_object]

&lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;get_dict&lt;/span&gt;(&lt;span style="color: #006FE0;"&gt;object&lt;/span&gt;):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; _get_dict(&lt;span style="color: #006FE0;"&gt;object&lt;/span&gt;).contents.value

get_dict(&lt;span style="color: #006FE0;"&gt;str&lt;/span&gt;)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = &lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; s:&lt;span style="color: #008000;"&gt;'"{}"'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(&lt;span style="color: #006FE0;"&gt;str&lt;/span&gt;(s))
get_dict(&lt;span style="color: #006FE0;"&gt;float&lt;/span&gt;)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = &lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; f:&lt;span style="color: #008000;"&gt;'{}'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(&lt;span style="color: #006FE0;"&gt;str&lt;/span&gt;(f))
get_dict(&lt;span style="color: #006FE0;"&gt;int&lt;/span&gt;)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = &lt;span style="color: #0000FF;"&gt;lambda&lt;/span&gt; f:&lt;span style="color: #008000;"&gt;'{}'&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(&lt;span style="color: #006FE0;"&gt;str&lt;/span&gt;(f))

&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; collections
&lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; numpy &lt;span style="color: #0000FF;"&gt;as&lt;/span&gt; np

&lt;span style="color: #0000FF;"&gt;def&lt;/span&gt; &lt;span style="color: #006699;"&gt;lispify&lt;/span&gt;(L):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #008000;"&gt;"Convert a Python object L to a lisp representation."&lt;/span&gt;
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;if&lt;/span&gt; (&lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, &lt;span style="color: #006FE0;"&gt;str&lt;/span&gt;)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, &lt;span style="color: #006FE0;"&gt;float&lt;/span&gt;)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, &lt;span style="color: #006FE0;"&gt;int&lt;/span&gt;)):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; L.lisp()
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;elif&lt;/span&gt; (&lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, &lt;span style="color: #006FE0;"&gt;list&lt;/span&gt;)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;     &lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, &lt;span style="color: #006FE0;"&gt;tuple&lt;/span&gt;)
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;     &lt;span style="color: #0000FF;"&gt;or&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, np.ndarray)):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;s&lt;/span&gt; = []
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;for&lt;/span&gt; element &lt;span style="color: #0000FF;"&gt;in&lt;/span&gt; L:
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;s&lt;/span&gt; += [element.lisp()]
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; &lt;span style="color: #008000;"&gt;'('&lt;/span&gt; + &lt;span style="color: #008000;"&gt;' '&lt;/span&gt;.join(s) + &lt;span style="color: #008000;"&gt;')'&lt;/span&gt;
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;elif&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;isinstance&lt;/span&gt;(L, &lt;span style="color: #006FE0;"&gt;dict&lt;/span&gt;):
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;s&lt;/span&gt; = []
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;for&lt;/span&gt; key &lt;span style="color: #0000FF;"&gt;in&lt;/span&gt; L:
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;alist format&lt;/span&gt;
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;s += ["({0} . {1})".format(key, L[key].lisp())]&lt;/span&gt;
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #8D8D84;"&gt;# &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;plist&lt;/span&gt;
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #BA36A5;"&gt;s&lt;/span&gt; += [&lt;span style="color: #008000;"&gt;":{0} {1}"&lt;/span&gt;.&lt;span style="color: #006FE0;"&gt;format&lt;/span&gt;(key, L[key].lisp())]
&lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #9B9B9B; background-color: #EDEDED;"&gt; &lt;/span&gt;   &lt;span style="color: #0000FF;"&gt;return&lt;/span&gt; &lt;span style="color: #008000;"&gt;'('&lt;/span&gt; + &lt;span style="color: #008000;"&gt;' '&lt;/span&gt;.join(s) + &lt;span style="color: #008000;"&gt;')'&lt;/span&gt;

get_dict(&lt;span style="color: #006FE0;"&gt;list&lt;/span&gt;)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = lispify
get_dict(&lt;span style="color: #006FE0;"&gt;tuple&lt;/span&gt;)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = lispify
get_dict(&lt;span style="color: #006FE0;"&gt;dict&lt;/span&gt;)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = lispify
get_dict(np.ndarray)[&lt;span style="color: #008000;"&gt;'lisp'&lt;/span&gt;] = lispify
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
Let us test these out.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #0000FF;"&gt;from&lt;/span&gt; pylisp &lt;span style="color: #0000FF;"&gt;import&lt;/span&gt; *
&lt;span style="color: #BA36A5;"&gt;a&lt;/span&gt; = 4.5
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; &lt;span style="color: #006FE0;"&gt;int&lt;/span&gt;(a).lisp()
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; a.lisp()
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; &lt;span style="color: #008000;"&gt;"test"&lt;/span&gt;.lisp()

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; [1, 2, 3].lisp()
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; (1, 2, 3).lisp()

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; [[1, 3], (5, 6)].lisp()

&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; {&lt;span style="color: #008000;"&gt;"a"&lt;/span&gt;: 5}.lisp()
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; [[1, 3], (5, 6), {&lt;span style="color: #008000;"&gt;"a"&lt;/span&gt;: 5, &lt;span style="color: #008000;"&gt;"b"&lt;/span&gt;: &lt;span style="color: #008000;"&gt;"test"&lt;/span&gt;}].lisp()


&lt;span style="color: #BA36A5;"&gt;A&lt;/span&gt; = np.array([1, 3, 4])
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; A.lisp()
&lt;span style="color: #0000FF;"&gt;print&lt;/span&gt; ({&lt;span style="color: #008000;"&gt;"tree"&lt;/span&gt;: [5, 6]}, [&lt;span style="color: #008000;"&gt;"a"&lt;/span&gt;, 4, &lt;span style="color: #008000;"&gt;"list"&lt;/span&gt;], 5, 2.0 / 3.0).lisp()
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
4
4.5
"test"
(1 2 3)
(1 2 3)
((1 3) (5 6))
(:a 5)
((1 3) (5 6) (:a 5 :b "test"))
(1 3 4)
((:tree (5 6)) ("a" 4 "list") 5 0.666666666667)
&lt;/pre&gt;


&lt;p&gt;
Now, is that better than a single lisp function with a lot of conditionals to handle each type? I am not sure. This seems to work pretty well.
&lt;/p&gt;


&lt;p&gt;
Here is how I imagine using this idea. We would have some emacs-lisp variables and use them to dynamically generate a python script. We run the python script, capturing the output, and read it back in as a lisp data structure. Here is a simple kind of example that generates a dictionary.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((elisp-var 6)
       (result)
      (script (format &lt;span style="color: #008000;"&gt;"&lt;/span&gt;
&lt;span style="color: #008000;"&gt;from pylisp import *&lt;/span&gt;
&lt;span style="color: #008000;"&gt;print {x: [2*y for y in range(x)] for x in range(1, %s)}.lisp()&lt;/span&gt;
&lt;span style="color: #008000;"&gt;"&lt;/span&gt; elisp-var)))

  &lt;span style="color: #8D8D84;"&gt;;; &lt;/span&gt;&lt;span style="color: #8D8D84; font-style: italic;"&gt;start a python process&lt;/span&gt;
  (run-python)
  (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; result (read (python-shell-send-string-no-output
   script)))
  (plist-get result &lt;span style="color: #006FE0;"&gt;:5&lt;/span&gt;))
&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(0 2 4 6 8)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
That seems to work pretty well. One alternative idea to this is &lt;a href="https://github.com/pinard/Pymacs"&gt;Pymacs&lt;/a&gt; , which I have written about &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2014/10/19/Using-Pymacs-to-integrate-Python-into-Emacs/"&gt;before&lt;/a&gt; . This project isn't currently under active development, and I ran into some difficulties with it before.
&lt;/p&gt;

&lt;p&gt;
Here we can solve the problem I previously posed and get the result back as an elisp float, and then reuse the result
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #0000FF;"&gt;let*&lt;/span&gt; ((myvar 3)
       (script (format &lt;span style="color: #008000;"&gt;"from pylisp import *&lt;/span&gt;
&lt;span style="color: #008000;"&gt;from scipy.optimize import fsolve&lt;/span&gt;
&lt;span style="color: #008000;"&gt;def objective(x):&lt;/span&gt;
&lt;span style="color: #008000;"&gt;    return x - 5&lt;/span&gt;

&lt;span style="color: #008000;"&gt;ans, = fsolve(objective, %s)&lt;/span&gt;
&lt;span style="color: #008000;"&gt;print ans.lisp()"&lt;/span&gt; myvar)))
  (run-python)
  (&lt;span style="color: #0000FF;"&gt;setq&lt;/span&gt; result (read (python-shell-send-string-no-output
                       script)))
  (- 5 result))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
0.0
&lt;/pre&gt;

&lt;p&gt;
Bottom line: we can write python code in lisp functions that are dynamically updated, execute them, and get lisp data structures back for simple data types. I think that could be useful in some applications, where it is easier to do parsing/analysis in Python, but you want to do something else that is easier in Lisp.
&lt;/p&gt;
&lt;p&gt;Copyright (C) 2015 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2015/05/16/Python-data-structures-to-lisp.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>A prototype implementation of jasp in emacs-lisp</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2014/12/24/A-prototype-implementation-of-jasp-in-emacs-lisp</link>
      <pubDate>Wed, 24 Dec 2014 11:41:00 EST</pubDate>
      <category><![CDATA[lisp]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[ase]]></category>
      <category><![CDATA[vasp]]></category>
      <guid isPermaLink="false">GB14oL8NufR3GRSfetF6V-_v3KU=</guid>
      <description>A prototype implementation of jasp in emacs-lisp</description>
      <content:encoded><![CDATA[


&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-1"&gt;1. The Atom class&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-2"&gt;2. The Atoms class&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-3"&gt;3. The Calculator class&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-4"&gt;4. Putting it all together to run calculations&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#sec-5"&gt;5. Summary thoughts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;
I want to try implementing an interface to &lt;a href="http://www.vasp.at"&gt;VASP&lt;/a&gt; in emacs-lisp. VASP is a program that uses density functional theory to calculate properties of materials. VASP was designed for the user to create several text-based input files that contain an atomic geometry, and calculation parameters. Then you run the VASP program, which reads those input files and generates output files. Finally, you parse out the results you want from the output files. 
&lt;/p&gt;

&lt;p&gt;
It is moderately tedious to do that, so we already have a very extensive Python interface (&lt;a href="http://github.com/jkitchin/jasp"&gt;http://github.com/jkitchin/jasp&lt;/a&gt; ) that automates the input file creation and output file parsing, and with org-mode we have a pretty good literate research environment to document what we do. But, the Python/emacs environment is not as integrated as the emacs-lisp/emacs environment is, particularly when it comes to accessing documentation. So, I want to try out a lisp approach to see what this would look like, and if it has any benefits.
&lt;/p&gt;

&lt;p&gt;
The bare bones implementation will have an Atom object, to store the type of atom and its coordinates, an Atoms object which will be a collection of atoms, and a unit cell, and a Calculator object that will store calculation parameters.
&lt;/p&gt;

&lt;p&gt;
Then, we will try using it to see what advantages it might have. This will be a moderately long post.
&lt;/p&gt;

&lt;div id="outline-container-sec-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; The Atom class&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
This is my first serious effort to use the &lt;a href="https://www.gnu.org/software/emacs/manual/html_node/eieio/"&gt;object system&lt;/a&gt; in emacs-lisp, so I do not claim it is optimal, or beautiful. It is functional though. We make a simple Atom class that holds the chemical symbol, and xyz coordinates. We do not consider having a magnetic moment at this point, and this class has no methods.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;defclass&lt;/span&gt; &lt;span style="color: #4682b4;"&gt;Atom&lt;/span&gt; ()
  ((symbol &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt;
           &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"A string for the chemical symbol"&lt;/span&gt;)
   (x &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt;
      &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"The x coordinate"&lt;/span&gt;)
   (y &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt;
      &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"The y coordinate"&lt;/span&gt;)
   (z &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt;
      &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"The z coordinate"&lt;/span&gt;))
 &lt;span style="color: #228b22;"&gt;"A class to represent an atom."&lt;/span&gt;)

(&lt;span style="color: #8b0000;"&gt;provide&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;Atom&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Atom
&lt;/pre&gt;

&lt;p&gt;
Let us try it out. We make an Atom, then get the symbol using the oref function. I don't think we need a "Name" for the atom, so the second argument is nil.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[object Atom nil "C" 0 0 0]
&lt;/pre&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((a1 (Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0  &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)))
  (oref a1 symbol))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
C
&lt;/pre&gt;

&lt;p&gt;
It is not difficult to modify an Atom object. We use oset.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((a1 (Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0  &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)))
  (oset a1 x 2.5)
  a1)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[object Atom nil "C" 2.5 0 0]
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-sec-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;&lt;span class="section-number-2"&gt;2&lt;/span&gt; The Atoms class&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;
&lt;p&gt;
We need at Atoms object, which will store a list of Atom objects, and a unit cell. I do not know how to make this class act like a list the way that the Atoms object in ase acts. We will have to access the list of atoms as a slot. We define one method here to get the ith atom from the object.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;defclass&lt;/span&gt; &lt;span style="color: #4682b4;"&gt;Atoms&lt;/span&gt; ()
  ((atoms &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt;
          &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"A list of `&lt;/span&gt;&lt;span style="color: #cd0000;"&gt;Atom&lt;/span&gt;&lt;span style="color: #228b22;"&gt;' objects"&lt;/span&gt;)
   (cell &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt;
         &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"A 3-tuple of lengths of an orthorhombic unit cell"&lt;/span&gt;))
  &lt;span style="color: #228b22;"&gt;"A class to represent an atoms object."&lt;/span&gt;)


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;get-atom&lt;/span&gt; ((atoms Atoms) &lt;span style="color: #4682b4;"&gt;&amp;amp;rest&lt;/span&gt; args)
  &lt;span style="color: #228b22;"&gt;"Get a list of atoms in ARGS.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;Return atom if ARGS contains one element, a list of atom objects&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;otherwise."&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;cond&lt;/span&gt;
   ((= 1 (length args))
    (elt (oref atoms atoms) (car args)))
   (t
    (mapcar (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (i)
              (elt (oref atoms atoms) i))
            args))))

(&lt;span style="color: #8b0000;"&gt;provide&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;Atoms&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
get-atom
&lt;/pre&gt;

&lt;p&gt;
That seems to do what we need. It has some limitations:
&lt;/p&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;We only allowed for orthorhombic unit cells
&lt;/li&gt;
&lt;li&gt;We did not enable any constraints or magnetic moments on the atoms.
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Here is an example Atoms object. Note we use `, notation to ensure each Atom is created, since Atom is a constructor function that returns the object.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(Atoms nil
       &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; `(,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
                ,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 1.1 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0))
       &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt; '(8 9 10))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[object Atoms nil ([object Atom nil "C" 0 0 0] [object Atom nil "O" 1.1 0 0]) (8 9 10)]
&lt;/pre&gt;

&lt;p&gt;
We can drill into the object, e.g. to get the second atom:
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((A1 (Atoms nil
                 &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; `(,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
                          ,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 1.1 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0))
                 &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt; '(8 9 10))))
  (get-atom A1 1))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[object Atom nil "O" 1.1 0 0]
&lt;/pre&gt;

&lt;p&gt;
We can modify the atoms in place like this. Suppose we want to change the symbol of the first atom to "O". We use setf for this too.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((A1 (Atoms nil
                 &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; `(,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
                          ,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 1.1 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0))
                 &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt; '(8 9 10))))
  (oset (get-atom A1 0) symbol &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt;)
  A1)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[object Atoms nil ([object Atom nil "O" 0 0 0] [object Atom nil "O" 1.1 0 0]) (8 9 10)]
&lt;/pre&gt;

&lt;p&gt;
The only think I do not like about this syntax is the need to get the list of atoms from the object. That is a little clunkier than the Python analog where the object is a list itself. That may be just my inexperience with emacs-lisp. Probably you can define some getter function to smooth this over.
&lt;/p&gt;

&lt;p&gt;
This Atoms class lacks much of the functionality of the &lt;a href="https://wiki.fysik.dtu.dk/ase/ase/atoms.html"&gt;ase.Atoms&lt;/a&gt; class, but it is sufficient for this prototype.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-sec-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;&lt;span class="section-number-2"&gt;3&lt;/span&gt; The Calculator class&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;
&lt;p&gt;
Next, we need our Calculator. This will store the parameters, and be responsible for creating the INCAR, POSCAR, KPOINTS, and POTCAR files, running a calculation, and getting data from the output. We also create a with-current-directory macro that will temporarily change the working directory since VASP uses the same filenames over and over, but in different directories. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;defmacro&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;with-current-directory&lt;/span&gt; (directory &lt;span style="color: #4682b4;"&gt;&amp;amp;rest&lt;/span&gt; body)
  &lt;span style="color: #228b22;"&gt;"Set the working directory temporarily set to DIRECTORY and run BODY.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;DIRECTORY is expanded, and create it and its parents if needed."&lt;/span&gt;
  `(&lt;span style="color: #8b0000;"&gt;progn&lt;/span&gt;
     (&lt;span style="color: #8b0000;"&gt;unless&lt;/span&gt; (file-exists-p (file-name-as-directory
                             (expand-file-name ,directory)))
       (make-directory ,directory t))
     
     (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((default-directory (file-name-as-directory
                                (expand-file-name ,directory)))) 
        ,@body)))


(&lt;span style="color: #8b0000;"&gt;defclass&lt;/span&gt; &lt;span style="color: #4682b4;"&gt;Jasp&lt;/span&gt; ()
  ((wd &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:wd&lt;/span&gt;
       &lt;span style="color: #cd0000;"&gt;:initform&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"."&lt;/span&gt;  ; &lt;span style="color: #ff0000; font-weight: bold;"&gt;default to the current working directory&lt;/span&gt;
       &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"Directory to run calculation in."&lt;/span&gt;)
   (encut &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:encut&lt;/span&gt;
          &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"Positive number in eV for planewave cutoff.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;See URL `&lt;/span&gt;&lt;span style="color: #cd0000;"&gt;http://cms.mpi.univie.ac.at/vasp/vasp/ENCUT_tag.html&lt;/span&gt;&lt;span style="color: #228b22;"&gt;'."&lt;/span&gt;)
   (nbands &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:nbands&lt;/span&gt;
           &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"Integer number of bands.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;See URL `&lt;/span&gt;&lt;span style="color: #cd0000;"&gt;http://cms.mpi.univie.ac.at/vasp/vasp/NBANDS_tag.html&lt;/span&gt;&lt;span style="color: #228b22;"&gt;'."&lt;/span&gt;)
   (kpts &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:kpts&lt;/span&gt;
         &lt;span style="color: #cd0000;"&gt;:initform&lt;/span&gt; (1 1 1)  ; &lt;span style="color: #ff0000; font-weight: bold;"&gt;default value&lt;/span&gt;
         &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"3-tuple for Monkhorst-Pack K-point grid."&lt;/span&gt;)
   (xc &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:xc&lt;/span&gt;
       &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"String of exchange correlation functional."&lt;/span&gt;)
   (atoms &lt;span style="color: #cd0000;"&gt;:initarg&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt;
          &lt;span style="color: #cd0000;"&gt;:documentation&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"An `&lt;/span&gt;&lt;span style="color: #cd0000;"&gt;Atoms&lt;/span&gt;&lt;span style="color: #228b22;"&gt;' object."&lt;/span&gt;))
 &lt;span style="color: #228b22;"&gt;"A class to represent a calculator that runs VASP."&lt;/span&gt;)


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;view-atoms&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Open the ase-gui"&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;unless&lt;/span&gt; (and (file-exists-p &lt;span style="color: #228b22;"&gt;"POSCAR"&lt;/span&gt;)
               (file-exists-p &lt;span style="color: #228b22;"&gt;"POTCAR"&lt;/span&gt;))
    (write-poscar calc)
    (write-potcar calc))
  (shell-command &lt;span style="color: #228b22;"&gt;"ase-gui POSCAR"&lt;/span&gt;))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;write-poscar&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"create a POSCAR file for CALC."&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;with-temp-file&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"POSCAR"&lt;/span&gt;
    (insert &lt;span style="color: #228b22;"&gt;"Created by jasp.el\n"&lt;/span&gt;)
    (insert &lt;span style="color: #228b22;"&gt;"  1.0"&lt;/span&gt;) ; &lt;span style="color: #ff0000; font-weight: bold;"&gt;unit cell scale factor&lt;/span&gt;
    (&lt;span style="color: #8b0000;"&gt;let*&lt;/span&gt; ((atoms (oref calc atoms))
           (cell (oref atoms cell)))
      (loop for v in cell
            for i below (length cell)     
            do
            (insert &lt;span style="color: #228b22;"&gt;"\n"&lt;/span&gt;)
            (loop for j below (length cell)
                  do
                  (&lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; (equal i j)
                      (insert (format &lt;span style="color: #228b22;"&gt;" %f "&lt;/span&gt; (float (elt cell i))))
                    (insert (format &lt;span style="color: #228b22;"&gt;" %f "&lt;/span&gt; 0.0 ))))))
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;The next line is counts for each atom type. For each number in&lt;/span&gt;
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;this line, there will be a copy of the POTCAR in the POTCAR&lt;/span&gt;
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;file. In ase, we sort the atoms and group them so that there is&lt;/span&gt;
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;only one POTCAR per atom. We do not do that here yet. We will&lt;/span&gt;
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;have a POTCAR for each atom.&lt;/span&gt;
    (insert &lt;span style="color: #228b22;"&gt;"\n"&lt;/span&gt;)
    (loop for atom in (oref (oref calc atoms) atoms)
          do (insert (format &lt;span style="color: #228b22;"&gt;"1 "&lt;/span&gt;)))
    
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;now we do the atoms&lt;/span&gt;
    (insert &lt;span style="color: #228b22;"&gt;"\nCartesian\n"&lt;/span&gt;)
    (loop for atom in (oref (oref calc atoms) atoms)
          do
          (insert
           (format &lt;span style="color: #228b22;"&gt;"%f %f %f\n"&lt;/span&gt;
                   (oref atom x)
                   (oref atom y)
                   (oref atom z))))
    (buffer-string)))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;write-kpoints&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Create KPOINTS file for CALC. &lt;/span&gt;
&lt;span style="color: #228b22;"&gt;Limited to automatic generation, and no offset."&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;with-temp-file&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"KPOINTS"&lt;/span&gt;
    (insert &lt;span style="color: #228b22;"&gt;"Automatic mesh&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;0&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;Monkhorst-Pack&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;"&lt;/span&gt;)
    (&lt;span style="color: #8b0000;"&gt;dolist&lt;/span&gt; (k (oref calc kpts))
      (insert (format &lt;span style="color: #228b22;"&gt;"%4d "&lt;/span&gt; k)))
    (insert &lt;span style="color: #228b22;"&gt;"\n0.0 0.0 0.0"&lt;/span&gt;)
    (buffer-string)))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;write-potcar&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Generate the POTCAR file for CALC.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;No `&lt;/span&gt;&lt;span style="color: #cd0000;"&gt;Atom&lt;/span&gt;&lt;span style="color: #228b22;"&gt;' grouping is done, there is one POTCAR for each atom."&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;with-temp-file&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"POTCAR"&lt;/span&gt;
    (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((xc (oref calc xc))
          (atoms (oref calc atoms))
          (vasp_pp_path (getenv &lt;span style="color: #228b22;"&gt;"VASP_PP_PATH"&lt;/span&gt;)))
      (loop for atom in (oref atoms atoms)
            do
            (insert-file-contents
             (f-join
              vasp_pp_path
              (concat &lt;span style="color: #228b22;"&gt;"potpaw_"&lt;/span&gt; xc)
              (oref atom symbol)
              &lt;span style="color: #228b22;"&gt;"POTCAR"&lt;/span&gt;))))
    (buffer-string)))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;write-incar&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Generate the INCAR file for CALC."&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;with-temp-file&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"INCAR"&lt;/span&gt;
    (insert (format &lt;span style="color: #228b22;"&gt;"ENCUT = %f\n"&lt;/span&gt; (oref calc encut)))
    (insert (format &lt;span style="color: #228b22;"&gt;"NBANDS = %d\n"&lt;/span&gt; (oref calc nbands)))
    (buffer-string)))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;run&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Write out input files, and run VASP as a simple shell command"&lt;/span&gt;
  (write-poscar calc)
  (write-kpoints calc)
  (write-potcar calc)
  (write-incar calc)
  (shell-command &lt;span style="color: #228b22;"&gt;"vasp"&lt;/span&gt;))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;update&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Run vasp if needed for CALC.&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;We just check for a properly ended OUTCAR."&lt;/span&gt;
  (with-current-directory
   (oref calc wd)
   (&lt;span style="color: #8b0000;"&gt;unless&lt;/span&gt; (and (file-exists-p &lt;span style="color: #228b22;"&gt;"OUTCAR"&lt;/span&gt;)
                (&lt;span style="color: #8b0000;"&gt;with-temp-buffer&lt;/span&gt;
                  (insert-file-contents &lt;span style="color: #228b22;"&gt;"OUTCAR"&lt;/span&gt;)
                  (re-search-forward
                  &lt;span style="color: #228b22;"&gt;"                 Voluntary context switches:"&lt;/span&gt;
                  (point-max)
                  t)))
     (run calc))))


(&lt;span style="color: #8b0000;"&gt;defmethod&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;get-potential-energy&lt;/span&gt; ((calc Jasp))
  &lt;span style="color: #228b22;"&gt;"Get potential energy from CALC."&lt;/span&gt;
  (update calc)
  (with-current-directory
   (oref calc wd)
   (&lt;span style="color: #8b0000;"&gt;with-temp-buffer&lt;/span&gt;
     (insert-file-contents &lt;span style="color: #228b22;"&gt;"OUTCAR"&lt;/span&gt;)
     ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;go to last entry&lt;/span&gt;
     (&lt;span style="color: #8b0000;"&gt;while&lt;/span&gt; (re-search-forward
             &lt;span style="color: #228b22;"&gt;"free  energy   TOTEN  =\\s-*&lt;/span&gt;&lt;span style="color: #228b22;"&gt;\\&lt;/span&gt;&lt;span style="color: #228b22;"&gt;(&lt;/span&gt;&lt;span style="color: #228b22;"&gt;[-]?[0-9]*\\.[0-9]*&lt;/span&gt;&lt;span style="color: #228b22;"&gt;\\&lt;/span&gt;&lt;span style="color: #228b22;"&gt;)&lt;/span&gt;&lt;span style="color: #228b22;"&gt; eV"&lt;/span&gt;
             (point-max)
             t)
       nil)
     ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;return last match&lt;/span&gt;
     (string-to-number  (match-string 1)))))

(&lt;span style="color: #8b0000;"&gt;provide&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;jasp&lt;/span&gt;)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
get-potential-energy
&lt;/pre&gt;


&lt;p&gt;
This is worth some discussion. On one hand, the constructor is a bit more verbose than the implementation in Python. In Python we use a context handler in place of the macro here. On the other hand, that verbosity comes with detailed, accessible documentation for each argument. We only considered the simplest of input arguments. It might be trickier to include lists, and other types of input. But I think those can all be worked out like they were in ase. We only implemented the simplest job control logic, but that also can be worked out. The biggest challenge might be getting more complex data from the output. Nearly everything can be obtained from vasprun.xml also, in the event that parsing is to slow or difficult.
&lt;/p&gt;

&lt;p&gt;
Now, let us test this out. We can make a calculator:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(setq calc (Jasp
            nil
            &lt;span style="color: #cd0000;"&gt;:xc&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"PBE"&lt;/span&gt;
            &lt;span style="color: #cd0000;"&gt;:encut&lt;/span&gt; 350
            &lt;span style="color: #cd0000;"&gt;:nbands&lt;/span&gt; 6
            &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; (Atoms
                    nil
                    &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; `(,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
                             ,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 1.1 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0))
                    &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt; '(8 9 10))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
[object Jasp nil "." 350 6 (1 1 1) "PBE" [object Atoms nil ([object Atom nil "C" 0 0 0] [object Atom nil "O" 1.1 0 0]) (8 9 10)]]
&lt;/pre&gt;

&lt;p&gt;
We can call the class functions like this. Here we write out the corresponding POSCAR:
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(write-poscar calc)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Created by jasp.el
  1.0
 8.000000  0.000000  0.000000 
 0.000000  9.000000  0.000000 
 0.000000  0.000000  10.000000 
1 1 
Cartesian
0.000000 0.000000 0.000000
1.100000 0.000000 0.000000
&lt;/pre&gt;

&lt;p&gt;
It looks a little backward if you have only seen Python, where this would be calc.write&lt;sub&gt;poscar&lt;/sub&gt;(). It is almost the same characters, just a different order (and no . in lisp)!
&lt;/p&gt;

&lt;p&gt;
Here we get the KPOINTS file:
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(write-kpoints calc)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
Automatic mesh
0
Monkhorst-Pack
   1    1    1 
0.0 0.0 0.0
&lt;/pre&gt;


&lt;p&gt;
I cannot show the POTCAR file for licensing reasons, but it works.
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(write-potcar calc)
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
and the INCAR file:
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(write-incar calc)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
ENCUT = 350.000000
NBANDS = 6
&lt;/pre&gt;

&lt;p&gt;
We run a calculation like this. This will run vasp directly (not through the queue system). 
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(run calc)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
0
&lt;/pre&gt;

&lt;p&gt;
The returned 0 means the shell command finished correctly.
&lt;/p&gt;

&lt;p&gt;
And we retrieve the potential energy like this:
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(get-potential-energy calc)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
-14.687906
&lt;/pre&gt;

&lt;p&gt;
Not bad. That is close to the result we got from a similar calculation &lt;a href="http://kitchingroup.cheme.cmu.edu/dft-book/dft.html#sec-3-3-1"&gt;here&lt;/a&gt; . 
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-sec-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;&lt;span class="section-number-2"&gt;4&lt;/span&gt; Putting it all together to run calculations&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;
&lt;p&gt;
If we put this all together the way we might use it in practice, it looks like this. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(load-file &lt;span style="color: #228b22;"&gt;"Atom.el"&lt;/span&gt;)
(load-file &lt;span style="color: #228b22;"&gt;"Atoms.el"&lt;/span&gt;)
(load-file &lt;span style="color: #228b22;"&gt;"Jasp.el"&lt;/span&gt;)

(&lt;span style="color: #8b0000;"&gt;let*&lt;/span&gt; ((co (Atoms
            nil
            &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; `(,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
                     ,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 1.1 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0))
            &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt; '(8 9 10)))

       (calc (Jasp
              nil
              &lt;span style="color: #cd0000;"&gt;:xc&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"PBE"&lt;/span&gt;
              &lt;span style="color: #cd0000;"&gt;:nbands&lt;/span&gt; 6
              &lt;span style="color: #cd0000;"&gt;:encut&lt;/span&gt; 350
              &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; co)))
  
  (get-potential-energy calc))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
-14.687906
&lt;/pre&gt;

&lt;p&gt;
Compare this with this Python code which does approximately the same thing:
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; ase &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; Atoms, Atom
&lt;span style="color: #8b0000;"&gt;from&lt;/span&gt; jasp &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; *

&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;co&lt;/span&gt; = Atoms([Atom(&lt;span style="color: #228b22;"&gt;'C'&lt;/span&gt;, [&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;,   &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;]),
            Atom(&lt;span style="color: #228b22;"&gt;'O'&lt;/span&gt;, [&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;1&lt;/span&gt;.&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;1&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;, &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;0&lt;/span&gt;])],
            &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;cell&lt;/span&gt;=(&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;6&lt;/span&gt;., &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;6&lt;/span&gt;., &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;6&lt;/span&gt;.))

&lt;span style="color: #8b0000;"&gt;with&lt;/span&gt; jasp(&lt;span style="color: #228b22;"&gt;'molecules/simple-co'&lt;/span&gt;, &lt;span style="color: #ff0000; font-weight: bold;"&gt;#output dir&lt;/span&gt;
          &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;xc&lt;/span&gt;=&lt;span style="color: #228b22;"&gt;'PBE'&lt;/span&gt;,  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# the exchange-correlation functional&lt;/span&gt;
          &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;nbands&lt;/span&gt;=&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;6&lt;/span&gt;,  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# number of bands&lt;/span&gt;
          &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;encut&lt;/span&gt;=&lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;350&lt;/span&gt;, &lt;span style="color: #ff0000; font-weight: bold;"&gt;# planewave cutoff&lt;/span&gt;
          &lt;span style="color: #000000; background-color: #cccccc; font-weight: bold;"&gt;atoms&lt;/span&gt;=co) &lt;span style="color: #8b0000;"&gt;as&lt;/span&gt; calc:
    &lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; &lt;span style="color: #228b22;"&gt;'energy = {0} eV'&lt;/span&gt;.format(co.get_potential_energy())
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
They look pretty similar. One thing clearly missing from emacs-lisp that Python has is full support for numerics and plotting. Some of this could be addressed via &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2014/10/19/Using-Pymacs-to-integrate-Python-into-Emacs/"&gt;Pymacs&lt;/a&gt; , but certainly not all of it. Some of it could also be handled using org-mode to enable data from emacs-lisp to go to other code blocks that can handle it. 
&lt;/p&gt;

&lt;p&gt;
Finally, for a little fun, we illustrate mapping over a range of bond lengths. There is more than one way to do this. For example, we could create a list of calculators, and then run over them. Here we create one calculator, and just change the x position in a loop. We use the more general setf approach instead of oset to see what it looks like.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let*&lt;/span&gt; ((co (Atoms
            nil
            &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; `(,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"C"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0)
                     ,(Atom nil &lt;span style="color: #cd0000;"&gt;:symbol&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"O"&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;:x&lt;/span&gt; 1.1 &lt;span style="color: #cd0000;"&gt;:y&lt;/span&gt; 0 &lt;span style="color: #cd0000;"&gt;:z&lt;/span&gt; 0))
            &lt;span style="color: #cd0000;"&gt;:cell&lt;/span&gt; '(8 9 10)))
       (calc (Jasp
              nil
              &lt;span style="color: #cd0000;"&gt;:wd&lt;/span&gt; nil
              &lt;span style="color: #cd0000;"&gt;:xc&lt;/span&gt; &lt;span style="color: #228b22;"&gt;"PBE"&lt;/span&gt;
              &lt;span style="color: #cd0000;"&gt;:nbands&lt;/span&gt; 6
              &lt;span style="color: #cd0000;"&gt;:encut&lt;/span&gt; 350
              &lt;span style="color: #cd0000;"&gt;:atoms&lt;/span&gt; co)))
  (&lt;span style="color: #8b0000;"&gt;dolist&lt;/span&gt; (d '(1.05 1.1 1.15 1.2 1.25))
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;change working directory&lt;/span&gt;
    (setf (oref calc wd) (format &lt;span style="color: #228b22;"&gt;"co-%s"&lt;/span&gt; d))
    ;; &lt;span style="color: #ff0000; font-weight: bold;"&gt;set x-coordinate on oxygen atom&lt;/span&gt;
    (setf (oref (elt (oref co atoms) 1) x) d)
    (print (format &lt;span style="color: #228b22;"&gt;"d = %s\nEnergy = %s eV"&lt;/span&gt;
                   d
                   (get-potential-energy calc)))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
"d = 1.05
Energy = -14.195892 eV"

"d = 1.1
Energy = -14.698456 eV"

"d = 1.15
Energy = -14.814809 eV"

"d = 1.2
Energy = -14.660395 eV"

"d = 1.25
Energy = -14.319904 eV"
&lt;/pre&gt;

&lt;p&gt;
See &lt;a href="http://kitchingroup.cheme.cmu.edu/dft-book/dft.html#sec-3-4-1"&gt;http://kitchingroup.cheme.cmu.edu/dft-book/dft.html#sec-3-4-1&lt;/a&gt; for how this was done in Python. It looks pretty similar to me.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div id="outline-container-sec-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;&lt;span class="section-number-2"&gt;5&lt;/span&gt; Summary thoughts&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;
&lt;p&gt;
We implemented a bare bones emacs-lisp calculator for VASP. The library automates creation of input files, running the calculation, and parsing the output.
&lt;/p&gt;

&lt;p&gt;
It seems pretty feasible to implement a pretty complete interface to VASP in emacs-lisp. The main reasons to do this are:
&lt;/p&gt;
&lt;ol class="org-ol"&gt;
&lt;li&gt;Integrated access to documentation
&lt;/li&gt;
&lt;li&gt;Emacs editing of emacs-lisp code 
&lt;/li&gt;
&lt;li&gt;Integration with org-mode
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;
Even with Python editor that had access to documentation as deeply integrated as emacs has with emacs-lisp, it would still just be a Python editor, i.e. you probably could not use the editor to write org-mode, LaTeX, etc&amp;#x2026; It is time to recognize we need both scientific document creation &lt;i&gt;and&lt;/i&gt; code editing capability in the same place! This kind of suggests a need to get a better Python environment going in Emacs, which deeper integration of the documentation. See &lt;a href="http://kitchingroup.cheme.cmu.edu/blog/2014/12/20/A-new-mode-for-Python-documentation/"&gt;this&lt;/a&gt; post for some progress in that area!
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2014 by John Kitchin. See the &lt;a href="/copying.html"&gt;License&lt;/a&gt; for information about copying.&lt;p&gt;&lt;p&gt;&lt;a href="/org/2014/12/24/A-prototype-implementation-of-jasp-in-emacs-lisp.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.10&lt;/p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
