<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>cyocum</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/" />
    <link rel="self" type="application/atom+xml" href="http://blogs.perl.org/users/cyocum/atom.xml" />
    <id>tag:blogs.perl.org,2009-11-03:/users/cyocum//522</id>
    <updated>2013-03-25T16:50:01Z</updated>
    <subtitle>Celticist, Computer Scientist, Nerd, sometimes a poet...</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 4.38</generator>

<entry>
    <title>Citations in the Humanities</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2013/03/citations-in-the-humanities.html" />
    <id>tag:blogs.perl.org,2013:/users/cyocum//522.4474</id>

    <published>2013-03-25T15:28:25Z</published>
    <updated>2013-03-25T16:50:01Z</updated>

    <summary>I have written a few articles and more than a few essays for school/class. I know most of the major citations styles (MHRA, MLA, Chicago, etc.). Mostly, however, I use biblatex to format them all. However, I have begun to...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="citations" label="citations" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="humanities" label="humanities" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="libraryofcongress" label="library of congress" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="permaurl" label="permaurl" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="url" label="url" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="worldcat" label="worldcat" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>I have written a few articles and more than a few essays for school/class.  I know most of the major citations styles (MHRA, MLA, Chicago, etc.).  Mostly, however, I use biblatex to format them all.  However, I have begun to think that most of these citation styles are really just pointers to other resources or more generally they are really just URLs with a arcane special formatting rules.  Many academics are obsessed with these bits of meta-data as they validate that the writer knows the rules of the road.  When I read academic articles or books, I generally don&#8217;t notice or skip over citations as, while they are necessary and useful, they don&#8217;t always help with digestion of the actual argument.  Although, occasionally in fields I know well, I will scan the citation to see who they are referencing just for credibility purposes.</p>

<p>In any case, that brings me to the format for these citations.  Many of them have: the author, publisher, date, place of publication, and page number.  In the &#8220;bad old days&#8221;, these were necessary to allow the reader to trace arguments and facts.  Now, however, we have the greatest machine ever created to do that for us: the URL and the Internet.  The first thing you may ask is: how do you cite books then?  Well, you may have not noticed but the Library of Congress now has a permalink.  For instance, <a href="http://lccn.loc.gov/93047677">The Road to Judgment</a>.  So, in the &#8220;Yocum Style Citation&#8221; (YSC), which I just made up, you would put &#8220;Stacey, Robin Chapman. <a href="http://lccn.loc.gov/93047677">http://lccn.loc.gov/93047677</a>. pg. 55&#8221; in a footnote.  For a journal article, the DOI, Handle, Jstor stable URL plus page number.  If you don&#8217;t want to use the Library of Congress permalink for books, you can use Worldcat&#8217;s (http://www.worldcat.org/oclc/29519665) instead.  Unfortunately, the <a href="http://www.bl.uk/">British Library</a> does not yet have permalinks for its collection.  What happens if the URL is too long?  DOI has a <a href="http://shortdoi.org/">shortening service</a> for DOIs.  OpenURLs are notoriously awful for being long.  I don&#8217;t have a solution to that but, given that DOIs are pretty much de facto now, it won&#8217;t matter too much.  You could use a shortened url in the text and a full URL in the bibliography, which are still required under YCS but are generally the same except that you put full URLs instead of any shortened ones that are in the text.</p>

<p>Why do this?  First, I am tired of the rather complex and arcane rules of citations in the Humanities.  Every journal seems to have its own preferred style and it makes submitting to these journals difficult and if you get turned down and resubmit it else where, you have to completely change everything.  Second, URLs are good enough now to cite with.  The Internet is not leaving us any time soon.  If you are that paranoid, just require the full citation in the bibliography.</p>

<p>It is high time we change to make it easier for everyone.  If I want to look up a book citation, I can just go to the url and get the book from my local library or if it is a DOI, Handle, or Jstor stable URL, I can get the article right away. If I don&#8217;t have access to the internet that second, it&#8217;s not like I cannot get access later.</p>

<p>Teaching the arcane rules with attached URLs just does not make much sense.  It duplicates information and makes it harder on the student or writer.  It makes the humanities look out of touch and old.  For medievalists, like myself, even the manuscripts now have URLs and can be referenced via them so instead of using some off-the-cuff MS citation method because each archive is different and each has a different way of cataloguing their collections, you can use the URL to point to the exact MS page you are talking about.</p>

