<?xml version="1.0" encoding="utf8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Chris Done's Blog</title>
        <link>http://chrisdone.com</link>
        <description>Chris Done's blog posts, programming, Haskell, Lisp, Linux, etc.</description>
        <atom:link href="http://chrisdone.com/rss.xml" rel="self"
                   type="application/rss+xml" />
        <lastBuildDate>Wed, 11 Aug 2010 00:00:00 UT</lastBuildDate>
        <item>
    <title>Haskell mode Emacs extensions</title>
    <link>http://chrisdone.com/posts/2010-08-11-haskell-mode-exts.html</link>
    <description><p
>I have made <a href="http://github.com/chrisdone/haskell-mode-exts"
  >a github project for a package of Emacs modules for extending Haskell mode.</a
  > Some initial modules so far:</p
><ol style="list-style-type: decimal;"
><li
  >haskell-align-imports.el</li
  ><li
  >haskell-installed-packages.el</li
  ><li
  >haskell-navigate-imports.el</li
  ><li
  >haskell-sort-imports.el</li
  ><li
  >inf-haskell-send-cmd.el</li
  ></ol
><p
>I have more in progress and more planned.</p
><p
>One example is <a href="http://www.youtube.com/watch?v=UXHSiUPKLvg&amp;fmt=35"
  >making editing imports automatically alphabetically ordered and indented.</a
  > This one is a little tricky to get right so I will get back to you on this one. Once I am happy with it, I will generalize it to a lot of Haskell statements, like <code
  >data</code
  > declarations:</p
><pre
><code
  >data Foo = X
         | Y
</code
  ></pre
><p
>If I type <code
  >bar</code
  > after <code
  >Foo</code
  >, I should get,</p
><pre
><code
  >data Foobar = X
            | Y
</code
  ></pre
><p
>automatically. I have seen this type of thing in <a href="http://haskell.org/haskellwiki/Yi"
  >the Yi editor</a
  > and it is a really good idea. Equally for definitions:</p
><pre
><code
  >foo x = do x
           y
</code
  ></pre
><p
>If I add another parameter, the rest of the function body should be indented accordingly,</p
><pre
><code
  >foo x y = do x
             y
</code
  ></pre
><p
>and so on.</p
></description>
    <pubDate>Wed, 11 Aug 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-08-11-haskell-mode-exts.html</guid>
</item>
<item>
    <title>Getting all installed Haskell packages/modules from Emacs</title>
    <link>http://chrisdone.com/posts/2010-08-07-emacs-haskell-installed-packages.html</link>
    <description><p
>I wrote a self-contained Emacs module for retrieving all the installed Haskell package descriptions (as an association list), and the exposed modules inside them.</p
><ul
><li
  ><a href="http://gist.github.com/511670"
    >Haskell installed packages</a
    ></li
  ></ul
><p
>This is intended for Elisp programmers to use to make import handling and package handling easier.</p
><p
>For example, if I run <code
  >(haskell-installed-packages)</code
  >, I get:</p
><pre
><code
  >(&quot;Cabal-1.8.0.2&quot; &quot;array-0.3.0.0&quot; &quot;base-3.0.3.2&quot; &quot;base-4.2.0.0&quot;
&quot;bin-package-db-0.0.0.0&quot; &quot;bytestring-0.9.1.5&quot;
&quot;containers-0.3.0.0&quot; &quot;directory-1.0.1.0&quot; &quot;extensi\
ble-exceptions-0.1.1.1&quot; &quot;ffi-1.0&quot; &quot;filepath-1.1.0.3&quot;
&quot;ghc-6.12.1&quot; ...)
</code
  ></pre
><p
>I can also run the following:</p
><pre
><code
  >(haskell-read-pkg-description 
  (haskell-ghc-pkg-command &quot;describe Cabal-1.8.0.2&quot;))
</code
  ></pre
