<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>friggeri.net &#187; ocaml</title>
	<atom:link href="http://friggeri.net/blog/tag/ocaml/feed" rel="self" type="application/rss+xml" />
	<link>http://friggeri.net/blog</link>
	<description>Adrien Friggeri's blog</description>
	<lastBuildDate>Mon, 14 Dec 2009 21:19:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9-rare</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Ciml : C in OCaml</title>
		<link>http://friggeri.net/blog/2008/02/26/ciml-c-in-ocaml</link>
		<comments>http://friggeri.net/blog/2008/02/26/ciml-c-in-ocaml#comments</comments>
		<pubDate>Tue, 26 Feb 2008 13:27:54 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2008/02/26/ciml-c-in-ocaml</guid>
		<description><![CDATA[Olivier has been busy extending my previous work on inlining C in OCaml, and it is pretty cool. You can get it from the following darcs repository.
darcs get http://chadok.info/darcs/ciml/

Basically, it allows you to inline C in OCaml code, in a very fashionable way. Type conversion between OCaml and C is (almost always) automatically dealt with [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://callcc.chadok.info/">Olivier</a> has been busy extending my previous work on inlining C in <acronym title="Objective Caml">OCaml</acronym>, and it is pretty cool. You can get it from the following darcs repository.</p>
<div class="ch_code_container" style="font-family: monospace;">darcs get http://chadok.info/darcs/ciml/</p>
</div>
<p>Basically, it allows you to inline C in <acronym title="Objective Caml">OCaml</acronym> code, in a very fashionable way. Type conversion between <acronym title="Objective Caml">OCaml</acronym> and C is (almost always) automatically dealt with for base types (int, float, bool, string, etc.) and you can register and unregister custom converters. A few examples are shown in the <code>test.ml</code>.</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">letext</span> hello <span style="color:#8ac6f2;">&#40;</span>s:string<span style="color:#8ac6f2;">&#41;</span> : unit =&nbsp; &lt;&lt; <br />
&nbsp; printf<span style="color:#8ac6f2;">&#40;</span><span style="color:#95e454;">&quot;Hello %s !<span style="color:#e7f6da;">\n</span>&quot;</span>, s<span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; Return<span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&gt;&gt;<br />
<span style="color:#8ac6f2;">letext</span> add <span style="color:#8ac6f2;">&#40;</span>a:int<span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span>b:int<span style="color:#8ac6f2;">&#41;</span> : int =&nbsp; &lt;&lt; <br />
&nbsp; int c;<br />
&nbsp; c = a + b;<br />
&nbsp; Return<span style="color:#8ac6f2;">&#40;</span>c<span style="color:#8ac6f2;">&#41;</span>;<br />
&gt;&gt;</p>
<p><span style="color:#99968b;">(* Unregister the converters for int *)</span><br />
unregister_fromval : int<br />
unregister_toval : int</p>
<p><span style="color:#99968b;">(* Register converters *)</span><br />
register_fromval <span style="color:#95e454;">&quot;Int_val&quot;</span> <span style="color:#95e454;">&quot;int&quot;</span> : int<br />
register_toval <span style="color:#95e454;">&quot;Val_int&quot;</span> : int</p>
</div>
<p>The idea behind all this syntaxic sugar for inlining C code in <acronym title="Objective Caml">OCaml</acronym> is to provide a natural way of using C and <acronym title="Objective Caml">OCaml</acronym> together to whoever wants to bind C libraries to <acronym title="Objective Caml">OCaml</acronym>. Of course, this is still under developement, and any ideas to improve it are welcome.</p>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2008/02/26/ciml-c-in-ocaml/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Inlining C code in OCaml, part II</title>
		<link>http://friggeri.net/blog/2008/02/13/inlining-c-code-in-ocaml-part-ii</link>
		<comments>http://friggeri.net/blog/2008/02/13/inlining-c-code-in-ocaml-part-ii#comments</comments>
		<pubDate>Tue, 12 Feb 2008 23:06:02 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2008/02/13/inline-c-la-suite</guid>
		<description><![CDATA[I finally managed to do what I was talking about this morning. There are a few awful things in the code, but it works.
Here is what one can write:
&#60;:c&#60;
#include &#60;stdio.h&#62;
&#62;&#62;
ext add &#40;a:int&#41; &#40;b:int&#41; : int =&#160; &#60;:cfun&#60; 
&#160; int c;
&#160; c = Int_val&#40;a&#41; + Int_val&#40;b&#41;;
&#160; CAMLreturn&#40;Val_int&#40;c&#41;&#41;;
&#62;&#62;
ext hello &#40;s:string&#41; : unit =&#160; &#60;:cfun&#60; 
&#160; printf&#40;&#34;Hello %s [...]]]></description>
			<content:encoded><![CDATA[<p>I finally managed to do what I was talking about this morning. There are a few awful things in the code, but it works.</p>
<p>Here is what one can write:</p>
<div class="ch_code_container" style="font-family: monospace;">&lt;:c&lt;<br />
#include &lt;stdio.<span style="color:#dedede;">h</span>&gt;<br />
&gt;&gt;</p>
<p>ext add <span style="color:#8ac6f2;">&#40;</span>a:int<span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span>b:int<span style="color:#8ac6f2;">&#41;</span> : int =&nbsp; &lt;:cfun&lt; <br />
&nbsp; int c;<br />
&nbsp; c = Int_val<span style="color:#8ac6f2;">&#40;</span>a<span style="color:#8ac6f2;">&#41;</span> + Int_val<span style="color:#8ac6f2;">&#40;</span>b<span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; CAMLreturn<span style="color:#8ac6f2;">&#40;</span>Val_int<span style="color:#8ac6f2;">&#40;</span>c<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&gt;&gt;</p>
<p>ext hello <span style="color:#8ac6f2;">&#40;</span>s:string<span style="color:#8ac6f2;">&#41;</span> : unit =&nbsp; &lt;:cfun&lt; <br />
&nbsp; printf<span style="color:#8ac6f2;">&#40;</span><span style="color:#95e454;">&quot;Hello %s !<span style="color:#e7f6da;">\n</span>&quot;</span>, String_val<span style="color:#8ac6f2;">&#40;</span>s<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; CAMLreturn<span style="color:#8ac6f2;">&#40;</span>Val_unit<span style="color:#8ac6f2;">&#41;</span>;<br />
&gt;&gt;</p>
<p>ext ping_pong <span style="color:#8ac6f2;">&#40;</span>n:int<span style="color:#8ac6f2;">&#41;</span> : int = &lt;:cfun&lt;<br />
&nbsp; int c = Int_val<span style="color:#8ac6f2;">&#40;</span>caml_callback<span style="color:#8ac6f2;">&#40;</span> *caml_named_value<span style="color:#8ac6f2;">&#40;</span><span style="color:#95e454;">&quot;fact&quot;</span><span style="color:#8ac6f2;">&#41;</span>, n<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; printf<span style="color:#8ac6f2;">&#40;</span><span style="color:#95e454;">&quot;C : ping. fact 4 = %d<span style="color:#e7f6da;">\n</span>&quot;</span>, c<span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; CAMLreturn<span style="color:#8ac6f2;">&#40;</span>Val_int<span style="color:#8ac6f2;">&#40;</span>c<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&gt;&gt;</p>
<p><span style="color:#8ac6f2;">let</span> fact n =<br />
&nbsp; <span style="color:#8ac6f2;">let</span> <span style="color:#8ac6f2;">rec</span> fact acc = <span style="color:#8ac6f2;">function</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#e5786d;">0</span> -&gt; acc<br />
&nbsp; &nbsp; | n -&gt; fact <span style="color:#8ac6f2;">&#40;</span>n*acc<span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span>n<span style="color:#e5786d;">-1</span><span style="color:#8ac6f2;">&#41;</span><br />
&nbsp; <span style="color:#8ac6f2;">in</span><br />
&nbsp; fact <span style="color:#e5786d;">1</span> n</p>
<p><span style="color:#8ac6f2;">let</span> _ = <br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Callback.html"><span style="color:#e5786d;">Callback</span></a>.<span style="color:#dedede;">register</span> <span style="color:#95e454;">&quot;fact&quot;</span> fact;<br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html"><span style="color:#e5786d;">Printf</span></a>.<span style="color:#dedede;">printf</span> <span style="color:#95e454;">&quot;%d<span style="color:#e7f6da;">\n</span>%!&quot;</span> <span style="color:#8ac6f2;">&#40;</span>add <span style="color:#e5786d;">1</span> <span style="color:#e5786d;">5</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html"><span style="color:#e5786d;">Printf</span></a>.<span style="color:#dedede;">printf</span> <span style="color:#95e454;">&quot;Ocaml : pong. fact 4 = %d<span style="color:#e7f6da;">\n</span>%!&quot;</span> <span style="color:#8ac6f2;">&#40;</span>ping_pong <span style="color:#e5786d;">4</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; hello <span style="color:#95e454;">&quot;World&quot;</span>;</p>
</div>
<p>The <code>ext</code> keyword is the equivalent of <code>let</code> to define <code>external</code> functions. All types must be specified (for the arguments <strong>and</strong> for the return value. Camlp4 the generates the C function, the external declaration etc.</p>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">246 times</span></div>
<div><span class="label">File : </span>  <span class="value">inlinec-0.42.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/inlinec-0.42.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">1.4 ko</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2008/02/13/inlining-c-code-in-ocaml-part-ii/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Inlining C code in OCaml</title>
		<link>http://friggeri.net/blog/2008/02/12/inlining-c-code-in-ocaml</link>
		<comments>http://friggeri.net/blog/2008/02/12/inlining-c-code-in-ocaml#comments</comments>
		<pubDate>Tue, 12 Feb 2008 16:13:38 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2008/02/12/inliner-du-c-dans-ocaml</guid>
		<description><![CDATA[A great thing about OCaml is the possibility of creating bindings with C. The bad thing about it is that it&#039;s a pain to do. Olivier and I are currently thinking about a way to make that less painful. Here is a first draft : inlining C code in OCaml, in order to avoir multiple [...]]]></description>
			<content:encoded><![CDATA[<p>A great thing about <acronym title="Objective Caml">OCaml</acronym> is the possibility of creating bindings with C. The bad thing about it is that it&#039;s a pain to do. <a href="http://callcc.chadok.info/">Olivier</a> and I are currently thinking about a way to make that less painful. Here is a first draft : inlining C code in <acronym title="Objective Caml">OCaml</acronym>, in order to avoir multiple files.</p>
<p>Using camlp4, we extract the <em>quotations</em> containing C code and we dump that in a temporary <code>.c</code> file, and compile it. Here is an example:</p>
<div class="ch_code_container" style="font-family: monospace;">&lt;:c&lt;<br />
#include &lt;caml/mlvalues.<span style="color:#dedede;">h</span>&gt;<br />
#include &lt;caml/memory.<span style="color:#dedede;">h</span>&gt;<br />
#include &lt;caml/alloc.<span style="color:#dedede;">h</span>&gt;<br />
#include &lt;caml/custom.<span style="color:#dedede;">h</span>&gt;<br />
&gt;&gt;</p>
<p><span style="color:#8ac6f2;">external</span> add: int -&gt; int -&gt; int = <span style="color:#95e454;">&quot;add&quot;</span> <br />
&lt;:c&lt; <br />
value add<span style="color:#8ac6f2;">&#40;</span>value a, value b<span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#123;</span><br />
&nbsp; int c;<br />
&nbsp; CAMLparam2<span style="color:#8ac6f2;">&#40;</span>a,b<span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; c = Int_val<span style="color:#8ac6f2;">&#40;</span>a<span style="color:#8ac6f2;">&#41;</span> + Int_val<span style="color:#8ac6f2;">&#40;</span>b<span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; CAMLreturn<span style="color:#8ac6f2;">&#40;</span>Val_int<span style="color:#8ac6f2;">&#40;</span>c<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
<span style="color:#8ac6f2;">&#125;</span><br />
&gt;&gt;</p>
<p><span style="color:#8ac6f2;">let</span> _ = <br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html"><span style="color:#e5786d;">Printf</span></a>.<span style="color:#dedede;">printf</span> <span style="color:#95e454;">&quot;La réponse est %d<span style="color:#e7f6da;">\n</span>%!&quot;</span> <span style="color:#8ac6f2;">&#40;</span>add <span style="color:#e5786d;">21</span> <span style="color:#e5786d;">21</span><span style="color:#8ac6f2;">&#41;</span>;</p>
</div>
<p>The thing we would like to do is the following:</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">let</span> add a b = &lt;:cfun&lt;<br />
&nbsp; int c = Int_val<span style="color:#8ac6f2;">&#40;</span>a<span style="color:#8ac6f2;">&#41;</span> + Int_val<span style="color:#8ac6f2;">&#40;</span>b<span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; CAMLreturn<span style="color:#8ac6f2;">&#40;</span>Val_int<span style="color:#8ac6f2;">&#40;</span>c<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&gt;&gt;</p>
</div>
<p>Which would be converted to:</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">external</span> add: int -&gt; int -&gt; int = <span style="color:#95e454;">&quot;add&quot;</span></p>
</div>
<div class="ch_code_container" style="font-family: monospace;">value add <span style="color: #66cc66;">&#40;</span>value a, value b<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; CAMLparams2<span style="color: #66cc66;">&#40;</span>a,b<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; <span style="color: #993333;">int</span> c = Int_val<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> + Int_val<span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; CAMLreturn<span style="color: #66cc66;">&#40;</span>Val_int<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></p>
</div>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">236 times</span></div>
<div><span class="label">File : </span>  <span class="value">inlinec.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/inlinec.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">732 o</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2008/02/12/inlining-c-code-in-ocaml/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Protoml + Camlp4</title>
		<link>http://friggeri.net/blog/2008/02/11/protoml-camlp4</link>
		<comments>http://friggeri.net/blog/2008/02/11/protoml-camlp4#comments</comments>
		<pubDate>Mon, 11 Feb 2008 17:28:31 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[camlp4]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2008/02/11/protoml-camlp4</guid>
		<description><![CDATA[A little update to this morning&#039;s article, I wrote a little camlp4 extension which makes it easier to user the prototype module (and it doesn&#039;t overwrite the records).
let _ = 
&#160; let p = &#123;&#125; in
&#160; p.foo &#60;- &#34;p.foo&#34;;
&#160; p.baz &#60;- &#34;p.baz&#34;;
&#160; let o = &#123;&#125;&#124;p&#124; in
&#160; o.foo &#60;- &#34;o.foo&#34;;
&#160; o.bar &#60;- &#40;fun f -&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>A little update to <a href="/blog/2008/02/11/ocaml-oriente-prototype">this morning&#039;s article</a>, I wrote a little camlp4 extension which makes it easier to user the prototype module (and it doesn&#039;t overwrite the records).</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">let</span> _ = <br />
&nbsp; <span style="color:#8ac6f2;">let</span> p = <span style="color:#8ac6f2;">&#123;</span><span style="color:#8ac6f2;">&#125;</span> <span style="color:#8ac6f2;">in</span><br />
&nbsp; p.<span style="color:#dedede;">foo</span> &lt;- <span style="color:#95e454;">&quot;p.foo&quot;</span>;<br />
&nbsp; p.<span style="color:#dedede;">baz</span> &lt;- <span style="color:#95e454;">&quot;p.baz&quot;</span>;</p>
<p>&nbsp; <span style="color:#8ac6f2;">let</span> o = <span style="color:#8ac6f2;">&#123;</span><span style="color:#8ac6f2;">&#125;</span>|p| <span style="color:#8ac6f2;">in</span><br />
&nbsp; o.<span style="color:#dedede;">foo</span> &lt;- <span style="color:#95e454;">&quot;o.foo&quot;</span>;<br />
&nbsp; o.<span style="color:#dedede;">bar</span> &lt;- <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> f -&gt; f <span style="color:#8ac6f2;">&#40;</span>o.<span style="color:#dedede;">baz</span><span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;</p>
<p>&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a> <span style="color:#8ac6f2;">&#40;</span>p.<span style="color:#dedede;">foo</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a> <span style="color:#8ac6f2;">&#40;</span>p.<span style="color:#dedede;">baz</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a> <span style="color:#8ac6f2;">&#40;</span>o.<span style="color:#dedede;">foo</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#40;</span>o.<span style="color:#dedede;">bar</span><span style="color:#8ac6f2;">&#41;</span> : <span style="color:#95e454;">&#039;a-&gt;&#039;</span>b<span style="color:#8ac6f2;">&#41;</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a>;</p>
</div>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">237 times</span></div>
<div><span class="label">File : </span>  <span class="value">protoml.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/protoml.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">957 o</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2008/02/11/protoml-camlp4/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prototype based OCaml</title>
		<link>http://friggeri.net/blog/2008/02/11/prototype-based-ocaml</link>
		<comments>http://friggeri.net/blog/2008/02/11/prototype-based-ocaml#comments</comments>
		<pubDate>Mon, 11 Feb 2008 13:02:52 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[randomness]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2008/02/11/ocaml-oriente-prototype</guid>
		<description><![CDATA[Talking about prototypes, here is a little module which allows prototype based programming in OCaml. I&#039;ll warn you right now : it&#039;s horrible, but funny.
type obj =
&#160; &#160; Empty
&#160; &#124; Obj of &#40;string, string&#41; Hashtbl.t * obj
let create ?&#40;parent=Empty&#41; &#40;&#41; = Obj &#40;Hashtbl.create 111, parent&#41;
let set o k v =
&#160; match o with
&#160; &#160; Empty [...]]]></description>
			<content:encoded><![CDATA[<p>Talking about prototypes, here is a little module which allows prototype based programming in <acronym title="Objective Caml">OCaml</acronym>. I&#039;ll warn you right now : it&#039;s horrible, but funny.</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">type</span> obj =<br />
&nbsp; &nbsp; Empty<br />
&nbsp; | <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html"><span style="color:#e5786d;">Obj</span></a> <span style="color:#8ac6f2;">of</span> <span style="color:#8ac6f2;">&#40;</span>string, string<span style="color:#8ac6f2;">&#41;</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html"><span style="color:#e5786d;">Hashtbl</span></a>.<span style="color:#dedede;">t</span> * obj</p>
<p><span style="color:#8ac6f2;">let</span> create ?<span style="color:#8ac6f2;">&#40;</span>parent=Empty<span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#41;</span> = <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html"><span style="color:#e5786d;">Obj</span></a> <span style="color:#8ac6f2;">&#40;</span><a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html"><span style="color:#e5786d;">Hashtbl</span></a>.<span style="color:#dedede;">create</span> <span style="color:#e5786d;">111</span>, parent<span style="color:#8ac6f2;">&#41;</span></p>
<p><span style="color:#8ac6f2;">let</span> set o k v =<br />
&nbsp; <span style="color:#8ac6f2;">match</span> o <span style="color:#8ac6f2;">with</span><br />
&nbsp; &nbsp; Empty -&gt; <span style="color:#8ac6f2;">raise</span> Not_found<br />
&nbsp; | <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html"><span style="color:#e5786d;">Obj</span></a> <span style="color:#8ac6f2;">&#40;</span>o, p<span style="color:#8ac6f2;">&#41;</span> -&gt; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html"><span style="color:#e5786d;">Hashtbl</span></a>.<span style="color:#dedede;">replace</span> o k <span style="color:#8ac6f2;">&#40;</span><a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html"><span style="color:#e5786d;">Obj</span></a>.<span style="color:#dedede;">magic</span> v<span style="color:#8ac6f2;">&#41;</span></p>
<p><span style="color:#8ac6f2;">let</span> <span style="color:#8ac6f2;">rec</span> get o k =<br />
&nbsp; <span style="color:#8ac6f2;">match</span> o <span style="color:#8ac6f2;">with</span><br />
&nbsp; &nbsp; Empty -&gt; <span style="color:#8ac6f2;">raise</span> Not_found<br />
&nbsp; | <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html"><span style="color:#e5786d;">Obj</span></a> <span style="color:#8ac6f2;">&#40;</span>o, p<span style="color:#8ac6f2;">&#41;</span> -&gt;<br />
&nbsp; &nbsp; &nbsp; <span style="color:#8ac6f2;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Obj.html"><span style="color:#e5786d;">Obj</span></a>.<span style="color:#dedede;">magic</span> <span style="color:#8ac6f2;">&#40;</span><a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Hashtbl.html"><span style="color:#e5786d;">Hashtbl</span></a>.<span style="color:#dedede;">find</span> o k<span style="color:#8ac6f2;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#8ac6f2;">with</span> Not_found -&gt; get p k</p>
</div>
<p>There is nothing difficult to understand (it was actually difficult to <strong>do</strong>). An object is just a hashtable and a parent object. Typing is bypassed using <code>Obj.magic</code></p>
<p>Let&#039;s have a little fun:</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">open</span> Proto</p>
<p><span style="color:#8ac6f2;">let</span> _ =<br />
&nbsp; <span style="color:#8ac6f2;">let</span> p = create <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">in</span><br />
&nbsp; set p <span style="color:#95e454;">&quot;foo&quot;</span> <span style="color:#95e454;">&quot;p.foo&quot;</span>;<br />
&nbsp; set p <span style="color:#95e454;">&quot;baz&quot;</span> <span style="color:#95e454;">&quot;p.baz&quot;</span>;</p>
<p>&nbsp; <span style="color:#8ac6f2;">let</span> o = create ~parent:p <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">in</span><br />
&nbsp; set o <span style="color:#95e454;">&quot;foo&quot;</span> <span style="color:#95e454;">&quot;o.foo&quot;</span>;<br />
&nbsp; set o <span style="color:#95e454;">&quot;bar&quot;</span> <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> f -&gt; f <span style="color:#8ac6f2;">&#40;</span>get o <span style="color:#95e454;">&quot;baz&quot;</span><span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span>;</p>
<p>&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a> <span style="color:#8ac6f2;">&#40;</span>get p <span style="color:#95e454;">&quot;foo&quot;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a> <span style="color:#8ac6f2;">&#40;</span>get p <span style="color:#95e454;">&quot;baz&quot;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a> <span style="color:#8ac6f2;">&#40;</span>get o <span style="color:#95e454;">&quot;foo&quot;</span><span style="color:#8ac6f2;">&#41;</span>;<br />
&nbsp; <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#40;</span>get o <span style="color:#95e454;">&quot;bar&quot;</span><span style="color:#8ac6f2;">&#41;</span> : <span style="color:#95e454;">&#039;a-&gt;&#039;</span>b<span style="color:#8ac6f2;">&#41;</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALprint_endline"><span style="">print_endline</span></a>;</p>
</div>
<p>The result:</p>
<div class="ch_code_container" style="font-family: monospace;">$ ./prototest<br />
p.foo<br />
p.baz<br />
o.foo<br />
p.baz</p>
</div>
<p>Note : This is only a proof of concept, never use this in <acronym title="Objective Caml">OCaml</acronym>, it is not at all compliant with <acronym title="Objective Caml">OCaml</acronym> philosophy. It&#039;s funny, however <img src='http://friggeri.net/blog/wp-content/plugins/smilies-themer/friggeri/emoticon_grin.png' alt=':D' class='wp-smiley' /></p>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2008/02/11/prototype-based-ocaml/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Joo, JavaScript on OCaml</title>
		<link>http://friggeri.net/blog/2008/02/07/joo-javascript-on-ocaml</link>
		<comments>http://friggeri.net/blog/2008/02/07/joo-javascript-on-ocaml#comments</comments>
		<pubDate>Thu, 07 Feb 2008 16:08:12 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2008/02/07/joo-javascript-on-ocaml</guid>
		<description><![CDATA[My readers might have noticed that I really like JavaScript and OCaml. Thus, it&#039;s natural to write a webserver in OCaml which serves scripts written in JavaScript. Crazy ? Maybe a little, but as the guys from  mod_js said: others do worse.
I started by modifying Thumper which I slightly modified (in order to deal [...]]]></description>
			<content:encoded><![CDATA[<p>My readers might have noticed that I really like JavaScript and <acronym title="Objective Caml">OCaml</acronym>. Thus, it&#039;s natural to write a webserver in <acronym title="Objective Caml">OCaml</acronym> which serves scripts written in JavaScript. Crazy ? Maybe a little, but as the guys from  <a href="http://www.modjs.org/">mod_js</a> said: <a href="http://modbf.sourceforge.net/">others</a> do worse.</p>
<p>I started by modifying <a href="http://www.vector-seven.com/2007/07/17/thumper-my-ugly-little-ocaml-web-server/">Thumper</a> which I slightly modified (in order to deal with post queries). Then I added <a href="http://www.lri.fr/~filliatr/ftp/ocaml/cgi/"><acronym title="Objective Caml">OCaml</acronym> <acronym title="Common Gateway Interface">CGI</acronym></a> to encode and decode queries, <a href="http://alain.frisch.fr/soft.html#spider">SpiderCaml</a> which includes <a href="http://www.mozilla.org/js/spidermonkey/">SpiderMonkey</a> in <acronym title="Objective Caml">OCaml</acronym> and  <a href="http://raevnos.pennmush.org/code/ocaml-mysql/"><acronym title="Objective Caml">OCaml</acronym> Mysql</a> to use MySQL. Actually this was more like doing Lego than coding&#8230;</p>
<p>What can it do ?<br />
URIs are parsed as : <code>/module/action/arg1/arg2/arg3</code>. This will execute <code>Module.action(arg1, arg2, arg3)</code>, where <code>Module</code> is defined in the file <code>modules/module.js</code>. The default module being <em>home</em> and the default action, <em>index</em>.</p>
<p>This being said, their is a <code>Joo</code> object in the global namespace which contains useful stuff:</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#99968b;">// headers</span><br />
<span style="color:#8ac6f2;">for</span> <span style="color:#dedede;">&#40;</span>e <span style="color:#8ac6f2;">in</span> Joo.<span style="color:#dedede;">Headers</span><span style="color:#dedede;">&#41;</span> <span style="color:#cae682;">print</span><span style="color:#dedede;">&#40;</span>e+<span style="color:#95e454;">&quot; &quot;</span>+Joo.<span style="color:#dedede;">Headers</span><span style="color:#dedede;">&#91;</span>e<span style="color:#dedede;">&#93;</span>+<span style="color:#95e454;">&quot;&lt;br/&gt;&quot;</span><span style="color:#dedede;">&#41;</span>;</p>
<p><span style="color:#99968b;">//&nbsp; GET</span><br />
<span style="color:#8ac6f2;">for</span> <span style="color:#dedede;">&#40;</span>e <span style="color:#8ac6f2;">in</span> Joo.<span style="color:#dedede;">Get</span><span style="color:#dedede;">&#41;</span> <span style="color:#cae682;">print</span><span style="color:#dedede;">&#40;</span>e+<span style="color:#95e454;">&quot; &quot;</span>+Joo.<span style="color:#dedede;">Headers</span><span style="color:#dedede;">&#91;</span>e<span style="color:#dedede;">&#93;</span>+<span style="color:#95e454;">&quot;&lt;br/&gt;&quot;</span><span style="color:#dedede;">&#41;</span>;</p>
<p><span style="color:#99968b;">// POST</span><br />
<span style="color:#8ac6f2;">for</span> <span style="color:#dedede;">&#40;</span>e <span style="color:#8ac6f2;">in</span> Joo.<span style="color:#dedede;">Post</span><span style="color:#dedede;">&#41;</span> <span style="color:#cae682;">print</span><span style="color:#dedede;">&#40;</span>e+<span style="color:#95e454;">&quot; &quot;</span>+Joo.<span style="color:#dedede;">Headers</span><span style="color:#dedede;">&#91;</span>e<span style="color:#dedede;">&#93;</span>+<span style="color:#95e454;">&quot;&lt;br/&gt;&quot;</span><span style="color:#dedede;">&#41;</span>;</p>
<p><span style="color:#99968b;">// a mysql wrapper</span><br />
Joo.<span style="color:#dedede;">mysql</span>.<span style="color:#dedede;">connect</span><span style="color:#dedede;">&#40;</span><span style="color:#dedede;">&#123;</span><br />
&nbsp; &nbsp; host:<span style="color:#95e454;">&quot;localhost&quot;</span>,<br />
&nbsp; &nbsp; user:<span style="color:#95e454;">&quot;user&quot;</span>,<br />
&nbsp; &nbsp; <span style="color:#cae682;">name</span>:<span style="color:#95e454;">&quot;database&quot;</span><br />
&nbsp; <span style="color:#dedede;">&#125;</span><span style="color:#dedede;">&#41;</span>;<br />
Joo.<span style="color:#dedede;">mysql</span>.<span style="color:#dedede;">query</span><span style="color:#dedede;">&#40;</span><span style="color:#95e454;">&#039;select * from foo&#039;</span><span style="color:#dedede;">&#41;</span>;<br />
<span style="color:#8ac6f2;">while</span><span style="color:#dedede;">&#40;</span>r = Joo.<span style="color:#dedede;">mysql</span>.<span style="color:#dedede;">fetch</span><span style="color:#dedede;">&#40;</span><span style="color:#dedede;">&#41;</span><span style="color:#dedede;">&#41;</span> <span style="color:#cae682;">print</span><span style="color:#dedede;">&#40;</span>r.<span style="color:#dedede;">id</span> + <span style="color:#95e454;">&quot;&lt;br/&gt;&quot;</span><span style="color:#dedede;">&#41;</span>;</p>
<p><span style="color:#99968b;">// an include function</span><br />
Joo.<span style="color:#dedede;">include</span><span style="color:#dedede;">&#40;</span><span style="color:#95e454;">&#039;foo/bar.js&#039;</span><span style="color:#dedede;">&#41;</span>;</p>
</div>
<p>Note : the <code>print</code> function just adds text to the output buffer, their is no control on the flushing, sorry. Everything is sent at the end.<br />
Moreover, I have added the <acronym title="Multipurpose Internet Mail Extension">MIME</acronym> types only for html and jpeg files, you may want to add other <acronym title="Multipurpose Internet Mail Extension">MIME</acronym> types in <code>main.ml</code></p>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">293 times</span></div>
<div><span class="label">File : </span>  <span class="value">joo.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/joo.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">47.1 ko</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2008/02/07/joo-javascript-on-ocaml/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sandclick</title>
		<link>http://friggeri.net/blog/2007/12/17/sandclick</link>
		<comments>http://friggeri.net/blog/2007/12/17/sandclick#comments</comments>
		<pubDate>Mon, 17 Dec 2007 15:35:53 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[randomness]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2007/12/17/sandclick</guid>
		<description><![CDATA[Often when I feel depressed I have this imperious need to create something, anything. And those days, I&#039;m not so well, so I code quite a lot. Let me introduce you to Sandclick, a stupid little game written in less than an hour in OCaml. The code is awful, but nevermind&#8230;
First, what does it look [...]]]></description>
			<content:encoded><![CDATA[<p>Often when I feel depressed I have this imperious need to create something, anything. And those days, I&#039;m not so well, so I code quite a lot. Let me introduce you to Sandclick, a stupid little game written in less than an hour in <acronym title="Objective Caml">OCaml</acronym>. The code is awful, but nevermind&#8230;</p>
<p>First, what does it look like ?</p>
<div style="text-align:center;"><img src="/wp-content/sandclick.png"/></div>
<p>I bet most of you have already played to those games where you have to click on colored boxes on a grid to make them disappear. Sandclick is exactly this king of game, the only difference is in the way it deals with falling boxes.</p>
<p>Usually, in those games, when a colored block is above and empty space, it falls into it. In Sandclick, blocks fall like sand : grains can fall on the side, here is what happens when there is an empty space:</p>
<ul>
<li>if there is something above, this something falls down</li>
<li>if not, then the content of what is on the top left or top right falls</li>
</ul>
<p>And it goes on and on until it stabilizes.</p>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">278 times</span></div>
<div><span class="label">File : </span>  <span class="value">sandclick.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/sandclick.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">9.5 ko</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2007/12/17/sandclick/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OCaml zipper and holidays</title>
		<link>http://friggeri.net/blog/2007/08/03/ocaml-zipper-and-holidays</link>
		<comments>http://friggeri.net/blog/2007/08/03/ocaml-zipper-and-holidays#comments</comments>
		<pubDate>Fri, 03 Aug 2007 13:52:22 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[randomness]]></category>
		<category><![CDATA[ocaml]]></category>
		<category><![CDATA[zipper]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2007/08/03/zipper-ocaml-et-vacances</guid>
		<description><![CDATA[This blog will be in holidays until the 15th of August. This is a little something to keep you busy while waiting for my holiday pictures : a light and nifty implementation of list zippers.
Basically a zipper is a list which has a cursor, thus reading and moving through is non destructive.
sig
&#160; type &#039;a t [...]]]></description>
			<content:encoded><![CDATA[<p>This blog will be in holidays until the 15th of August. This is a little something to keep you busy while waiting for my holiday pictures : a light and nifty implementation of list zippers.</p>
<p>Basically a zipper is a list which has a cursor, thus reading and moving through is non destructive.</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">sig</span><br />
&nbsp; <span style="color:#8ac6f2;">type</span> <span style="color:#95e454;">&#039;a t = &#039;</span>a list * <span style="color:#95e454;">&#039;a list<br />
&nbsp; exception Out_of_bound<br />
&nbsp; exception End_of_zip<br />
&nbsp; val empty : &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> next : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> previous : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> first : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> last : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> get : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a<br />
&nbsp; <span style="color:#8ac6f2;">val</span> insert : <span style="color:#95e454;">&#039;a -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span> -&gt; <span style="color:#95e454;">&#039;a Zip.t<br />
&nbsp; val delete : &#039;</span>a Zip.<span style="color:#dedede;">t</span> -&gt; <span style="color:#95e454;">&#039;a Zip.t<br />
&nbsp; val replace : &#039;</span>a -&gt; <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> rev : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span><br />
&nbsp; <span style="color:#8ac6f2;">val</span> append : <span style="color:#95e454;">&#039;a Zip.t -&gt; &#039;</span>a Zip.<span style="color:#dedede;">t</span> -&gt; <span style="color:#95e454;">&#039;a Zip.t<br />
&nbsp; val iter : (&#039;</span>a -&gt; unit<span style="color:#8ac6f2;">&#41;</span> -&gt; <span style="color:#95e454;">&#039;a Zip.t -&gt; unit<br />
&nbsp; val chop : &#039;</span>a Zip.<span style="color:#dedede;">t</span> -&gt; <span style="color:#95e454;">&#039;a Zip.t<br />
&nbsp; val from_list : &#039;</span>a list -&gt; <span style="color:#95e454;">&#039;a Zip.t<br />
&nbsp; val to_list : &#039;</span>a Zip.<span style="color:#dedede;">t</span> -&gt; <span style="color:#95e454;">&#039;a list<br />
end</span></p>
</div>
<p>A simple example : </p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">open</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html"><span style="color:#e5786d;">Printf</span></a></p>
<p><span style="color:#8ac6f2;">let</span> _ = <br />
&nbsp; <span style="color:#8ac6f2;">let</span> print = <span style="color:#8ac6f2;">function</span><br />
&nbsp; &nbsp; &nbsp; b, <span style="color:#8ac6f2;">&#91;</span><span style="color:#8ac6f2;">&#93;</span> -&gt; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">iter</span> <span style="color:#8ac6f2;">&#40;</span>printf <span style="color:#95e454;">&quot; %d&quot;</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span><a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">rev</span> b<span style="color:#8ac6f2;">&#41;</span>; printf <span style="color:#95e454;">&quot;.<span style="color:#e7f6da;">\n</span>&quot;</span><br />
&nbsp; &nbsp; | b, c::a -&gt; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">iter</span> <span style="color:#8ac6f2;">&#40;</span>printf <span style="color:#95e454;">&quot; %d&quot;</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span><a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">rev</span> b<span style="color:#8ac6f2;">&#41;</span>; printf <span style="color:#95e454;">&quot;.%d&quot;</span> c; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">iter</span> <span style="color:#8ac6f2;">&#40;</span>printf <span style="color:#95e454;">&quot; %d&quot;</span><span style="color:#8ac6f2;">&#41;</span> a; printf <span style="color:#95e454;">&quot;<span style="color:#e7f6da;">\n</span>&quot;</span><br />
&nbsp; <span style="color:#8ac6f2;">in</span><br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">empty</span> <span style="color:#8ac6f2;">in</span> <br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">insert</span> <span style="color:#e5786d;">5</span> z <span style="color:#8ac6f2;">in</span> <br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z2 = Zip.<span style="color:#dedede;">from_list</span> <span style="color:#8ac6f2;">&#91;</span><span style="color:#e5786d;">1</span>;<span style="color:#e5786d;">2</span>;<span style="color:#e5786d;">3</span>;<span style="color:#e5786d;">4</span><span style="color:#8ac6f2;">&#93;</span> <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z2;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">append</span> z2 z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">next</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">next</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">next</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">previous</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">last</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">previous</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">previous</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">previous</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">insert</span> <span style="color:#e5786d;">7</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">replace</span> <span style="color:#e5786d;">9</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z;<br />
&nbsp; <span style="color:#8ac6f2;">let</span> z = Zip.<span style="color:#dedede;">delete</span> z <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; print z</p>
</div>
<p>Its result :</p>
<div class="ch_code_container" style="font-family: monospace;">.5<br />
.1 2 3 4<br />
.1 2 3 4 5<br />
&nbsp;1.2 3 4 5<br />
&nbsp;1 2.3 4 5<br />
&nbsp;1 2 3.4 5<br />
&nbsp;1 2.3 4 5<br />
&nbsp;1 2 3 4 5.<br />
&nbsp;1 2 3 4.5<br />
&nbsp;1 2 3.4 5<br />
&nbsp;1 2.3 4 5<br />
&nbsp;1 2.7 3 4 5<br />
&nbsp;1 2.9 3 4 5<br />
&nbsp;1 2.3 4 5</p>
</div>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">167 times</span></div>
<div><span class="label">File : </span>  <span class="value">ml_zipper.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/ml_zipper.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">1 ko</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2007/08/03/ocaml-zipper-and-holidays/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>List comprehension in OCaml</title>
		<link>http://friggeri.net/blog/2007/07/24/list-comprehension-in-ocaml</link>
		<comments>http://friggeri.net/blog/2007/07/24/list-comprehension-in-ocaml#comments</comments>
		<pubDate>Tue, 24 Jul 2007 09:32:52 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[list comprehension]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2007/07/24/comprehension-de-listes-en-ocaml/</guid>
		<description><![CDATA[A nifty thing when working with list is to use list comprehension. This is something natural for a python programmer :
l1 = &#91;&#40;1,2&#41;,&#40;2,3&#41;,&#40;3,4&#41;,&#40;4,5&#41;,&#40;5,6&#41;,&#40;6,7&#41;&#93;
# [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)]
l2 = &#91;2*x+y for &#40;x,y&#41; in l1&#93;
# [4, 7, 10, 13, 16, 19]
l3 = &#91;x for x in l2 if x [...]]]></description>
			<content:encoded><![CDATA[<p>A nifty thing when working with list is to use <em>list comprehension</em>. This is something natural for a python programmer :</p>
<div class="ch_code_container" style="font-family: monospace;">l1 = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span>,<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>,<span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span>,<span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><br />
<span style="color: #808080; font-style: italic;"># [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7)]</span><br />
l2 = <span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>*x+y <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> l1<span style="color: black;">&#93;</span><br />
<span style="color: #808080; font-style: italic;"># [4, 7, 10, 13, 16, 19]</span><br />
l3 = <span style="color: black;">&#91;</span>x <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> l2 <span style="color: #ff7700;font-weight:bold;">if</span> x &gt; <span style="color: #ff4500;">12</span><span style="color: black;">&#93;</span><br />
<span style="color: #808080; font-style: italic;"># [13, 16, 19]</span></p>
</div>
<p>It is however absent from <acronym title="Objective Caml">OCaml</acronym>&#8230; Of course, it can be emulated:</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">let</span> l1 = <span style="color:#8ac6f2;">&#91;</span><span style="color:#e5786d;">1</span>,<span style="color:#e5786d;">2</span>;<span style="color:#e5786d;">2</span>,<span style="color:#e5786d;">3</span>;<span style="color:#e5786d;">3</span>,<span style="color:#e5786d;">4</span>;<span style="color:#e5786d;">4</span>,<span style="color:#e5786d;">5</span>;<span style="color:#e5786d;">5</span>,<span style="color:#e5786d;">6</span>;<span style="color:#e5786d;">6</span>,<span style="color:#e5786d;">7</span><span style="color:#8ac6f2;">&#93;</span>&nbsp; <span style="color:#8ac6f2;">in</span><br />
<span style="color:#8ac6f2;">let</span> l2 = <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> fmap l -&gt; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">rev_map</span> fmap <span style="color:#8ac6f2;">&#40;</span><a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">rev</span> l<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> <span style="color:#8ac6f2;">&#40;</span>x,y<span style="color:#8ac6f2;">&#41;</span> -&gt; <span style="color:#e5786d;">2</span>*x+y<span style="color:#8ac6f2;">&#41;</span> l1 <span style="color:#8ac6f2;">in</span><br />
<span style="color:#8ac6f2;">let</span> l3 = <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> ffilt fmap l -&gt; <br />
&nbsp; &nbsp; <span style="color:#8ac6f2;">let</span> rev_filter f l = <br />
&nbsp; &nbsp; &nbsp; <span style="color:#8ac6f2;">let</span> <span style="color:#8ac6f2;">rec</span> aux d = <span style="color:#8ac6f2;">fun</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#8ac6f2;">&#91;</span>&nbsp; <span style="color:#8ac6f2;">&#91;</span><span style="color:#8ac6f2;">&#93;</span> -&gt; d<br />
&nbsp; &nbsp; &nbsp; | <span style="color:#8ac6f2;">&#91;</span>t::q<span style="color:#8ac6f2;">&#93;</span> -&gt; aux <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">if</span> f t <span style="color:#8ac6f2;">then</span> <span style="color:#8ac6f2;">&#91;</span>t::d<span style="color:#8ac6f2;">&#93;</span> <span style="color:#8ac6f2;">else</span> d<span style="color:#8ac6f2;">&#41;</span> q<span style="color:#8ac6f2;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; &nbsp; aux <span style="color:#8ac6f2;">&#91;</span><span style="color:#8ac6f2;">&#93;</span> l<br />
&nbsp; &nbsp; <span style="color:#8ac6f2;">in</span><br />
&nbsp; &nbsp; <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color:#e5786d;">List</span></a>.<span style="color:#dedede;">rev_map</span> fmap <span style="color:#8ac6f2;">&#40;</span>rev_filter ffilt l<span style="color:#8ac6f2;">&#41;</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> x -&gt; x &gt; <span style="color:#e5786d;">12</span><span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">fun</span> x -&gt; x<span style="color:#8ac6f2;">&#41;</span> l2 <br />
<span style="color:#8ac6f2;">in</span> <span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#41;</span></p>
</div>
<p>But this is really too much&#8230; Wouldn&#039;t it be nice to write something like that : </p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">let</span> l1 = <span style="color:#8ac6f2;">&#91;</span><span style="color:#e5786d;">1</span>,<span style="color:#e5786d;">2</span>;<span style="color:#e5786d;">2</span>,<span style="color:#e5786d;">3</span>;<span style="color:#e5786d;">3</span>,<span style="color:#e5786d;">4</span>;<span style="color:#e5786d;">4</span>,<span style="color:#e5786d;">5</span>;<span style="color:#e5786d;">5</span>,<span style="color:#e5786d;">6</span>;<span style="color:#e5786d;">6</span>,<span style="color:#e5786d;">7</span><span style="color:#8ac6f2;">&#93;</span>&nbsp; <span style="color:#8ac6f2;">in</span><br />
<span style="color:#8ac6f2;">let</span> l2 = <span style="color:#8ac6f2;">&#91;</span>+ <span style="color:#e5786d;">2</span>*x+y | <span style="color:#8ac6f2;">&#40;</span>x,y<span style="color:#8ac6f2;">&#41;</span> &lt;- l1<span style="color:#8ac6f2;">&#93;</span> <span style="color:#8ac6f2;">in</span><br />
<span style="color:#8ac6f2;">let</span> l3 = <span style="color:#8ac6f2;">&#91;</span>+ x | x &lt;- l2 <span style="color:#8ac6f2;">when</span> x &gt; <span style="color:#e5786d;">12</span><span style="color:#8ac6f2;">&#93;</span> <span style="color:#8ac6f2;">in</span><br />
<span style="color:#8ac6f2;">&#40;</span><span style="color:#8ac6f2;">&#41;</span></p>
</div>
<p>The <code>[+ (* ... *)]</code> indicates that this is a list comprehension. This actually is possible using camlp4 <img src='http://friggeri.net/blog/wp-content/plugins/smilies-themer/friggeri/emoticon_wink.png' alt=';)' class='wp-smiley' /> </p>
<div class="download_box box">
<div class='right'><span class="label">Downloaded : </span>  <span class="value">182 times</span></div>
<div><span class="label">File : </span>  <span class="value">listcomp.tar.gz</span></div>
<div class='right'><span class="dl">    <a href="http://friggeri.net/download/listcomp.tar.gz">download</a>  </span></div>
<div><span class="label">Size: </span>  <span class="value">841 o</span></div>
</div>
<div class="clear"></div>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2007/07/24/list-comprehension-in-ocaml/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ocaml et hexadécimal</title>
		<link>http://friggeri.net/blog/2007/07/23/ocaml-et-hexadecimal</link>
		<comments>http://friggeri.net/blog/2007/07/23/ocaml-et-hexadecimal#comments</comments>
		<pubDate>Mon, 23 Jul 2007 14:12:38 +0000</pubDate>
		<dc:creator>Adrien Friggeri</dc:creator>
				<category><![CDATA[hexadecimal]]></category>
		<category><![CDATA[ocaml]]></category>

		<guid isPermaLink="false">http://www.friggeri.net/blog/2007/07/23/ocaml-et-hexadecimal/</guid>
		<description><![CDATA[Je vois qu&#039;il y a beaucoup de gens qui tombent sur mon blog en cherchant &#034;ocaml hexadecimal&#034;. Et ils doivent être décus de ne rien trouver&#8230; C&#039;est pourquoi j&#039;ai décidé d&#039;écrire rapidement un petit quelque chose à ce sujet là.
Tout d&#039;abord, il faut savoir qu&#039;en caml, les choses suivantes sont équivalentes :
# 0xf;;
- : int [...]]]></description>
			<content:encoded><![CDATA[<p>Je vois qu&#039;il y a beaucoup de gens qui tombent sur mon blog en cherchant &#034;ocaml hexadecimal&#034;. Et ils doivent être décus de ne rien trouver&#8230; C&#039;est pourquoi j&#039;ai décidé d&#039;écrire rapidement un petit quelque chose à ce sujet là.</p>
<p>Tout d&#039;abord, il faut savoir qu&#039;en caml, les choses suivantes sont équivalentes :<br />
<code># 0xf;;<br />
- : int = 15<br />
# 0o17;;<br />
- : int = 15<br />
# 0b1111;;<br />
- : int = 15<br />
# 15;;<br />
- : int = 15</code><br />
Le premier est écrit en hexadécimal, le second en ocal, le troisième en binaire, et le dernier en décimal. Donc la gestion de l&#039;hexadécimal en <acronym title="Objective Caml">OCaml</acronym> est native.</p>
<p>Une des utilisations courantes de l&#039;hexadécimal est pour représenter les couleurs en 65536 couleurs. Pour convertir un entier en tuple r,g,b on peut utiliser la fonction suivante : </p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">let</span> hex_to_rgb n =&nbsp; <span style="color:#8ac6f2;">&#40;</span>n / 0&#215;10000, <span style="color:#8ac6f2;">&#40;</span>n/0&#215;100<span style="color:#8ac6f2;">&#41;</span> <span style="color:#8ac6f2;">mod</span> 0&#215;100, n <span style="color:#8ac6f2;">mod</span> 0&#215;100<span style="color:#8ac6f2;">&#41;</span></p>
</div>
<p>Dans l&#039;autre sens, c&#039;est un peu plus compliqué : en effet, en mémoire tous les entiers sont stockés de la même manière (int31 à la caml), et on s&#039;en fiche de savoir sous quelle forme ils sont entrés. Mais ce n&#039;est pas un drame ; effectivement, on a besoin d&#039;avoir un entier sous forme hexadécimale principalement pour l&#039;affichage, donc on utilise les fonctions du module Printf :</p>
<div class="ch_code_container" style="font-family: monospace;"><span style="color:#8ac6f2;">let</span> print_hexa = printf <span style="color:#95e454;">&quot;0x%x&quot;</span></p>
</div>
<p>Je vous renvoie à la documentation du module <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html">Pervasives</a> pour plus de détails.</p>
<p>Espérons que maintenant ceux qui passeront ici en cherchant <em>ocaml hexadécimal</em> repartirons heureux <img src='http://friggeri.net/blog/wp-content/plugins/smilies-themer/friggeri/emoticon_wink.png' alt=';)' class='wp-smiley' /></p>]]></content:encoded>
			<wfw:commentRss>http://friggeri.net/blog/2007/07/23/ocaml-et-hexadecimal/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