<p>Anyway, I will probably be putting this into practice myself soon as I get the chance.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Writing State Monads in Ocaml</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2012/11/writing-state-monads-in-ocaml.html" />
    <id>tag:blogs.perl.org,2012:/users/cyocum//522.4073</id>

    <published>2012-11-27T14:49:41Z</published>
    <updated>2012-11-28T16:40:12Z</updated>

    <summary>One of my projects for quite a while is to understand Monads. I have been working on this, off and on, for an embarrassing amount of time. Yes, I could have just learned Haskell and that would have done it...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamlmonadsstate" label="ocaml monads state" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>One of my projects for quite a while is to understand Monads.  I have been working on this, off and on, for an embarrassing amount of time.  Yes, I could have just learned <a href="http://www.haskell.org/haskellwiki/Haskell">Haskell</a> and that would have done it but I wanted to be a pain in the arse.  Also, I had a horrible experience attempting to learn Haskell a long time ago.  Thus, I decided as I use Ocaml for most things then I should be able to do it.  First, I read <a href="http://blog.enfranchisedmind.com/2007/08/a-monad-tutorial-for-ocaml/">this article</a> a few times and <a href="http://brandon.si/code/the-state-monad-a-tutorial-for-the-confused/">this</a> which is really good and helps hugely in my understanding.  Anyway, I will take a step by step approach to showing how to write a State Monad for Ocaml.</p>
]]>
        <![CDATA[<p>So, first thing, what is a Monad? If you remember your undergraduate philosophy classes a <a href="http://en.wikipedia.org/wiki/Monadology">Monad</a> is an entity that is basically impenetrable but can communicate.  Thus, you don&#8217;t know what&#8217;s <em>in</em> the monad but you can communicate this.  With this in mind, a Monad in code is:</p>

<pre><code>module type MONAD =
  sig
    type 'a t
    val bind : 'a t -&gt; ('a -&gt; 'b t) -&gt; 'b t
    val return : 'a -&gt; 'a t
  end
</code></pre>

<p>You will notice here that I am declaring a <em>module type</em> and not a module.  Basically, I want to build monads out of things.  Thus, Monad in <a href="http://en.wikipedia.org/wiki/Object-oriented_programming">OO terms</a> is an abstract base class.  So, if we want to have a &#8220;kind of&#8221; Monad, we have to build it.  In any case,  the type <code>'a t</code> is the type system wrapper around a polymorphic value.  Basically, the type system wraps the value in a type so that it can be manipulated at the type level rather than at the value level.  <code>return</code> is kind of an odd way to put it but it &#8220;return&#8221;s a Monad out of a value of some kind (as has been pointed out by <a href="http://pozorvlak.livejournal.com/">pozorvlak</a>, <a href="http://blog.plover.com/prog/haskell/monad-terminology.html">mote</a> is a better term for values of that type and is a common mistake in Monad tutorials); we don&#8217;t care what kind (or type) the value is.  <code>bind</code> is a way for the user to bind a function to a Monad.  What you will get is the Monad feeding the value into the function and expecting a new Monad from the function then it will automatically return that Monad.</p>

<p>So, you can create different kinds of Monads to do different things and one of the side-effects is that side-effects are encapsulated within the Monad itself.  This means that you can do things which are not &#8220;pure&#8221; functional programming within a purely functional system (this isn&#8217;t strictly true either).  As you can see with <code>bind</code>, you will <em>always</em> return a Monad.  However, I can hear you saying, &#8220;but how do I get values out of a Monad?&#8221;  Don&#8217;t worry you can do that by having a function like this:</p>

<pre><code>val access : 'a t -&gt; 'a
</code></pre>

<p>This function is some times called &#8220;run&#8221; as well.  What it does is it asks the Monad for its computation (aka &#8220;a thunk&#8221;) as the Monad writer can put off actually doing the computation until this stage in the processing.  So, with that knowledge, let&#8217;s attempt the next stage:</p>

<pre><code>module type STATE =
  sig
     type t
     val empty : t
  end
</code></pre>

<p>Again, this is an abstract base class for &#8220;State&#8221; and it allows you to define your own state using a functor (I should write another thing on functors).  Basically, I don&#8217;t want to constrain the kinds of StateMonads and just let my users define what kind of state they need.  So, bringing this together with the MONAD declaration above:</p>

<pre><code>module type STATE_MONAD = 
   functor(State : STATE) -&gt;
     sig
       include MONAD
       val access : 'a t -&gt; 'a
       val put : State.t -&gt; unit t
       val get : State.t t
     end
</code></pre>

<p>Ok, again another declaration with no real code, please be patient all will be revealed.  This has access and put and get functions.  The access function, actually does the computation of functions that are bound within the Monad.  The put and get is something I am still trying to get my head around but it basically allows you to use the StateMonad as a variable store.  You can store things in it like a variable and retrieve the information and return it.</p>

<p>So, now on to some <b>Real Code</b>:</p>

<pre><code>module StateMonad : STATE_MONAD =
   functor(State : STATE) -&gt;
     struct
       type state = State.t
       type 'a t = state -&gt; ('a * state)
       let bind m f =
         fun s -&gt;
           match m s with 
              | (x, s') -&gt; f x s'
       let return a = fun s -&gt; (a, s)
       let access m =
           match m State.empty with
             | (x, s) -&gt; x
       let put s =
           fun _ -&gt; ((), s)
       let get =
           fun s -&gt; (s, s)
     end
</code></pre>

<p>So, lets break this down.  The functor allows the user to set what kind of state they want.  I will demonstrate this below.  The next bit is  <code>type 'a t = state -> ('a * state)</code>.  This defines the actual type that is wrapped inside the StateMonad.  Basicaly it said &#8220;This Monad is a function which takes one state and translates that state into a value and the next state&#8221;.  Thus, when you put a state in the StateMonad via <code>return</code>, it returns just what it says on the tin a function which takes a state and return a tuple of a value and the state.  The next function <code>bind</code> binds a function to the Monad and the monad calls the last function it created (in this case the &#8220;m&#8221; in the code) and matches the output of &#8220;m&#8221; with the expected tuple which is then passed to the next function in the chain.  The <code>access</code> function takes an empty state and that fires the function chain to come to a final state and value then the final value is returned.</p>

<p>Now, the <code>put</code> and <code>get</code> functions.  I am not sure I actually understand these as well as I should so I will tell you now that if what I say is wrong, <em>please</em> let me know.  So basically  <code>put</code> creates a degenerate return function which puts the value in the same kind of wrapper as a function.  The <code>get</code> passes back a function which fires immediately and gives you back it&#8217;s value.  I am completely unsure of how exactly this works but it seems to work in practice.  If someone could enlighten me, that would be wonderful.</p>

<p>So, now I will present a trivial example: IntStateMonad.</p>

<pre><code>   module IntStateMonad = StateMonad(
     struct
       type t = int
       let empty = 0
     end
    )
</code></pre>

<p>This tells the StateMonad what kind of state the user wants.  This basically fills in the functor State from above.  I am still slightly unsure if the empty value is really necessary or if there is some other way I can get the underlying function to fire without it.  So here is an example:</p>

<pre><code>let _ =
   let blah = IntStateMonad.return 1 in 
   let blah2 = IntStateMonad.bind blah (fun i -&gt; IntStateMonad.return (succ i)) in 
      print_endline (string_of_int (IntStateMonad.access blah2))
</code></pre>

<p>So, can we do something more Haskell like in this instance?  Yes, with the &#8220;perform&#8221; notation that is provided by <a href="https://bitbucket.org/camlspotter/pa_monad_custom">pa<em>monad</em>custom</a>, which uses camp4 to parse ocaml and do syntax transformations.  So we can do this like so:</p>

<pre><code>let return = IntStateMonad.return

let _ =
   let blah = 
        perform with IntStateMonad.bind in 
           a &lt;-- return 1;
           b &lt;-- return (succ a);
           return b
 in 
    print_endline (string_of_int (IntStateMonad.access blah))
</code></pre>

<p>So you have to bind the return and bind functions to something that is accessible by the pa_monad syntax.  Otherwise, it takes care of interleaving the binds then it returns the resulting Monad which you can run (or access&#8230;whatever you prefer to call it).</p>

<p>As for <code>get</code> and <code>put</code>, you can use it in this way:</p>

<pre><code>let _ =
   let blah = 
      perform with IntStateMonad.bind in 
        a &lt;-- return 1;
        b &lt;-- IntStateMonad.put 5;
        c &lt;-- return (succ a);      
        d &lt;-- IntStateMonad.get;
        return (c + d)
 in
    print_endline (string_of_int (IntStateMonad.access blah))
</code></pre>

<p>So, there you have it.  This is a Monad which keeps track of the state for you and will return the evaluated functions in a type wrapper.  This will also get and store state like a variable wrapped inside the bound function calls.  All side-effects are explicitly handled inside the Monad and <a href="http://en.wikipedia.org/wiki/Gottfried_Wilhelm_Leibniz">Leibniz</a> gets to smile.  Who said <a href="http://en.wikipedia.org/wiki/Philosophy">philosophy</a> was useless?</p>

<p><em>UPDATE</em> Added information on motes and clarified some stuff on side-effects in a purely functional programming language. </p>
]]>
    </content>