><p
>And the following is returned:</p
><pre
><code
  >((&quot;name&quot; . &quot;Cabal&quot;) (&quot;version&quot; . &quot;1.8.0.2&quot;) (&quot;id&quot;
. &quot;Cabal-1.8.0.2-bc92fe595a99db06fca8c2eb712108b4&quot;)
(&quot;license&quot; . &quot;BSD3&quot;) (&quot;copyright&quot; . &quot;2003-2006, Isaac Jones
2005-2009, Duncan Coutts&quot;) (&quot;maintainer&quot;
. &quot;cabal-devel@haskell.org&quot;) (&quot;stability:&quot; . &quot;&quot;) (&quot;homepage&quot;
. &quot;http://www.haskell.org/cabal/&quot;) (&quot;package-url:&quot; . &quot;&quot;)
(&quot;description&quot; . &quot;The Haskell Common Architecture for
Building Applications and Libraries: a framework defining a
common interface for authors to more easily build their
Haskell applications in a portable way. . The Haskell Cabal
is part of a larger infrastructure for distributing,
organizing, and cataloging Haskell libraries and tools.&quot;)
(&quot;category&quot; . &quot;Distribution&quot;) (&quot;author&quot; . &quot;Isaac Jones
&lt;ijones@syntaxpolice.org&gt; Duncan Coutts
&lt;duncan@haskell.org&gt;&quot;) ...)
</code
  ></pre
><p
>Finally I acquire a complete list of all modules installed with <code
  >(haskell-installed-modules)</code
  >, which is defined in terms of the above two functions.</p
><pre
><code
  >(&quot;Distribution.Compiler&quot; &quot;Distribution.InstalledPackageInfo&quot;
&quot;Distribution.License&quot; &quot;Distribution.Make&quot;
&quot;Distribution.ModuleName&quot; &quot;Distribution.Package&quot;
&quot;Distribution.PackageDescription&quot;
&quot;Distribution.PackageDescription.Configuration&quot;
&quot;Distribution.PackageDescription.Parse&quot;
&quot;Distribution.PackageDescription.Check&quot; &quot;Distribution.ParseUtils&quot;
&quot;Distribution.ReadE&quot; ...)
</code
  ></pre
><p
>These functions cache automatically. You can force them to refresh the cache by passing <code
  >t</code
  > to them. Or run the following function.</p
><p
>In your .emacs start-up file, put:</p
><pre
><code
  >(haskell-installed-packages-refresh-all)
</code
  ></pre
><p
>Which will refresh all packages and modules. It takes about 3 seconds on my machine. But I am okay with that because I restart Emacs rarely. You do not have to put it here, but it saves waiting for it to update when you are mid-way through some action.</p
><p
>In the future it is completely possible to speed this up by checking the last modified date on the Haskell package databases, and to only update the changed packages.</p
><p
>For my use case, I am going to use it for smex or anything.el-like completion and write another module with clever import insertion so that one can navigate between import statement parts with arrow keys. What can I say?; I really do not like typing out import statements.</p
><p
>For Emacs newbies, if you are impatient and want to use this right away, pop this in your .emacs file:</p
><pre
><code
  >(require 'haskell-installed-packages)
(setq haskell-ghc-pkg-bin-path &quot;/home/chris/Programs/bin/ghc-pkg&quot;)
(haskell-installed-packages-refresh-all)
</code
  ></pre
><p
>You can omit the <code
  >setq</code
  > line if it works without it; otherwise change it to wherever your ghc-pkg binary lies. Now a very simple shortcut for completing Haskell modules:</p
><pre
><code
  >(defun haskell-complete-module ()
  &quot;Insert a completed Haskell module.&quot;
  (interactive)
  (insert (ido-completing-read &quot;Module: &quot;
                               (haskell-installed-modules))))
</code
  ></pre
><p
>Then bind it to a key:</p
><pre
><code
  >(define-key haskell-mode-map (kbd &quot;C-c i&quot;) 'haskell-complete-module)
</code
  ></pre
><p
>Or whatever you prefer.</p
></description>
    <pubDate>Sat, 07 Aug 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-08-07-emacs-haskell-installed-packages.html</guid>
</item>
<item>
    <title>Navigating to/from Haskell import lists in Emacs</title>
    <link>http://chrisdone.com/posts/2010-08-06-haskell-navigate-modules.html</link>
    <description><p
>I wrote a self-contained Emacs module for navigating to and from the import list in Haskell files.</p
><ul
><li
  ><a href="http://gist.github.com/511240"
    >Haskell modules navigator</a
    ></li
  ></ul
><p
>The use case is, for example, two sets of module lists:</p
><pre
><code
  >import           Control.Applicative                   ((&lt;$&gt;))
import           Data.Bool.Higher                      (bool)
import           Data.List                             (sort,intercalate)
import           Data.Maybe                            (catMaybes)
import           System.Process                        (readProcess)
import           System.Environment                    (getArgs)

import qualified Distribution.PackageDescription.Parse as P
import qualified Distribution.PackageDescription       as PD
import qualified Distribution.ModuleName               as PM
import           Text.Regex.Posix                      ((=~))
</code
  ></pre
><p
>And I am half way down the file, and realise I need to import <code
  >Control.Concurrent</code
  >. So I hit <code
  >F8</code
  >, which takes the cursor to the <code
  >Control.Applicative</code
  > line. I enter my import statement, and then hit <code
  >C-u F8</code
  > to return back to the middle of the file to what I was doing.</p
></description>
    <pubDate>Fri, 06 Aug 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-08-06-haskell-navigate-modules.html</guid>
</item>
<item>
    <title>Haskell align imports</title>
    <link>http://chrisdone.com/posts/2010-07-31-haskell-align-imports.html</link>
    <description><p
>I made a little Elisp script which aligns your import lists in your Haskell code. I pasted it to Gist:</p
><ul
><li
  ><a href="http://gist.github.com/453933"
    >Haskell align imports for Emacs</a
    ></li
  ><li
  ><a href="http://gist.github.com/454255"
    >Haskell align imports for Vim</a
    ></li
  ></ul
><p
>mauke from the Freenode IRC contributed an equivalent vim plugin.</p
></description>
    <pubDate>Sat, 31 Jul 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-07-31-haskell-align-imports.html</guid>
</item>
<item>
    <title>Uploading Hackage Packages</title>
    <link>http://chrisdone.com/posts/2010-07-13-uploading-hackage-packages.html</link>
    <description><p
>I found two good Haskell libraries recently and realised they were not packaged up and put on Hackage! So I updated them to the latest base and GHC extensions, made a proper Cabal package out of them and uploaded them:</p
><ul
><li
  ><a href="http://hackage.haskell.org/package/frisby"
    >frisby: Linear time composable parser for PEG grammars.</a
    ></li
  ><li
  ><a href="http://hackage.haskell.org/package/pappy"
    >pappy: Packrat parsing; linear-time parsers for grammars in TDPL.</a
    ></li
  ></ul
><p
>If you have interest in being the maintainer of these packages, please let me know.</p
><p
>Lots of people had not even heard of these two packages above before I uploaded them to Hackage. So I am gonna go out on a limb and say packaging up substantial code bases and putting them on Hackage should be standard practice in the Haskell community.</p
></description>
    <pubDate>Tue, 13 Jul 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-07-13-uploading-hackage-packages.html</guid>
</item>
<item>
    <title>The Haskell module landscape</title>
    <link>http://chrisdone.com/posts/2010-07-04-haskell-module-landscape.html</link>
    <description><p
>I was pondering how naming Haskell packages with a proper hierarchy is quite important, and wondered what the general hierarchy of the Hackage packages was like. So I grabbed the <a href="http://hackage.haskell.org/packages/archive/00-index.tar.gz"
  >tarball of pakage descriptions</a
  > from <a href="http://hackage.haskell.org/packages/hackage.html"
  >Hackage</a
  > and ran <a href="http://gist.github.com/463472"
  >a little messy Haskell script</a
  > and got <a href="http://gist.github.com/raw/463423/f8458d83b1a7cc26cdbf812747188993e50cd8a2/The%20Haskell%20module%20landscape"
  >this list.</a
  ></p
><p
>Here is a snippet:</p
><pre
><code
  >hsdns:          ADNS
hsdns:          ADNS.Base
hsdns:          ADNS.Endian
hsdns:          ADNS.Resolver
cv-combinators: AI.CV.ImageProcessors
HOpenCV:        AI.CV.OpenCV.CV
HOpenCV:        AI.CV.OpenCV.CxCore
HOpenCV:        AI.CV.OpenCV.HighGui
</code
  ></pre
><p
>It is interesting to see how there are quite a few packages that could be easily put into the proper namesace, e.g ADNS below should be in the Network hierarchy. It is also cool to see clusters of separate packages providing functionality for the same module, e.g check out Control.Applicative!</p
><pre
><code
  >special-functors:                 Control.Applicative
base:                             Control.Applicative
applicative-extras:               Control.Applicative.Backwards
applicative-extras:               Control.Applicative.Compose
applicative-extras:               Control.Applicative.Error
InfixApplicative:                 Control.Applicative.Infix
category-extras:                  Control.Applicative.Parameterized
action-permutations:              Control.Applicative.Permutation
applicative-extras:               Control.Applicative.State
yjtools:                          Control.Applicative.Tools
unicode-symbols:                  Control.Applicative.Unicode
base-unicode-symbols:             Control.Applicative.Unicode
</code
  ></pre
><p
>Control.Concurrent also has a lot going on inside it. There are quite a lot of packages under Network, too.</p
><p
>One idea: Hackage could have an additional page which displays this, fully linked up to the packages in question, with an MSDN-style complete collapsible/expandable module hierarchy menu on the left and documentation on the right. I am not saying that is the right way to do it, but it certainly makes for fun browsing for the Haskell entire open source codebase. (It would also encourage library writers to put their libraries in a useful namespace.)</p
></description>
    <pubDate>Sun, 04 Jul 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-07-04-haskell-module-landscape.html</guid>
</item>
<item>
    <title>Try Haskell Updates</title>
    <link>http://chrisdone.com/posts/2010-06-13-try-haskell-updates.html</link>
    <description><p
>In the past month I moved to Italy for a new Haskell job. Hopefully, we will be releasing some open source contributions.</p
><p
>I added <a href="http://github.com/chrisdone/tryhaskell/commits/master/"
  >about 25 more steps to Try Haskell and touched on types in Haskell, clickable code samples</a
  >, lessons selection, go-back support, stricter expression checking, from the help of feedback from <a href="http://news.ycombinator.com/item?id=1393593"
  >YCombinator</a
  >, including <a href="http://simondavidpratt.com/"
  >Simon Pratt</a
  >, who provided <a href="http://github.com/chrisdone/jquery-console/commits/master/"
  >various patches to the jquery console</a
  > with refactoring, fixing the history and adding standard console shortcuts.</p
><p
>I <a href="http://github.com/chrisdone/haskell-json/commits/master/"
  >tidied up the JSON service code</a
  >, removed redundant files, tided up module imports, made the mueval-core path not hard coded, etc. It is currently written with the FastCGI API. I have started a branch with the aim of switching it to <a href="http://snapframework.com/"
  >Snap</a
  >.</p
><p
>I have also added top-level binding support, e.g.:</p
><pre
><code
  >&gt; x = 1
&gt; (y,z) = (5,6)
&gt; x * y * z
=&gt; 30
&gt;
</code
  ></pre
><p
>It <a href="http://github.com/chrisdone/haskell-json/commit/ee87544698759eadbb90a911b78343bbde8531a6"
  >works mostly in the same way GHCi does, with nested LET expressions</a
  >. I think this way is sound. The bindings are per-session; they expire when you close your browser.</p
><p
>There is one change to the JSON service, when evaluating a top-level expression, the success return value is:</p
><pre
><code
  >{&quot;bind&quot;:&quot;OK.&quot;}
</code
  ></pre
><p
>Failure is returned as normal, e.g.</p
><pre
><code
  >{&quot;error&quot;:&quot;Not in scope: `y&quot;}
</code
  ></pre
><p
>Another neat thing I added as <a href="http://news.ycombinator.com/item?id=1393735"
  >suggested by tumult on YCombinator</a
  > is support to link to a given expression, e.g.:</p
><pre
><code
  >&gt; map (*2) [1..1 0]
=&gt; [2,4,6,8,10,12,14,16,18,20]
&gt; link
link for map (*2) [1..10]
&gt;  
</code
  ></pre
><p
>(The <a href="http://tryhaskell.org/?input=%6d%61%70%20%28%2a%32%29%20%5b%31%2e%2e%31%30%5d"
  ><code
    >link for map (*2) [1..10]</code
    ></a
  > text is a link.)</p
><p
>I also <a href="http://tryhaskell.org/?input=circle%2020%2020%2020"
  >re-enabled Raphael support</a
  > for the hell of it.</p
><p
>I have more plans for it such as embedded code editor and smallcheck exercises, user-uploaded tutorials, and adding an embedded console to Hackage at some point would be nice, but that is all for now.</p
><p
>In other news, two other languages have started their own Try <em
  >Lang</em
  > sites! Check these out:</p
><ul
><li
  ><a href="http://www.try-clojure.org/"
    >Try Clojure</a
    ></li
  ><li
  ><a href="http://www.tryerlang.org/"
    >Try Erlang</a
    ></li
  ></ul
><p
>Raising awareness of all functional languages is a great thing!</p
></description>
    <pubDate>Sun, 13 Jun 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-06-13-try-haskell-updates.html</guid>
</item>
<item>
    <title>Yay! hpaste bot is back</title>
    <link>http://chrisdone.com/posts/2010-04-11-hpaste-bot.html</link>
    <description><p
>The <a href="http://hpaste.org/"
  >hpaste</a
  > site used to join the #haskell IRC channel and sit there waiting for pastes. When someone made a paste it would announce them in the channel with a link. Half a year ago (or longer, I guess) hpaste was rewritten and hpaste2 was born. Unfortunately, the IRC announcer failed to work on the server it runs on.</p
><p
>It’s a pretty convenient feature and it encourages people to paste and share the code they’re having problems with and the code they find interesting and want to share. If hpaste sends the link for you, it’s not an active thing on your part.</p
><p
>I figured it would be a good idea to use the <a href="http://hackage.haskell.org/package/rss2irc"
  >rss2irc program on Hackage</a
  >, which, I believe, is what the Hackage IRC announcer uses. E.g.</p
><pre
><code
  >* hackagebot hashed-storage 0.4.11 - Hashed file storage support code.                            
  http://hackage.haskell.org/package/hashed-storage-0.4.11 (EricKow)
</code
  ></pre
><p
>I read the hpaste source and it has some code for generating an RSS feed but it’s all commented out. So I wrote <a href="http://github.com/chrisdone/hpaste-feed"
  >a little program to periodically fetch the main page of hpaste and generate an RSS feed from it</a
  > and write it to <a href="http://tryhaskell.org/hpaste.rss"
  >a file in my web server directory.</a
  > I’ve been thinking of doing this for more than half a year, but thought someone’ll sort it out.. never did. So here we go.</p
><p
>I run it like this:</p
><pre
><code
  >hpaste-feed /var/www/tryhaskell.org/hpaste.rss 30
</code
  ></pre
><p
>And then just hook-up rss2irc with it:</p
><pre
><code
  >rss2irc http://tryhaskell.org/hpaste.rss \
'hpaste@irc.freenode.net/#hpaste-test' \
-i 1 -l --author &amp; disown
</code
  ></pre
><p
>I make a paste, and within a couple seconds, tada!</p
><pre
><code
  >&lt;hpaste&gt;  a test paste (chrisdone) http://hpaste.org/fastcgi/hpaste.fcgi/view?id=24822
</code
  ></pre
><p
>I’m running it on the tryhaskell server so hopefully it should be stable 24/7. Only problem is hpaste.org going down, but can’t be avoided. I’ll need to see what errors are thrown with the <a href="http://hackage.haskell.org/package/HTTP"
  >Network.HTTP</a
  > library when the request times out or fails.</p
></description>
    <pubDate>Sun, 11 Apr 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-04-11-hpaste-bot.html</guid>
</item>
<item>
    <title>A bit of cabal-install hacking</title>
    <link>http://chrisdone.com/posts/2010-04-09-cabal-install-hacking.html</link>
    <description><p
><strong
  >EDIT:</strong
  > It appears cabal-install already supports bash completion for commands, provided by a script in the bash-completion directory of the cabal-install package. It also supports flag completion. Cool!</p
><p
>A couple other people at Zurihac agreed that it would be nice if Cabal (cabal-install) supported completion like <code
  >git</code
  >. You can write <code
  >git co</code
  > instead of <code
  >git commit</code
  >, etc. saving a bit of typing in the late hacking hours. I made <a href="http://github.com/chrisdone/cabal-install/commit/cf74c1e9136b53ac198bc6915abd57cb9972ec2c"
  >a wee patch for Cabal 0.6.4 to do this.</a
  > Here’s me building cabal-install with cabal-install.</p
><pre
><code
  >chris@chrisamilo:~/Haskell/cabal-install-0.6.4$ cabal co;cabal b;cabal i
Resolving dependencies...
Configuring cabal-install-0.6.4...
Preprocessing executables for cabal-install-0.6.4...
Building cabal-install-0.6.4...
Resolving dependencies...
Configuring cabal-install-0.6.4...
Preprocessing executables for cabal-install-0.6.4...
Building cabal-install-0.6.4...
Installing executable(s) in /home/chris/.cabal/bin
</code
  ></pre
><p
>It’s pretty nice. I guess it’s probably preferable to write some personal scripts to run on top of cabal but it’d be nice if everyone had it.</p
><p
>As with most Haskell projects the source code is really easy to patch so I’ll probably do more cabal-install hacking.</p
></description>
    <pubDate>Fri, 09 Apr 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-04-09-cabal-install-hacking.html</guid>
</item>
<item>
    <title>A Haskell JSON service and updated Try Haskell!</title>
    <link>http://chrisdone.com/posts/2010-04-05-haskell-json-service-tryhaskell.html</link>
    <description><p
><strong
  >UPDATE:</strong
  > The service went down 5 April at about 19:50 GMT+1 to about 20:21. I had to update the watchdog to be more aggressive for Linode (as opposed to my development machine). Just letting you guys know.</p
><div id="make-your-own-try-haskell"
><h2
  >Make your own Try Haskell!</h2
  ><p
  >You can now run <a href="http://tryhaskell.org/"
    >Try Haskell!</a
    > as a simple HTML file locally, or on your own domain, if you like! All you need is an internet connection.</p
  ><p
  >Grab the source with git (or download a <a href="http://github.com/chrisdone/tryhaskell/zipball/master"
    >.zip</a
    > or <a href="http://github.com/chrisdone/tryhaskell/tarball/master"
    >.tar.gz</a
    > of the latest version):</p
  ><pre
  ><code
    >$ git clone git://github.com/chrisdone/tryhaskell.git
</code
    ></pre
  ><p
  >Get writing interactive tutorials! Please let me know how you get on.</p
  ></div
><div id="evaluate-haskell-code-anywhere"
><h2
  >Evaluate Haskell code anywhere</h2
  ><p
  >You can now also use the JSONP service that Try Haskell uses from anywhere!</p
  ><p
  >It is located at: <code
    >http://tryhaskell.org/haskell.json</code
    ></p
  ><p
  >And you can download <a href="http://github.com/chrisdone/haskell-json"
    >the haskell-json source from Github</a
    >:</p
  ><pre
  ><code
    >$ git clone git://github.com/chrisdone/haskell-json.git
</code
    ></pre
  ><p
  >Here is a simple Haskell evaluating text box. <a href="/code/haskell-json-example.zip"
    >Download the source.</a
    ></p
  ><form action="" id="evaluator">
     <fieldset>
       <legend>Haskell evaluation</legend> <label>Expression:</label> <input type="text" id="expr" /> <input type="submit" value="Evaluate" /><p>Result: <code id="eval-output">Output here.</code></p>
       <p>Type: <code id="eval-type">Type here.</code></p>
     </fieldset>
   </form>
<form action="" id="loader">
     <fieldset>
       <legend>Haskell loading</legend> <label>Code:</label> <textarea id="contents"></textarea> <input type="submit" value="Load code" /><p>Result: <code id="load-output">Output here.</code></p>
     </fieldset>
   </form>
<script type="text/javascript" src="/code/jquery-1.4.2.min.js"></script>
   <script type="text/javascript" src="/code/haskell-json/encode-hex.js"></script>
   <script type="text/javascript" src="/code/haskell-json/haskell-json.js"></script>
<p
  >Feel free to make this pretty and use it in your blog or books, Haskell courses, etc. Trying Haskell should be easy!</p
  ></div
><div id="methods-and-options"
><h2
  >Methods and options</h2
  ><div id="eval-method"
  ><h3
    ><code
      >eval</code
      > method</h3
    ><p
    >There is an <strong
      >eval</strong
      > method for evaluating expressions, used like this:</p
    ><pre
    ><code
      >http://tryhaskell.org/haskell.json?method=eval&amp;expr=23*52
</code
      ></pre
    ><p
    >and you get back:</p
    ><pre
    ><code
      >{&quot;result&quot;:&quot;1196&quot;,&quot;type&quot;:&quot;(Num t) =&gt; t&quot;,&quot;expr&quot;:&quot;23*52&quot;}
</code
      ></pre
    ><p
    >Note that computations are limited to 750ms. This limit may be softened if people have problems doing useful things.</p
    ></div
  ><div id="load-method"
  ><h3
    ><code
      >load</code
      > method</h3
    ><p
    >And there is a <strong
      >load</strong
      > method for loading your own file into the service for later evaluation, used like this:</p
    ><pre
    ><code
      >http://tryhaskell.org/haskell.json?method=load&amp;contents=x=1
</code
      ></pre
    ><p
    >and you get back:</p
    ><pre
    ><code
      >{&quot;success&quot;:&quot;wrote file.&quot;}
</code
      ></pre
    ><p
    >Then you can request</p
    ><pre
    ><code
      >http://tryhaskell.org/haskell.json?method=eval&amp;expr=x
</code
      ></pre
    ><p
    >and get back:</p
    ><pre
    ><code
      >{&quot;result&quot;:&quot;5&quot;,&quot;type&quot;:&quot;Integer&quot;,&quot;expr&quot;:&quot;x&quot;}
</code
      ></pre
    ><p
    >Hurrah!</p
    ></div
  ><div id="guid-for-unique-states"
  ><h3
    ><code
      >guid</code
      > for unique states</h3
    ><p
    >If you are writing a tutorial or a blog post, maybe you want your visitors to be able to maintain two different states depending on what post/chapter they have open in what tab. You can do this by adding a guid to your request:</p
    ><pre
    ><code
      >http://tryhaskell.org/haskell.json?method=load&amp;contents=x=1&amp;guid=ch2
</code
      ></pre
    ><p
    >For chapter2, or maybe you could use the ID of your blog post, or whatever you want, with the condition that it is <strong
      >alphanumeric.</strong
      ></p
    ></div
  ><div id="pad-or-jsonp"
  ><h3
    ><code
      >pad</code
      > or JSONP</h3
    ><p
    >If you are making your request from JavaScript, you will need to follow the JSON style of making requests. <code
      >haskell.json</code
      > supports this by adding a <code
      >pad</code
      > parameter:</p
    ><pre
    ><code
      >http://tryhaskell.org/haskell.json?method=eval&amp;expr=Just%201&amp;pad=doStuff
</code
      ></pre
    ><p
    >and you get back:</p
    ><pre
    ><code
      >doStuff({&quot;result&quot;:&quot;Just 1&quot;,&quot;type&quot;:&quot;(Num t) =&gt; Maybe t&quot;,&quot;expr&quot;:&quot;Just 1&quot;})
</code
      ></pre
    ></div
  ></div
><div id="implementing-simple-jsonp-in-javascript"
><h2
  >Implementing simple JSONP in JavaScript</h2
  ><p
  >I use jQuery in these examples, because it is really easy.</p
  ><p
  >So for example I wrote a little function for tryhaskell for making JSONP requests:</p
  ><pre
  ><code
    >function jsonp(url,func) {
    handleJSON = function(r){
        script.remove();
        func(r);
    };
    var script = $('&lt;script type=&quot;text/javascript&quot; src=&quot;'+url+'&quot;&gt;&lt;/script&gt;');
    script.attr('src',url);
    $('body').append(script);
}
</code
    ></pre
  ><p
  >And it references a global <code
    >handleJSON</code
    > function that I define at the top level so that any script file could access it:</p
  ><pre
  ><code
    >var handleJSON = function(a){ alert('Unassigned JSONP: ' + a); }
</code
    ></pre
  ><p
  >(Just have a little alert message in case we get something wrong.)</p
  ><p
  >It adds a script tag to the body with the URL we want to query. This is how it gets around the domain restriction of normal AJAX. It is kind of an ugly solution if you ask me, but there you go!</p
  ><p
  >Once the script has been loaded, the global function <code
    >handleJSON</code
    > is called. So before that happens, we need to overwrite it <code
    >handleJSON</code
    > with our closure to remove our <code
    >script</code
    > tag from the DOM and then call the function provided to <code
    >jsonp</code
    >.</p
  ><p
  >Bringing it all together we can write something like this:</p
  ><pre
  ><code
    >jsonp(&quot;http://tryhaskell.org/haskell.json?method=eval&amp;pad=handleJSON&amp;expr=&quot;
       + encodeHex(&quot;34*23&quot;),
      function(resp){
         alert(&quot;Result: &quot; + resp.result);
      });
</code
    ></pre
  ><p
  >Encoding what you provide in the URL is important.</p
  ><p
  >See the sample zip file above for an example of this.</p
  ></div
></description>
    <pubDate>Mon, 05 Apr 2010 00:00:00 UT</pubDate>
    <guid>http://chrisdone.com/posts/2010-04-05-haskell-json-service-tryhaskell.html</guid>
</item>

    </channel> 
</rss>
