<?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>Make a list of org-files in all the subdirectories of the current working directory</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2014/03/23/Make-a-list-of-org-files-in-all-the-subdirectories-of-the-current-working-directory</link>
      <pubDate>Sun, 23 Mar 2014 15:36:56 EDT</pubDate>
      <category><![CDATA[recursive]]></category>
      <category><![CDATA[emacs]]></category>
      <category><![CDATA[org-mode]]></category>
      <guid isPermaLink="false">ol_gYLGLHCsyZ025rQ_qIKwXg34=</guid>
      <description>Make a list of org-files in all the subdirectories of the current working directory</description>
      <content:encoded><![CDATA[



&lt;p&gt;
It would be helpful to get a listing of org-files in a directory tree in the form of clickable links. This would be useful, for example, to find all files associated with a project in a directory with a particular extension, or to do some action on all files that match a pattern. To do this, we will have to recursively walk through the directories and examine their contents. 
&lt;/p&gt;

&lt;p&gt;
Let us examine some of the commands we will need to use. One command is to get the contents of a directory. We will explore the contents of a directory called &lt;code&gt;literate&lt;/code&gt; in my computer.
&lt;/p&gt;

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

&lt;pre class="src src-emacs-lisp"&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;list contents of the directory&lt;/span&gt;
(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((abspath nil)
      (match nil)
      (nosort t))
  (directory-files &lt;span style="color: #228b22;"&gt;"literate"&lt;/span&gt; abspath match nosort))
&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="left" /&gt;

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

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

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

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

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

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

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

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

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

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

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

&lt;col  class="left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;makefile-main&lt;/td&gt;
&lt;td class="left"&gt;Makefile&lt;/td&gt;
&lt;td class="left"&gt;main.o&lt;/td&gt;
&lt;td class="left"&gt;main.f90&lt;/td&gt;
&lt;td class="left"&gt;main&lt;/td&gt;
&lt;td class="left"&gt;literate.org&lt;/td&gt;
&lt;td class="left"&gt;hello.f90&lt;/td&gt;
&lt;td class="left"&gt;circle.o&lt;/td&gt;
&lt;td class="left"&gt;circle.mod&lt;/td&gt;
&lt;td class="left"&gt;circle.f90&lt;/td&gt;
&lt;td class="left"&gt;circle-area.png&lt;/td&gt;
&lt;td class="left"&gt;archive&lt;/td&gt;
&lt;td class="left"&gt;a.out&lt;/td&gt;
&lt;td class="left"&gt;..&lt;/td&gt;
&lt;td class="left"&gt;.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Note the presence of &lt;code&gt;.&lt;/code&gt; and &lt;code&gt;..&lt;/code&gt;. Those stand for current directory and one directory up. We should remove those from the list. We can do that like this.
&lt;/p&gt;

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

&lt;pre class="src src-emacs-lisp"&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;remove . and ..&lt;/span&gt;
(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((abspath nil)
      (match nil)
      (nosort t))
  (remove &lt;span style="color: #228b22;"&gt;"."&lt;/span&gt; 
          (remove &lt;span style="color: #228b22;"&gt;".."&lt;/span&gt; 
                  (directory-files &lt;span style="color: #228b22;"&gt;"literate"&lt;/span&gt; abspath match nosort))))
&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="left" /&gt;

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

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

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

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

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

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

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

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

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

&lt;col  class="left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;makefile-main&lt;/td&gt;
&lt;td class="left"&gt;Makefile&lt;/td&gt;
&lt;td class="left"&gt;main.o&lt;/td&gt;
&lt;td class="left"&gt;main.f90&lt;/td&gt;
&lt;td class="left"&gt;main&lt;/td&gt;
&lt;td class="left"&gt;literate.org&lt;/td&gt;
&lt;td class="left"&gt;hello.f90&lt;/td&gt;
&lt;td class="left"&gt;circle.o&lt;/td&gt;
&lt;td class="left"&gt;circle.mod&lt;/td&gt;
&lt;td class="left"&gt;circle.f90&lt;/td&gt;
&lt;td class="left"&gt;circle-area.png&lt;/td&gt;
&lt;td class="left"&gt;archive&lt;/td&gt;
&lt;td class="left"&gt;a.out&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Next, we need to know if a given entry in the directory files is a file or a directory. Emacs-lisp has a few functions for that. We use absolute filenames here since the paths are relative to the  "molecules" directory. Note we could use absolute paths in directory-files, but that makes it hard to remove "." and "..".
&lt;/p&gt;

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

&lt;pre class="src src-emacs-lisp"&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;print types of files in the directory&lt;/span&gt;
(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((root &lt;span style="color: #228b22;"&gt;"literate"&lt;/span&gt;)
      (abspath nil)
      (match nil)
      (nosort t))
  (mapcar (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (x)
            (&lt;span style="color: #8b0000;"&gt;cond&lt;/span&gt;
             ((file-directory-p (expand-file-name x root))
              (print (format &lt;span style="color: #228b22;"&gt;"%s is a directory"&lt;/span&gt; x)))
             ((file-regular-p (expand-file-name x root))
              (print (format &lt;span style="color: #228b22;"&gt;"%s is a regular file"&lt;/span&gt; x)))))
          (remove &lt;span style="color: #228b22;"&gt;"."&lt;/span&gt; 
                  (remove &lt;span style="color: #228b22;"&gt;".."&lt;/span&gt; 
                          (directory-files root abspath match nosort)))))
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
"makefile-main is a regular file"

"Makefile is a regular file"

"main.o is a regular file"

"main.f90 is a regular file"

"main is a regular file"

"literate.org is a regular file"

"hello.f90 is a regular file"

"circle.o is a regular file"

"circle.mod is a regular file"

"circle.f90 is a regular file"

"circle-area.png is a regular file"

"archive is a directory"

"a.out is a regular file"
&lt;/pre&gt;

&lt;p&gt;
Now, we are at the crux of this problem. We can differentiate between files and directories. For each directory in this directory, we need to recurse into it, and list the contents. There is some code at &lt;a href="http://turingmachine.org/bl/2013-05-29-recursively-listing-directories-in-elisp.html"&gt;http://turingmachine.org/bl/2013-05-29-recursively-listing-directories-in-elisp.html&lt;/a&gt; which does this, but I found that I had to modify the code to not list directories, and here I want to show a simpler recursive code. 
&lt;/p&gt;

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

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;defun&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;os-walk&lt;/span&gt; (root)
  &lt;span style="color: #228b22;"&gt;"recursively walks through directories getting list of absolute paths of files"&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((files '()) &lt;span style="color: #ff0000; font-weight: bold;"&gt;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;empty list to store results&lt;/span&gt;
        (current-list (directory-files root t)))
    &lt;span style="color: #ff0000; font-weight: bold;"&gt;;;&lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;process current-list&lt;/span&gt;
    (&lt;span style="color: #8b0000;"&gt;while&lt;/span&gt; current-list
      (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((fn (car current-list))) &lt;span style="color: #ff0000; font-weight: bold;"&gt;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;get next entry&lt;/span&gt;
        (&lt;span style="color: #8b0000;"&gt;cond&lt;/span&gt; 
         &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;regular files&lt;/span&gt;
         ((file-regular-p fn)
          (add-to-list 'files fn))
         &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;directories&lt;/span&gt;
         ((and
           (file-directory-p fn)
           &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;ignore . and ..&lt;/span&gt;
           (not (string-equal &lt;span style="color: #228b22;"&gt;".."&lt;/span&gt; (substring fn -2)))
           (not (string-equal &lt;span style="color: #228b22;"&gt;"."&lt;/span&gt; (substring fn -1))))
          &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;we have to recurse into this directory&lt;/span&gt;
          (setq files (append files (os-walk fn))))
        )
      &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;cut list down by an element&lt;/span&gt;
      (setq current-list (cdr current-list)))
      )
    files))

(os-walk &lt;span style="color: #228b22;"&gt;"literate"&lt;/span&gt;)
&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="left" /&gt;

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

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

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

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

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

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

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

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

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

&lt;col  class="left" /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/makefile-main&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/main.o&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/main.f90&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/main&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/literate.org&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/hello.f90&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/circle.o&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/circle.mod&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/circle.f90&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/circle-area.png&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/a.out&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/Makefile&lt;/td&gt;
&lt;td class="left"&gt;c:/Users/jkitchin/Dropbox/blogofile-jkitchin.github.com/&lt;sub&gt;blog&lt;/sub&gt;/literate/archive/empty-text-file.txt&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;
Nice, that gives us a recursive listing of all the files in this directory tree. Let us take this a step further, and apply a function to that list to filter out a list of the org files. We will also create org-links out of these files.
&lt;/p&gt;

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

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;defun&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;os-walk&lt;/span&gt; (root)
  (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((files '()) &lt;span style="color: #ff0000; font-weight: bold;"&gt;;&lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;empty list to store results&lt;/span&gt;
        (current-list (directory-files root t)))
    &lt;span style="color: #ff0000; font-weight: bold;"&gt;;;&lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;process current-list&lt;/span&gt;
    (&lt;span style="color: #8b0000;"&gt;while&lt;/span&gt; current-list
      (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((fn (car current-list))) &lt;span style="color: #ff0000; font-weight: bold;"&gt;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;get next entry&lt;/span&gt;
        (&lt;span style="color: #8b0000;"&gt;cond&lt;/span&gt; 
         &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;regular files&lt;/span&gt;
         ((file-regular-p fn)
          (add-to-list 'files fn))
         &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;directories&lt;/span&gt;
         ((and
           (file-directory-p fn)
           &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;ignore . and ..&lt;/span&gt;
           (not (string-equal &lt;span style="color: #228b22;"&gt;".."&lt;/span&gt; (substring fn -2)))
           (not (string-equal &lt;span style="color: #228b22;"&gt;"."&lt;/span&gt; (substring fn -1))))
          &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;we have to recurse into this directory&lt;/span&gt;
          (setq files (append files (os-walk fn))))
        )
      &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;cut list down by an element&lt;/span&gt;
      (setq current-list (cdr current-list)))
      )
    files))

(&lt;span style="color: #8b0000;"&gt;require&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;cl&lt;/span&gt;)

(mapcar 
 (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (x) (princ (format &lt;span style="color: #228b22;"&gt;"[[%s][%s]]\n"&lt;/span&gt; x (file-relative-name x &lt;span style="color: #228b22;"&gt;"."&lt;/span&gt;))))
 (remove-if-not 
  (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (x) (string= (file-name-extension x) &lt;span style="color: #228b22;"&gt;"org"&lt;/span&gt;))
  (os-walk &lt;span style="color: #228b22;"&gt;"literate"&lt;/span&gt;)))
&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;
&lt;a href="/media/2014-03-23-Make-a-list-of-org-files-in-all-the-subdirectories-of-the-current-working-directory/literate.org"&gt;literate/literate.org&lt;/a&gt; 
&lt;/p&gt;


&lt;p&gt;
That is certainly functional. It might be nice to format the links a bit nicer to show their structure in a table of contents way, or to sort them in a nice order if there were many of these files. 
&lt;/p&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/03/23/Make-a-list-of-org-files-in-all-the-subdirectories-of-the-current-working-directory.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;&lt;p&gt;Org-mode version = 8.2.5h&lt;/p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Some of this, sum of that</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/02/02/Some-of-this-sum-of-that</link>
      <pubDate>Sat, 02 Feb 2013 09:00:00 EST</pubDate>
      <category><![CDATA[recursive]]></category>
      <category><![CDATA[miscellaneous]]></category>
      <guid isPermaLink="false">jwqtEjOstEi3dSxDeCT9k6P1SCs=</guid>
      <description>Some of this, sum of that</description>
      <content:encoded><![CDATA[


&lt;p&gt;
&lt;a href="http://matlab.cheme.cmu.edu/2012/05/29/some-of-this-sum-of-that/" &gt;Matlab plot&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
Python provides a sum function to compute the sum of a list. However, the sum function does not work on every arrangement of numbers, and it certainly does not work on nested lists. We will solve this problem with recursion.
&lt;/p&gt;

&lt;p&gt;
Here is a simple example.
&lt;/p&gt;

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

&lt;pre class="src src-python"&gt;v = [1, 2, 3, 4, 5, 6, 7, 8, 9] &lt;span style="color: #ff0000; font-weight: bold;"&gt;# a list&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; sum(v)

v = (1, 2, 3, 4, 5, 6, 7, 8, 9)  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# a tuple&lt;/span&gt;
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; sum(v)
&lt;/pre&gt;
&lt;/div&gt;

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

&lt;p&gt;
If you have data in a dictionary, sum works by default on the keys. You can give the sum function the values like this.
&lt;/p&gt;

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

&lt;pre class="src src-python"&gt;v = {&lt;span style="color: #228b22;"&gt;'a'&lt;/span&gt;:1, &lt;span style="color: #228b22;"&gt;'b'&lt;/span&gt;:3, &lt;span style="color: #228b22;"&gt;'c'&lt;/span&gt;:4}
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; sum(v.values())
&lt;/pre&gt;
&lt;/div&gt;

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

&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Nested lists&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
Suppose now we have nested lists. This kind of structured data might come up if you had grouped several things together. For example, suppose we have 5 departments, with 1, 5, 15, 7 and 17 people in them, and in each department they are divided into groups.
&lt;/p&gt;

&lt;p&gt;
Department 1: 1 person
Department 2: group of 2 and group of 3
Department 3: group of 4 and 11, with a subgroups of 5 and 6 making
              up the group of 11.
Department 4: 7 people
Department 5: one group of 8 and one group of 9.
&lt;/p&gt;

&lt;p&gt;
We might represent the data like this nested list. Now, if we want to compute the total number of people, we need to add up each group. We cannot simply sum the list, because some elements are single numbers, and others are lists, or lists of lists. We need to recurse through each entry until we get down to a number, which we can add to the running sum. 
&lt;/p&gt;
&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;v = [1, 
    [2, 3],
    [4, [5, 6]],
    7,
    [8,9]]

&lt;span style="color: #8b0000;"&gt;def&lt;/span&gt; recursive_&lt;span style="color: #8b0000;"&gt;sum&lt;/span&gt;(X):
    &lt;span style="color: #228b22;"&gt;'compute sum of arbitrarily nested lists'&lt;/span&gt;
    s = 0 &lt;span style="color: #ff0000; font-weight: bold;"&gt;# initial value of the sum&lt;/span&gt;

    &lt;span style="color: #8b0000;"&gt;for&lt;/span&gt; i &lt;span style="color: #8b0000;"&gt;in&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;range&lt;/span&gt;(len(X)):
        &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; types  &lt;span style="color: #ff0000; font-weight: bold;"&gt;# we use this to test if we got a number&lt;/span&gt;
        &lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;isinstance&lt;/span&gt;(X[i], (types.IntType,
                             types.LongType,
                             types.FloatType,
                             types.ComplexType)):
            &lt;span style="color: #ff0000; font-weight: bold;"&gt;# this is the terminal step&lt;/span&gt;
            s += X[i]
        &lt;span style="color: #8b0000;"&gt;else:&lt;/span&gt;
            &lt;span style="color: #ff0000; font-weight: bold;"&gt;# we did not get a number, so we recurse&lt;/span&gt;
            s += recursive_&lt;span style="color: #8b0000;"&gt;sum&lt;/span&gt;(X[i])
    &lt;span style="color: #8b0000;"&gt;return&lt;/span&gt; s

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; recursive_&lt;span style="color: #8b0000;"&gt;sum&lt;/span&gt;(v)
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; recursive_&lt;span style="color: #8b0000;"&gt;sum&lt;/span&gt;([1,2,3,4,5,6,7,8,9]) &lt;span style="color: #ff0000; font-weight: bold;"&gt;# test on non-nested list&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;

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

&lt;p&gt;
In &lt;a href="http://matlab.cheme.cmu.edu/2012/05/28/lather-rinse-and-repeat/" &gt;Post 1970&lt;/a&gt; we examined recursive functions that could be replaced by loops. Here we examine a function that can only work with recursion because the nature of the nested data structure is arbitrary. There are arbitary branches and depth in the data structure. Recursion is nice because you do not have to define that structure in advance.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2013 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/2013/02/02/Some-of-this,-sum-of-that.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
    <item>
      <title>Lather, rinse and repeat</title>
      <link>https://kitchingroup.cheme.cmu.edu/blog/2013/02/02/Lather-rinse-and-repeat</link>
      <pubDate>Sat, 02 Feb 2013 09:00:00 EST</pubDate>
      <category><![CDATA[recursive]]></category>
      <category><![CDATA[math]]></category>
      <guid isPermaLink="false">I15qBGbvhvGa9uyJItYHe2Y8LnI=</guid>
      <description>Lather, rinse and repeat</description>
      <content:encoded><![CDATA[


&lt;p&gt;
&lt;a href="http://matlab.cheme.cmu.edu/2012/05/28/lather-rinse-and-repeat/" &gt;Matlab post&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
Recursive functions are functions that call themselves repeatedly until some exit condition is met. Today we look at a classic example of recursive function for computing a factorial. The factorial of a non-negative integer n is denoted n!, and is defined as the product of all positive integers less than or equal to n.
&lt;/p&gt;

&lt;p&gt;
The key ideas in defining a recursive function is that there needs to be some logic to identify when to terminate the function. Then, you need logic that calls the function again, but with a smaller part of the problem. Here we recursively call the function with n-1 until it gets called with n=0. 0! is defined to be 1.
&lt;/p&gt;

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

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;def&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;recursive_factorial&lt;/span&gt;(n):
    &lt;span style="color: #228b22;"&gt;'''compute the factorial recursively. Note if you put a negative&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    number in, this function will never end. We also do not check if&lt;/span&gt;
&lt;span style="color: #228b22;"&gt;    n is an integer.'''&lt;/span&gt;
    &lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; n == 0:
        &lt;span style="color: #8b0000;"&gt;return&lt;/span&gt; 1
    &lt;span style="color: #8b0000;"&gt;else:&lt;/span&gt;
        &lt;span style="color: #8b0000;"&gt;return&lt;/span&gt; n * recursive_factorial(n - 1)

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; recursive_factorial(5)
&lt;/pre&gt;
&lt;/div&gt;

&lt;pre class="example"&gt;
120
&lt;/pre&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; scipy.misc &lt;span style="color: #8b0000;"&gt;import&lt;/span&gt; factorial
&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; factorial(5)
&lt;/pre&gt;
&lt;/div&gt;

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

&lt;div id="outline-container-0-1" class="outline-3"&gt;
&lt;h3 id="sec-0-1"&gt;&lt;span class="section-number-3"&gt;0.1&lt;/span&gt; Compare to a loop solution&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-0-1"&gt;
&lt;p&gt;
This example can also be solved by a loop. This loop is easier to read and understand than the recursive function. Note the recursive nature of defining the variable as itself times a number.
&lt;/p&gt;

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

&lt;pre class="src src-python"&gt;n = 5
factorial_loop = 1
&lt;span style="color: #8b0000;"&gt;for&lt;/span&gt; i &lt;span style="color: #8b0000;"&gt;in&lt;/span&gt; &lt;span style="color: #8b0000;"&gt;range&lt;/span&gt;(1, n + 1):
    factorial_loop *= i

&lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; factorial_loop
&lt;/pre&gt;
&lt;/div&gt;

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

&lt;p&gt;
There are some significant differences in this example than in Matlab. 
&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the syntax of the for loop is quite different with the use of the &lt;code&gt;in&lt;/code&gt; operator.
&lt;/li&gt;
&lt;li&gt;python has the nice *= operator to replace a = a * i
&lt;/li&gt;
&lt;li&gt;We have to loop from 1 to n+1 because the last number in the range is not returned.
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Conclusions&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
Recursive functions have a special niche in mathematical programming. There is often another way to accomplish the same goal. That is not always true though, and in a future post we will examine cases where recursion is the only way to solve a problem.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Copyright (C) 2013 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/2013/02/02/Lather,-rinse-and-repeat.org"&gt;org-mode source&lt;/a&gt;&lt;p&gt;]]></content:encoded>
    </item>
  </channel>
</rss>