</entry>

<entry>
    <title>More Ocaml and Unicode</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2012/10/more-ocaml-and-unicode.html" />
    <id>tag:blogs.perl.org,2012:/users/cyocum//522.3962</id>

    <published>2012-10-16T13:27:12Z</published>
    <updated>2012-10-16T13:44:56Z</updated>

    <summary>I was wondering how you would do string encoding translation in Camomile (the Ocaml Unicode library). Why would you want to do this? Well, for instance, if you get a UTF-16 encoded string and you need to use PCRE on...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamunicodepcre" label="ocam unicode pcre" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>I was wondering how you would do string encoding translation in Camomile (the Ocaml Unicode library).  Why would you want to do this?  Well, for instance, if you get a UTF-16 encoded string and you need to use PCRE on it, which only takes UTF-8 input.  This means that you need to translate your string downward so you can match against it.</p>

<p>As Camomile uses functors extensively, you need to know how they work so you can follow the code example below.  However, it is fairly straight forward once you get your head around it (it took me a while to figure this out which is why I am putting this here).</p>

<pre><code>open CamomileLibraryDefault
open Camomile

module UTF8Decode = Camomile.CharEncoding.Make(UTF8)

let _ =
  try
    let changed = UTF8Decode.decode Camomile.CharEncoding.utf8 "神奈川大学" in 
      UTF8.validate changed;
      print_endline "yes!";
      exit 0
  with 
      | UTF8.Malformed_code -&gt;
          print_endline "no!";
          exit 1
</code></pre>
]]>
        

    </content>
</entry>

<entry>
    <title>Ocaml, Unicode, and Hashtables</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2012/08/ocaml-unicode-and-hashtables.html" />
    <id>tag:blogs.perl.org,2012:/users/cyocum//522.3733</id>

    <published>2012-08-24T11:14:47Z</published>
    <updated>2012-08-24T11:35:55Z</updated>

    <summary>So, Ocaml does not support Unicode out of the box. The &#8220;string&#8221; type is an 8 bit byte and that&#8217;s that. Some find this to be a major failing of the language in general and it is a pain in...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamlhashtablesunicode" label="ocaml hashtables unicode" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>So, Ocaml does not support Unicode out of the box.  The &#8220;string&#8221; type is an 8 bit byte and that&#8217;s that.  Some find this to be a major failing of the language in general and it is a pain in the ass.  However, there is a unicode library for ocaml <a href="http://camomile.sourceforge.net/">Camomile</a> which fills the gap.</p>

<p>In the project that I have been working on, I had to read in a Unicode file into Ocaml and create a &#8220;seen&#8221; hash.  Just as you would do in perl normally.  However, because Ocaml doesn&#8217;t support Unicode natively, you cannot use the generic Hashtable type &#8220;(&#8216;a, &#8216;b) t&#8221;, which stands for an arbitrary type for a key (the first &#8216;a) and an arbitrary type for the value (the second &#8216;b).  The key value types will be filled in by type inference as you use the Hashtable based on what you do with it.  This won&#8217;t work because the generic Hashtable depends on an equal function that will not conform to the Unicode standard compare.</p>

<p>All is not lost, however!  This is where one of the most powerful features of Ocaml comes into its own: the functor.  Modules in Ocaml can be parametrized in such a way as the user can redefine a module to meet his/her needs.  For Hashtables one can parametrize the Hash on the key value.  For instance:</p>

<pre><code>module UTF8Hash = Hashtbl.Make(
  struct
    type t = Camomile.UTF8.t
    let equal a b =
      if (Camomile.UTF8.compare a b) = 0 then
        true
      else
        false
     let hash = Hashtbl.hash
 end
)
</code></pre>

<p>Using the Hashtable Make functor, I set the key type to UTF8 then set the equal function to something that makes sense for UTF8 strings.  In the end, I left the hash function itself alone as I thought it would probably do the right thing and I didn&#8217;t want to get into it so I just defined it as itself.</p>

<p>Doing this creates a Hashtable type of &#8216;a UTF8Hash.t.  The &#8216;a is now the type of the value as you already know the type of the key for the hash.  In addition, doing this, you can create a hashtable that has an arbitrarily complex key type.  As long as you can write an equals function, you should be fine.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Ocaml and Hashtables</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2012/08/ocaml-and-hashtables.html" />
    <id>tag:blogs.perl.org,2012:/users/cyocum//522.3727</id>

    <published>2012-08-23T10:47:48Z</published>
    <updated>2012-08-23T16:45:31Z</updated>

    <summary>Coming from a Perl background and seeing the Ocaml Hashtbl documentation for the first time, it can be really confusing. Especially since there is no keys, values, and each functions in the documentation. So, first of all while Ocaml doesn&#8217;t...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamlhashtable" label="ocaml hashtable" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>Coming from a Perl background and seeing the Ocaml Hashtbl documentation for the first time, it can be really confusing.  Especially since there is no keys, values, and each functions in the <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html">documentation</a>.  So, first of all while Ocaml doesn&#8217;t give you these functions out of the box and I would argue that it should, you can write them fairly easily yourself using the fold function.</p>

<pre><code>let keys htbl =
  Hashtbl.fold (fun k _ accum -&gt; k::accum) htbl []
</code></pre>

<p>As you can see above the fold function gets three params, the first two are the key and value.  The third is the &#8220;accumulator&#8221; or in this case just a list where you will append the k variable.  The underscore means &#8220;anything&#8221; and tells the compiler that you don&#8217;t really care about the binding and it is not then necessary.</p>

<p>The values function works similarly.</p>

<pre><code>let values htbl = 
  Hashtbl.fold (fun _ v accum -&gt; v::accum) htbl []
</code></pre>

<p>The each function will be slightly different in that we need both k and v and the accum parameters.</p>

<pre><code>let each htbl =
  Hashtbl.fold (fun k v accum -&gt; (k, v)::accum) []
</code></pre>

<p>This creates a list of tuples of the key and value.  You can then match against it and get the key and value out.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>How to do confrence proceedings in (Xe)Latex</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2012/03/how-to-do-confrence-proceedings-in-xelatex.html" />
    <id>tag:blogs.perl.org,2012:/users/cyocum//522.2955</id>

    <published>2012-03-19T09:43:47Z</published>
    <updated>2012-03-19T16:07:53Z</updated>

    <summary>So, I had actually been wondering this for quite a while as I had thought that I would at some point be asked to typeset a journal or conference proceedings. I searched on Google but I could never get anything...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="biblatex" label="biblatex" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="journals" label="journals" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="latex" label="latex" scheme="http://www.sixapart.com/ns/types#tag" />
    <category term="proceedings" label="proceedings" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>So, I had actually been wondering this for quite a while as I had thought that I would at some point be asked to typeset a journal or conference proceedings.  I searched on Google but I could never get anything worthwhile to come up.  However,  I decided the time for research was over and the time to just attempt it was on me; this is also because I now have a publisher for the colloquium proceedings that I am running in the summer.</p>

<p>So, first of all some code</p>

<pre><code>\documentclass[b5paper, 12pt, openright]{book}

\usepackage{xltxtra}

\setmainfont[Mapping=tex-text,%
SlantedFont={Linux Libertine Slanted O}]{Linux Libertine O}

\usepackage{polyglossia}

\setmainlanguage[variant=british]{english}

\setotherlanguage[variant=ancient]{greek}

\usepackage[b5paper, top=2.5cm, bottom=2.5cm, left=3cm, right=3cm]{geometry}

\usepackage[style=verbose-ibid, backend=biber]{biblatex}

\begin{document}

\frontmatter

\tableofcontents

\mainmatter

\begin{refsection}[Art1.bib]
\input{Art1.tex}
\end{refsection}

\begin{refsection}[Art2.bib]
\input{Art2.tex}
\end{refsection}

\end{document}</code></pre>

<p>A few things about this code should jump out at you.  First, I am using <a href="http://en.wikipedia.org/wiki/XeTeX">XeLaTeX</a> rather than PDFLaTeX.  I do this because it has a much, much easier font selection system than PDFLaTeX.  Second, you will notice that I am using <a href="http://ctan.org/pkg/biblatex">biblatex</a> and <a href="http://biblatex-biber.sourceforge.net/">biber</a>.  Like <a href="http://www.modernperlbooks.com/">Modern Perl</a>, the LaTeX community have moved on from BibTeX to biblatex and biber (the bibtex replacement) make working with bibliographies an absolute pleasure.</p>

<p>So, a few things to say here.  The <code>refsection</code> is the biblatex way of breaking bibliographies across chapters.  The argument is the bibliography file where the bibtex database format, which is still in use, is stored.</p>

<p>So, on to the problem most people have.  How do I put the author&#8217;s name and the title of their paper in the table of contents?  So here is some sample code from the Art1.tex file:</p>

<pre><code>\chapter*{Title \\
Author}
\addcontentsline{toc}{chapter}{Author -- \emph{Title}}
</code></pre>

<p>So, as you can see here, using the <code>\chapter*</code> command which doesn&#8217;t put the chapter information into the table of contents, you can then use the <code>\addcontentsline</code> command to manually put whatever you like on the command line.  There is one thing you need to do however unless you want all the sections and subsections to appear in the table of contents: you must use <code>\section*</code> in all instances otherwise things look odd.  So, the last thing you need to do is put your <code>\printbibliography</code> at the end of the file and do the same for each article and you are done.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Fun with Git</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2012/03/fun-with-git.html" />
    <id>tag:blogs.perl.org,2012:/users/cyocum//522.2934</id>

    <published>2012-03-13T15:06:40Z</published>
    <updated>2012-03-13T15:10:41Z</updated>

    <summary>So, git has the &#8212;all command, which adds all tracked and untracked files for staging to commit. I really dislike this because I wanted something that would just add all the tracked files for staging to commit. So, a friend...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="gitshellawk" label="git shell awk" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>So, git has the &#8212;all command, which adds all tracked and <em>untracked</em> files for staging to commit.  I really dislike this because I wanted something that would just add all the tracked files for staging to commit.  So, a friend of mine in the office said that he had a shell script which did it so I give you git add for only tracked files:</p>

<pre><code>git status | grep "modified: " | awk '{print $3}' | xargs git add
</code></pre>
]]>
        

    </content>
</entry>

<entry>
    <title>Ocaml Pet Peeve</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/11/ocaml-pet-peeve.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.2392</id>

    <published>2011-11-03T10:43:42Z</published>
    <updated>2011-11-03T11:00:36Z</updated>

    <summary>While I generally like Ocaml (as some of you may have guessed), there is one pet peeve of mine with the language: record fields names are module wide rather than record specific. For example: type t = { blah :...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamlrecordspetpeeves" label="ocaml records &quot;pet peeves&quot;" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>While I generally like Ocaml (as some of you may have guessed), there is one pet peeve of mine with the language: record fields names are module wide rather than record specific.  For example:</p>

<p>type t = {
    blah : int
  }</p>

<p>The record field <em>blah</em> is now a module wide name so I cannot reuse it.  Thus, field names that happen to be useful in two contexts are not allowed.  Although, to be honest, having two record names the same is probably a sign of a refactor point.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Annotating PDFs with Evince</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/10/annotating-pdfs-with-evince.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.2337</id>

    <published>2011-10-22T15:07:03Z</published>
    <updated>2011-10-22T15:16:59Z</updated>

    <summary>For a very long time, I was rather stuck because, as a scholar in the humanities who uses LaTeX, I could not get people to use LaTeX because they could not annotate people&#8217;s work without a professional copy of Acrobat....</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="pdfannotationslatexhumanitiesevincepopplar" label="pdf annotations latex humanities evince popplar" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>For a very long time, I was rather stuck because, as a scholar in the humanities who uses LaTeX, I could not get people to use LaTeX because they could not annotate people&#8217;s work without a professional copy of Acrobat.  Well, I have figured out how to do this using Evince.  How you may ask?  It is a simple process but it is rather <em>hidden</em> and it should be more prominent when looking at PDFs.</p>

<p>These instructions should work on Evince 3.2 in Xubuntu 11.10.</p>

<ul>
<li>Open your desired PDF in Evince.</li>
<li>In the upper left, you should see the word &#8220;Thumbnails&#8221; and a down arrow.</li>
<li>Press the down arrow and choose &#8220;Annoations&#8221;.  This will show you the list of annotations in the current PDF.</li>
<li>You should see an &#8220;add&#8221; tab.  Click on the add tab.  There should be a small pencil icon.</li>
<li>Click on the pencil icon. Your cursor should turn into a plus sign.</li>
<li>Click where you want to put your annotation.  A yellow icon should appear and a small yellow window with your name in the title bar should appear.</li>
<li>Type your annotation.  I had a small bit of trouble getting it to recognize my typing but resizing the yellow window did the trick.</li>
<li>When you are done, you will need to save the PDF as a different file.  I am not sure why this is but if I saved it on top of the old file, it didn&#8217;t take the annotations.</li>
</ul>

<p>Yay!!  You can now annotate PDFs in Linux.  My life has gotten <em>so much easier now</em>.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Books and Ebooks: Renumerations on the Future </title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/07/books-and-ebooks-renumerations-on-the-future.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.2023</id>

    <published>2011-07-27T11:02:46Z</published>
    <updated>2011-07-27T11:29:40Z</updated>

    <summary>My GF recently published a book, The Girl in the Bunker, which has a ebook version and a physical version. This caused me to think about the future of books (also, given the fact that I will probably never get...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ebooksbooksfuturegirlinthebunker" label="ebooks books future &quot;girl in the bunker&quot;" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>My GF recently published a book, <a href="http://www.amazon.co.uk/gp/product/B005AZHQUW/ref=s9_simh_gw_p351_d0_i1?pf_rd_m=A3P5ROKL5A1OLE&amp;pf_rd_s=center2&amp;pf_rd_r=0H0GVN9WAP55PQPS9PR2&amp;pf_rd_t=101&amp;pf_rd_p=467128533&amp;pf_rd_i=468294">The Girl in the Bunker</a>, which has a ebook version and a physical version.  This caused me to think about the future of books (also, given the fact that I will probably never get my <a href="http://hdl.handle.net/1842/3320">thesis</a> published as a monograph, I had cause to ponder).  So my basic premise is that ebooks will be the larger market than physical books.  The difference between the two will be that ebooks will be more like renting a book, see <a href="http://www.gnu.org/philosophy/right-to-read.html">The Right To Read</a>, while a physical book will be a true ownership position. </p>

<p>The difference will be reflected in the price.  My GF&#8217;s ebook is priced at just over £1 while her physical book is priced at £10.  The difference is <em>ownership</em>.  In the case of an ebook you will never <em>own</em> the book itself, see <a href="http://www.nytimes.com/2009/07/18/technology/companies/18amazon.html">Amazon&#8217;s revocation of 1984</a> as an example.  The difference between the price of the ebook and the price of the physical book is the convenience of the digital form and the lack of true ownership of the ebook.  </p>

<p>There is also a difference in risk.  The risk of an ebook is that the proprietor of the site whence you purchased the book could at any time disappear and take your ebook with them.  This means that the risk needs to be adjusted in the renter&#8217;s favor.  To entice people to rent an ebook means that it will need to be priced such that if the website shuts down, the renter is not out of pocket too much and thus will take the risk of renting the ebook.</p>

<p>So, while ebooks will probably be the larger market, I do not expect books to disappear any time soon due to the difference in legal positions and the risk involved.  As a scholar, this means that while it would be nice to have digital editions of the books that I need, I will more than likely continue to purchase books as I cannot afford to have a book vanish.  Also, some people like to collect <em>things</em> so while the market in plain books will diminish, the market in autographed copies will probably take up some of the slack.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Google+: A Review</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/06/google-a-review.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.1922</id>

    <published>2011-06-29T14:10:29Z</published>
    <updated>2011-06-29T14:24:14Z</updated>

    <summary>So, I got an invite to Google+ so I logged in and had a look around. I do like the rotary phone style addition of people to a &#8220;Circle&#8221;. Overall, it has the normal Googlely look and feel, which I...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="googlesocialnetworkingfail" label="google social networking fail" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>So, I got an invite to <a href="http://plus.google.com">Google+</a> so I logged in and had a look around.  I do like the rotary phone style addition of people to a &#8220;Circle&#8221;.  Overall, it has the normal Googlely look and feel, which I like.  However, there are problems, mostly due to lack of integration with the rest of Google.  It is as if the team had <em>no clue</em> about any of the other piecemeal attempts at social networking that Google has tried.</p>

<ol>
<li><p><em>Goolge Buzz</em>: does not appear in your &#8220;Status&#8221; list.  This is mildly frustrating.  You must go all the way to your profile page to see this.  They should just appear in your status list as per facebook.</p></li>
<li><p><em>Google Reader Sharing</em>: If you share something via Reader, it does not appear as a link in your Status list.  Again, this means that if you want to share something cool in your RSS feed, you would have to do it twice.</p></li>
<li><p><em>Google Reader Like</em>: What is the difference between <em>Google Reader Like</em> and <em>Google +1</em>?  Don&#8217;t know? Me either.  Again integration and semantic fail.</p></li>
<li><p><em>Lack of People</em>: It seems like they are dibbling this out (a la gmail) which means that there is currently only <em>one</em> friend currently on there even if I have a huge &#8220;Circle&#8221; of friends.  This is boring.</p></li>
<li><p><em>Do I really need another one</em>:  This is just getting ridiculous.  I don&#8217;t want to waste any more time.  I tried it because it was Google but I don&#8217;t want to sign up for another damn one of these.</p></li>
</ol>

<p>Anyway, these are my first impressions.  Maybe it will get better but it is leaving me a bit flat.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Mutually Recursive Types</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/06/mutually-recursive-types.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.1855</id>

    <published>2011-06-16T11:18:28Z</published>
    <updated>2011-06-16T11:31:01Z</updated>

    <summary>Unlike C/C++, Ocaml does not allow you to have &#8220;forward declarations&#8221; of types. In other words, if you have a record type A that has an entry that of type B and type B has an entry of type A,...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamltypesrecursion" label="ocaml types recursion" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>Unlike C/C++, Ocaml does not allow you to have &#8220;forward declarations&#8221; of types.  In other words, if you have a record type A that has an entry that of type B and type B has an entry of type A, you cannot forward declare B so that it is in scope for record type A.  In code:</p>

<pre><code>type A = { foo : B }

type B = Bar of A
</code></pre>

<p>To get around this limitation, Ocaml has &#8220;mutually recursive types&#8221; with the keyword &#8220;and&#8221;.  Now, &#8220;and&#8221; is overloaded to work with mutually recursive functions as well.  So, if you want to do this you can use &#8220;and&#8221; thusly:</p>

<pre><code>type A = { foo : B }
and
B = Bar of A
</code></pre>

<p>There is one thing to notice here: B does not have a &#8220;type&#8221; keyword in front of it.  This isn&#8217;t necessary because the idea of &#8220;type&#8221; crosses the &#8220;and&#8221; boundary.  Sometimes, you get long chains of this which can look slightly odd but isn&#8217;t too much of a problem most times.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Bitcoin: Money is a sign of poverty</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/06/bitcoin-my-thoughts-possibly-part-1.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.1845</id>

    <published>2011-06-12T18:55:51Z</published>
    <updated>2011-06-12T20:03:13Z</updated>

    <summary>Some of you may have heard of Bitcoin before. This would be true if you have been following the lulsec hack of the NHS. Lulsec accept bitcoin as a donation method. There have also been some concern from the US...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="bitcoineconomics" label="bitcoin economics" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>Some of you may have heard of <a href="http://en.wikipedia.org/wiki/Bitcoin">Bitcoin</a> before.  This would be true if you have been following the <a href="http://www.zdnet.co.uk/blogs/security-bullet-in-10000166/lulzsec-accesses-nhs-admin-passwords-10022699/">lulsec hack of the NHS</a>.  Lulsec accept bitcoin as a donation method.  There have also been some concern from the <a href="http://news.cnet.com/8301-13506_3-20070268-17/senators-target-bitcoin-currency-citing-drug-sales/">US Senate about bitcoin</a>.</p>

<p>So, please read the wikipedia article above for a quick intro to bitcoin before you read any further.</p>

<p>I am interested in bitcoin because I like the anonymous and p2p nature of the service.  While I would describe myself as a &#8220;socialist&#8221;, I still believe that there are some things that are not the government&#8217;s business and sometimes I just want to be left alone to do what I want without interference in the form of government.  Another incentive for me is the cryptographic nerd in me loves the idea that you can have a completely anonymous currency using computers to do it.</p>

<p>For my analysis below please understand that I will be using &#8220;normal&#8221; logic rather than &#8220;economic&#8221; logic (also known as &#8220;voodo logic&#8221;).  I am not an economist and I do not trust them very much given their past track record in the prediction business.</p>

<p>So, what gives a bitcoin <em>value</em>?  This is a rather fraught question to start with and really should be the heart of economic philosophy.  In Neo-classical economics, something has value because someone will put a price on a good or service.  This neatly dodges the question.  Marx goes into the question in the first chapter of Capital.  His theory is that value is created when a raw material is turned into something useful by labour.  This makes sense even if Marx&#8217;s prose style is as dense as lead.  In terms of bitcoins, because they are generated by CPU/GPU mining, a bitcoin has value in the electricity spent in generating the bitcoin.  In other words, it is based on carbon emissions (unless your computer is run using renewable or non-carbon electricity generation).</p>

<p>Now, I just want to inject here another idea.  <em>All currencies are valueless</em>.  To mangle Nietzsche, &#8220;if you stare into money (the abyss), money (the abyss) will stare back into you&#8221;.  Currencies only have value because you are required to pay your taxes in them.  Otherwise, they are nice colorful bits of paper with dead white guys on them.  They don&#8217;t mean anything and will never have extrinsic meaning.  Bitcoin is the same.</p>

<p>With the theory of a bitcoin&#8217;s value out of the way very neatly using Marx (thanks dude). We can now turn to the criticisms of bitcoin.  The first of which is bitcoin&#8217;s deflationary nature.  There are only 26 million bitcoins that can be found.  This gives an upper boundary on the size of the market.  However, since you can divide a bitcoin up to eight decimal points, the correct upper boundary is 26,000,000 x 10^8 number of units.  This is still a hard limit.  </p>

<p>Now, why would this be a problem?  First, we must think about inflation.  Inflation is where a nominal currency becomes less valuable over time due to the entrance into the market of more of that currency.  Most central banks try to keep inflation at about 2%.  Why is inflation useful?  What happens when you know that your £1 will be worth slightly less tomorrow than it is worth today?  You will be encouraged to either invest that to beat inflation (and if you don&#8217;t beat inflation, you will be losing money in the long run) or spend that money.  This keeps the economy going because then you will have what economists call the &#8220;velocity of money&#8221; (Thanks again to Marx).  If the &#8220;velocity of money&#8221; ever reaches 0, it is like hitting 0 kelvin, we don&#8217;t know what will happen but your economy <em>will not work</em>.</p>

<p>Now, deflation is the exact opposite of inflation.  This means that my £1 will be worth more tomorrow than it is today.  This will discourage you from investing or purchasing anything other than necessities.  This also impacts debt but that is a thorny subject that I will avoid for this post.  Therefore, that bitcoin is deflationary is a sight problem because the market will never expand enough to cover what a normal currency would as it has hard limits to the number of bitcoins in existence.</p>

<p>A way of thinking around the deflation of bitcoin is to understand that this is a niche currency thus the market will never reach a stage where the deflationary aspects of the currency overtake the inflationary needs of huge advanced economies.</p>

<p>I have a few minor suggestions that might make bitcoin scale indefinitely but they may have their own problems.  First, build in a 2% inflation rate into the mining aspect of the currency.  This means that people will be encouraged to spend/invest/lend bitcoins.  2% is a modest number for an advanced system.  It means that hyperinflation will never happen but you can still do useful work and &#8220;get ahead&#8221; of inflation.  Second,  the anonymous nature of the currency causes many in power to fear it because they cannot control it.  In the end, are we not free to do what we please with ourselves?  I know it is a trite idea these days but the government needs to quit fearing and causing fear.  We are adults, as long as we are not doing harm to others or ourselves, what is the problem?  If bitcoin is to survive we need to have a reasoned debate about the usefulness of alternative currencies.  I doubt that this will happen in the current political climate.</p>

<p>I think bitcoin is cool and neat and a way of building up a store of value that has nothing to do government.  It is also a neat implementation of cryptography.</p>

<p>Also, this is <em>not</em> investment advise.  This is all at your own risk.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Arxiv.org Peer Review and the Humanities Journal</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2011/01/arxivorg-peer-review-and-the-humanities-journal.html" />
    <id>tag:blogs.perl.org,2011:/users/cyocum//522.1323</id>

    <published>2011-01-08T14:40:46Z</published>
    <updated>2011-01-08T15:10:00Z</updated>

    <summary>Traditionally, the humanities journal has relied on Peer Review much like many academic disciplines. I believe firmly that Arxiv.org or something very much like it will replace the humanities journal in the near future, much like it has in the...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="humanitiespeerreviewarxivorgjournal" label="humanities &quot;peer review&quot; &quot;arxiv.org&quot; journal" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>Traditionally, the humanities journal has relied on <a href="http://en.wikipedia.org/wiki/Peer_review">Peer Review</a> much like many academic disciplines.  I believe firmly that <a href="http://www.arxiv.org">Arxiv.org</a> or something very much like it will replace the humanities journal in the near future, much like it has in the sciences.  One of the main objections to this direction in the humanities is &#8220;what about peer review?&#8221;  This is a major sticking point.  The other is &#8220;will it count on the <a href="http://www.rae.ac.uk/">RAE</a>/whatever management <a href="http://www.nybooks.com/articles/archives/2011/jan/13/grim-threat-british-universities/">insanity</a> we have to deal with at the moment?&#8221;  I cannot answer the latter but I will give a vision of a solution for the former.</p>

<p>Peer Review is anonymous.  This allows for people to be straight-forward in their assessment of a piece of writing.  Errors both large and small can be brought to the author&#8217;s attention and fixed.  How would one go about implementing a system to deal with this?  First, Peer Review is usually done <em>gratis</em> so people could sign up to be in a pool of Peer Reviewers.  On an Arxiv.org type site, this might show up in your &#8220;profile&#8221;.  When an author submits a work, it is marked as &#8220;not peer reviewed&#8221; but it will show up in searches, etc.  The author then submits the article for Peer Review.  The system then randomly picks two Peer Reviewers to review the work.  They then read and vote on the work.  If they give &#8220;two thumbs up&#8221;, the article will be marked.  If they give it &#8220;two thumbs up with changes&#8221;, the changes are given to the author who then makes (or not) the changes and it is resubmitted to the reviewers who give the final &#8220;up or down vote&#8221;.  In the case of a tie in the voting, another Peer Reviewer is randomly selected and they look over the case for and against, the tie-breaker then does his job.  </p>

<p>As you can see, this can all be automated.  All you need is people willing to do the Peer Review thing.  Also, who nominates people for Peer Review?  That is a political question really but I would suggest that you use people who have credentials (PhD or above) from accredited universities.  Yes, there will be some cranks but this will be evened out by the possibility of the third tie-break reviewers.</p>

<p>In the large, a user could watch the &#8220;un-peer reviewed&#8221; articles stream to look for new ideas and but only reference those which are peer reviewed or something like that.  This would make scholarship so much easier to do rather than having to deal with publishers (who in the end only rent back to us work that was given mostly for free, which is a monumentally insane way of doing things) and <a href="www.jstor.org">Jstor</a>.</p>
]]>
        

    </content>
</entry>

<entry>
    <title>Currying</title>
    <link rel="alternate" type="text/html" href="http://blogs.perl.org/users/cyocum/2010/12/currying-or-how-to-make-life-easier-for-yourself.html" />
    <id>tag:blogs.perl.org,2010:/users/cyocum//522.1241</id>

    <published>2010-12-13T09:13:56Z</published>
    <updated>2010-12-13T09:48:59Z</updated>

    <summary>Until recently, while I thought currying was kind of cool, I did not really understand what the use of it was. I call a function and I am done with it. It was not until I was cleaning up some...</summary>
    <author>
        <name>cyocum</name>
        <uri>http://homepages.inf.ed.ac.uk/v1cyocum</uri>
    </author>
    
    <category term="ocamlcurryingfunctions" label="ocaml currying functions" scheme="http://www.sixapart.com/ns/types#tag" />
    
    <content type="html" xml:lang="en" xml:base="http://blogs.perl.org/users/cyocum/">
        <![CDATA[<p>Until recently, while I thought <a href="http://en.wikipedia.org/wiki/Currying">currying</a> was kind of cool, I did not really understand what the use of it was.  I call a function and I am done with it.  It was not until I was cleaning up some code in my <a href="https://github.com/cyocum/Football-Predictor">football predictor</a> project that I started to understand how currying could make your life easier.</p>

<p>Let us say that you have a function that takes an input and based on that input does some calculations.  Now, this is normal in Perl.  For instance,</p>

<pre><code>sub foo {
    my ($choice) = @_;
    my @float_lst;
    my $total;

    if($choice eq "win") {
        @float_lst = map { run_prediction($_) } (1..10);
        $total += $_ for @float_lst;
    } elsif($choice eq "lose")
        @float_lst = map { run_prediction($_) } (-1..-10);
        $total += $_ for @float_lst;
    } elsif($choice eq "draw") {
        @float_lst = map { run_prediction($_) } (0);
        $total += $_ for @float_lst;
    }
    return $total;
}
</code></pre>

<p>You probably notice that you repeat a rather long line of statements.  In Ocaml, you can use currying to cut down on those (I am sure there are ways of doing this in Perl as well).</p>

<pre><code> type outcome = Win | Lose | Draw

 let wins = [1;2;3;4;5;6;7;8;9;10]
 let draw = [0]
 let lose = [-1;-2;-3;-4;-5;-6;-7;-8;-9;-10]

 let calc_outcome outcome =
     let calc_outcome_aux = BatList.map run_prediction in
     let fold_float = List.fold_left (+.) 0. in
         match outcome with
             | Win -&gt;
                 fold_float (calc_outcome_aux wins)
             | Lose -&gt;
                 fold_float (calc_outcome_aux lose)
             | Draw -&gt;
                 fold_float (calc_outcome_aux draw)
</code></pre>

<p>Notice how I have called BatList.map and List.fold_left without the required number of arguments.  This is not an &#8220;off by one error&#8221;.  What the compiler does is break down the function into a set of single argument functions that it then stores the next in the chain in the lexical bindings.  Once you have fulfilled the required number of arguments, it will return its answer.  Of course, you can imagine using map over a function that has more than one argument that returns a list of functions that have the other required arguments left to fulfill.  In addition, you can call functions where you know one argument will take a long time to compute so you can do the quick arguments first then compute the heavy argument.</p>

<p>In this case, I was able to cut down the length of a line and repeated statements and reuse them based on user input.</p>
]]>
        

    </content>
</entry>

</feed>
