<?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>Zenol&#039;s Blog</title>
	<atom:link href="http://zenol.fr/site/feed/" rel="self" type="application/rss+xml" />
	<link>http://zenol.fr/site</link>
	<description>Carnet de bord de Jérémy Cochoy</description>
	<lastBuildDate>Thu, 23 May 2013 19:52:50 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Un série d&#039;articles-turoriels sur le développement d&#039;un raytracer en haskell</title>
		<link>http://zenol.fr/site/2013/05/14/un-serie-darticles-turoriels-sur-le-developpement-dun-raytracer-en-haskell/</link>
		<comments>http://zenol.fr/site/2013/05/14/un-serie-darticles-turoriels-sur-le-developpement-dun-raytracer-en-haskell/#comments</comments>
		<pubDate>Tue, 14 May 2013 18:24:30 +0000</pubDate>
		<dc:creator>Zenol</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Useful]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[raytracer]]></category>

		<guid isPermaLink="false">http://zenol.fr/site/?p=859</guid>
		<description><![CDATA[Une fois n'est pas coutume, une série de tutoriels seront publiés sur le site http://www.developpez.net. Ce premier article traite du b.a.-ba du raytracing : mise en place d'une caméra, plan de projection, calcul des intersections pour une sphère centré à l'origine, position des objets, matériaux et une "fausse" illumination uniquement basée sur la distance. Une <a href='http://zenol.fr/site/2013/05/14/un-serie-darticles-turoriels-sur-le-developpement-dun-raytracer-en-haskell/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Une fois n'est pas coutume, une série de tutoriels seront publiés sur le site <a href="http://cochoy-jeremy.developpez.com/articles/haskell/haskell_rt_p1/">http://www.developpez.net</a>.</p>
<p>Ce premier article traite du b.a.-ba du raytracing : mise en place d'une caméra, plan de projection, calcul des intersections pour une sphère centré à l'origine, position des objets, matériaux et une "fausse" illumination uniquement basée sur la distance. Une bonne façon de commencer un raytracer et d'expérimenter par la suite de nouveaux objets / calculs de luminosité.</p>
<p>L'article est disponible à l'adresse : <a href="http://cochoy-jeremy.developpez.com/articles/haskell/haskell_rt_p1/">http://cochoy-jeremy.developpez.com/articles/haskell/haskell_rt_p1/</a>.</p>
<p>N'hésitez pas à commenter / rajouter des précision / signaler les erreurs et typos, que ce soit via les commentaires du blog, ou directement sur le forum de développez.</p>
]]></content:encoded>
			<wfw:commentRss>http://zenol.fr/site/2013/05/14/un-serie-darticles-turoriels-sur-le-developpement-dun-raytracer-en-haskell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Le Haskell, un langage au label pure. Troisième partie.</title>
		<link>http://zenol.fr/site/2013/05/03/le-haskell-un-langage-au-label-pure-troisieme-partie/</link>
		<comments>http://zenol.fr/site/2013/05/03/le-haskell-un-langage-au-label-pure-troisieme-partie/#comments</comments>
		<pubDate>Fri, 03 May 2013 17:42:13 +0000</pubDate>
		<dc:creator>Zenol</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Useful]]></category>
		<category><![CDATA[catégorie]]></category>
		<category><![CDATA[foncteurs]]></category>
		<category><![CDATA[foncteurs applicatifs]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[monades]]></category>

		<guid isPermaLink="false">http://zenol.fr/site/?p=810</guid>
		<description><![CDATA[Et voici la troisième et dernière partie de notre découverte de ce joli langage. Au programme : de la généralisation de foncteur. D'abord des foncteurs applicatifs, puis des monades! Si vous rêviez de savoir pourquoi tant de gens s’enflamment devant ce langage, il est peut-être venue l'heure de la révélation. La première partie est ici, <a href='http://zenol.fr/site/2013/05/03/le-haskell-un-langage-au-label-pure-troisieme-partie/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Et voici la troisième et dernière partie de notre découverte de ce joli langage. Au programme : de la généralisation de foncteur. D'abord des foncteurs applicatifs, puis des monades! Si vous rêviez de savoir pourquoi tant de gens s’enflamment devant ce langage, il est peut-être venue l'heure de la révélation.</p>
<p>La <a href="http://zenol.fr/site/2013/04/16/le-haskell-un-language-au-label-pure-13/" title="Le Haskell, un langage au label pure. Première partie.">première partie est ici</a>, <a href="http://zenol.fr/site/2013/04/26/le-haskell-un-langage-au-label-pure-seconde-partie/" title="Le Haskell, un langage au label pure. Seconde partie.">la seconde là</a>.</p>
<h3>Foncteurs applicatifs</h3>
<p>Les foncteurs applicatifs sont un enrichissement des foncteurs. C'est donc une classe, définie dans le module Control.Applicative, de la façon suivante :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code25'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81025"><td class="code" id="p810code25"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #cccc00; font-weight: bold;">Functor</span></a> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> Applicative f <span style="color: #06c; font-weight: bold;">where</span>
   pure  <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> f a
   <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;*&gt;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> f <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> f a <span style="color: #339933; font-weight: bold;">-&gt;</span> f b</pre></td></tr></table></div>

<p>Avec un type fonctoriel Fonc, on pouvais :<br />
-Prendre un élément de type <i>c</i> et le transformer en <i>Fonc c</i>, c'est à dire le "placer dans un contexte minimal".<br />
-Prendre une fonction de type <i>a->b</i> et la transformer en une fonction de type <i>Fonc a -> Fonc b</i> grâce à <i>fmap</i>.</p>
<p>Grace à un foncteur applicatif, on peut :<br />
-Prendre un élément de type <i>c</i> et le transformer en type <i>Fonc c</i>, cela se fait grâce à la fonction <i>pure :: (Applicative f) => c -> f c</i><br />
-Prendre une fonction dans un foncteur (ie un élément de type <i>Fonc (a->b)</i>) et l'appliquer à un élément de type <i>Fonc a</i> pour produire un <i>Fonc b</i>. L'opérateur permettant une telle chose est noté <i>&lt;*&gt; :: (Applicative f) => f (a -> b) -> f a -> f b</i>.</p>
<p>La seconde notion est plus générale, car si vous avez un constructeur de type Fonc qui est une instance de Applicative (La classe des foncteurs applicatifs), alors c'est un foncteur de la façon suivante :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code26'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81026"><td class="code" id="p810code26"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #cccc00; font-weight: bold;">Functor</span></a> Fonc <span style="color: #06c; font-weight: bold;">where</span>
   <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f e <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>pure f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> e</pre></td></tr></table></div>

<p>C'est à dire : Prenez f une fonction de type <i>a -> b</i>, alors vous savez comment l'appliquer sur un <i>Fonc a</i>. Il suffit de la "placer" elle aussi dans un foncteur, pour se retrouver avec une "fonction dans un contexte" de type <i>Fonc (a->b)</i> à appliquer sur un "élément dans un contexte" de type <i>F a</i>.</p>
<p>C'est donc pour cela que l'on impose à tout foncteur applicatif d'être d'abord une instance de Functor, et que vous avez la contraire <i>"Functor f"</i> dans la définition de la classe applicative.</p>
<h4>On se prépare au grand saut avec maybe</h4>
<p>Tout ceci est très abstrait, alors regardons ce que cela donne avec un exemple simple. On considère Maybe qui est une instance de Applicative de la façon suivante :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code27'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81027"><td class="code" id="p810code27"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> Applicative <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: #06c; font-weight: bold;">where</span>
   pure <span style="color: #339933; font-weight: bold;">=</span> Just
   <span style="color: green;">&#40;</span>Just f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> <span style="color: green;">&#40;</span>Just x<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> Just <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span>
   <span style="color: #339933; font-weight: bold;">_</span>        <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> <span style="color: #339933; font-weight: bold;">_</span>        <span style="color: #339933; font-weight: bold;">=</span> Nothing</pre></td></tr></table></div>

<p>La méthode <i>pure</i> prend un <i>truc :: a</i> et le place dans un <i>Maybe a</i> par <i>Just truc</i>. C'est la façon la plus intuitive de placer notre <i>truc</i> dans un Maybe, sans perdre d'information. L'opérateur <i>&lt;*&gt;</i> se contente lui de récupérer la fonction à sa gauche, la valeur à sa droite, d'effectuer le calcul (l'application de f à x) puis de placer le résultat dans un contexte minimal en utilisant le constructeur Just. La dernière ligne s’occupe des cas où il manque la fonction, l'argument, ou bien les deux. Dans ces trois cas, on ne peut effectuer le calcul, et l'on ne peut donc produire de résultat.</p>
<p>Comme dit plus haut, si l'on vais une fonction qui prend la racine carré (<i>sqrt :: Int</i>) d'un entier, et "peut-être un nombre" (un <i>v :: Maybe Int</i>), on pouvais mapper notre fonction avec la ligne <i>fmap sqrt v</i>. Avec un foncteur applicatif, on peut aussi écrire :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code28'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81028"><td class="code" id="p810code28"><pre class="haskell" style="font-family:monospace;">resultat <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>pure <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v</pre></td></tr></table></div>

<p>Mais alors, quel est l’intérêt des foncteurs applicatifs? Un premier exemple est l'application d'une fonction prenant trois entier à trois "peut-être un entier".</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code29'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81029"><td class="code" id="p810code29"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--Ici, v1, v2 et v3 sont de type Maybe Int.</span>
<span style="color: #5d478b; font-style: italic;">--Si vous voulez savoir d'où il viennent, disons que ce sont le résultat</span>
<span style="color: #5d478b; font-style: italic;">--de la lecture d'une chaine de caractère et de tentative de conversion de</span>
<span style="color: #5d478b; font-style: italic;">--la chaine en nombres. Si c'était possible, alors on a des valeurs. Sinon, on auras &quot;Nothing&quot;.</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">--On a une superbe fonction :</span>
deepThought <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> Answer
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Et on veut l'appliquer à v1, v2, v3.</span>
<span style="color: #5d478b; font-style: italic;">--Si l'on essaye avec un foncteur :</span>
premiereApplication <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span>Int<span style="color: #339933; font-weight: bold;">-&gt;</span>Int<span style="color: #339933; font-weight: bold;">-&gt;</span>Answer<span style="color: green;">&#41;</span>
premiereApplication <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> deepThought v1
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Maintenant, on est bloquer, on ne sais pas appliquer</span>
<span style="color: #5d478b; font-style: italic;">-- une fonction qui se trouve dans un maybe...</span>
<span style="color: #5d478b; font-style: italic;">--... à moins d'utiliser &lt;*&gt; :)</span>
secondeApplication <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> Answer<span style="color: green;">&#41;</span>
secondeApplication <span style="color: #339933; font-weight: bold;">=</span> premiereApplication <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v2
derniereApplication <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> Answer
derniereApplication <span style="color: #339933; font-weight: bold;">=</span> secondeApplication <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v3
&nbsp;
<span style="color: #5d478b; font-style: italic;">--En fait, on aurait pu d'abord placer la fonction dans un Just,</span>
<span style="color: #5d478b; font-style: italic;">-- puis appliquer v1, v2 et v3 avec &lt;*&gt; :</span>
toutEnUn <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> Answer
toutEnUn <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>pure deepThought<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v1 <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v2 <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v3
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Et pour finir, sachez qu'il existe un petit sucre syntaxique</span>
<span style="color: #5d478b; font-style: italic;">-- pour &quot;(pure f) &lt;*&gt; x&quot; ; l'opérateur &lt;$&gt; :</span>
toutEnUn <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> Answer
toutEnUn' <span style="color: #339933; font-weight: bold;">=</span> deepThought <span style="color: #339933; font-weight: bold;">&lt;$&gt;</span> v1 <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v2 <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v3</pre></td></tr></table></div>

<p>Mais parce que le haskell détient bien plus de P-P-P-P-Puissance, intéressons nous à une autre instance de Applicative : Les listes.</p>
<h4>Premier plongeon avec les listes</h4>
<p>Comme nous l'avions vu la dernière fois, les listes sont un excellent exemple de foncteur. Mais pouvons nous en faire des foncteurs applicatifs? Il est facile de placer une valeur dans un contexte minimal : on définira <i>pure a = [a]</i>. En fait, il nous faudrait répondre à la question suivante : Si l'on a une liste de fonction [f1, f2, f3] et de valeurs [2, 3, 4], comment définir l'opérateur <i>&lt;*&gt;</i> ?</p>
<p>La solution retenue par haskell est on ne peut plus simple : on applique toutes les fonctions à toutes les valeurs. Voici la définition de l'instance :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code30'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81030"><td class="code" id="p810code30"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> Applicative <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">where</span>
    pure x <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span>
    fs <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> xs <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>f x <span style="color: #339933; font-weight: bold;">|</span> f <span style="color: #339933; font-weight: bold;">&lt;-</span> fs<span style="color: #339933; font-weight: bold;">,</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> xs<span style="color: green;">&#93;</span></pre></td></tr></table></div>

<p>Cela donne une façon très facile d'effectuer de nombreux calculs différents. C'est à dire : Vous disposez d'un ensemble de valeurs, et d'un ensemble de fonctions. Vous voulez connaitre TOUS les résultats possible. Il suffit alors d'appliquer la liste des fonctions sur la liste des valeurs avec l'opérateur <i>&lt;*&gt;</i>. Un exemple, en partie tiré de Learn You Haskell for Great Good est le parcoure d'un cavalier. Un cavalier est situé quelque part sur un échiquier infini, et vous voulez connaitre toute les positions où il peut se trouver apprès 5 coups. Il suffit décrire une fonction par déplacement possible et e les placer dans une liste, disons <i>[u1, u2, l1, l2, r1, r2, d1, d2]</i> et d'appliquer cette liste à la position d'origine dans un contexte <i>[(x, y)</i> ou encore <i>pure (x, y)</i>. La solution est donné par :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code31'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81031"><td class="code" id="p810code31"><pre class="haskell" style="font-family:monospace;">u1 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span>
u2 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span>
d1 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span>
d2 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span>
l1 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: green;">&#41;</span>
l2 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: green;">&#41;</span>
r1 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: green;">&#41;</span>
r2 <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: green;">&#41;</span>
&nbsp;
fctListe <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>u1<span style="color: #339933; font-weight: bold;">,</span> u2<span style="color: #339933; font-weight: bold;">,</span> d1<span style="color: #339933; font-weight: bold;">,</span> d2<span style="color: #339933; font-weight: bold;">,</span> l1<span style="color: #339933; font-weight: bold;">,</span> l2<span style="color: #339933; font-weight: bold;">,</span> r1<span style="color: #339933; font-weight: bold;">,</span> r2<span style="color: green;">&#93;</span>
origine <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span>c<span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
&nbsp;
etapeSuivante position <span style="color: #339933; font-weight: bold;">=</span> fctListe <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> position
&nbsp;
solution <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> etapeSuivante <span style="color: #339933; font-weight: bold;">.</span> etapeSuivante <span style="color: #339933; font-weight: bold;">.</span> etapeSuivante <span style="color: #339933; font-weight: bold;">.</span> etapeSuivante <span style="color: #339933; font-weight: bold;">.</span> etapeSuivante <span style="color: #339933; font-weight: bold;">$</span> origine <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<h4>En apnée : les foncteurs (-&gt;) r !</h4>
<p>On les avais cacher lors de la discussion des foncteurs, car ils sont difficile à cerné, leur intérêt n'est pas évident au premier abord, et leur construction est quelque peu... surprenante. Mais puisqu'ils sont utile comme foncteur applicatif, parlons en! Si la partie la plus abstraite vous échappe, aucune raison de vous inquiéter, l'idée est de survoler les notions pour avoir un aperçu, éveiller la curiosité et inciter à lire des livres/articles qui expliquent en détaille ce qui n'est ici que mentionner. Si tout cela vous intéresse, sautez à la section "Foncteurs applicatifs" de Learn You Haskell for Great Good!</p>
<p>L'opérateur -&#038;gt est un constructeur de type, à deux arguments. Vous lui donnez deux types, a et b, et il vous construiras le type "prend du a et retourne du b". On peut donc écrire <i>f :: a -> b</i> ou encore <i> f :: (-&gt;) a b</i>. Que signifie alors <i>(-&gt;) r</i> ? Cela signifie que l'on parle d'un constructeur de type à un argument, et que si vous lui donnez un type a, il désigneras alors les fonctions de type <i>a -> r</i> Si r désigne un type, on peut alors faire de <i>(->) r</i> une instance de functor où mapper une fonction de type <i>a->b</i> signifie l'appliquer au résultat de l'évaluation de la fonction de type <i>(-> r a</i>. Plus précisément :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code32'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81032"><td class="code" id="p810code32"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #cccc00; font-weight: bold;">Functor</span></a> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-&gt;</span><span style="color: green;">&#41;</span> r<span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f g <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> f <span style="color: green;">&#40;</span>g x<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>On peut se demander l’intérêt, puisque <i>(fmap f g) 42</i> se simplifie en <i> f . g $ 42</i> qui est bien plus lisible. Outre sa fantastique capacité à m'être votre esprit à rude épreuve, ce changement de point de vue devient très intéressant avec la classe applicative, puisqu'il donne la possibilité d'avoir un ensemble de paramètres.</p>
<p>Un exemple commenté :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code33'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81033"><td class="code" id="p810code33"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--Notre type &quot;paramètre&quot;. On aurais pu construire une sorte de grosse structure</span>
<span style="color: #5d478b; font-style: italic;">-- avec diverses informations.</span>
<span style="color: #5d478b; font-style: italic;">--Dans cette example, on se contenteras d'un nombre.</span>
<span style="color: #06c; font-weight: bold;">type</span> Param <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
&nbsp;
unEntier <span style="color: #339933; font-weight: bold;">::</span> Param <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
unEntier <span style="color: #339933; font-weight: bold;">=</span> pure <span style="color: red;">5</span>
&nbsp;
unAutreEntier <span style="color: #339933; font-weight: bold;">::</span> Param <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
unAutreEntier param <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">5</span> <span style="color: #339933; font-weight: bold;">+</span> param
&nbsp;
uneFonction <span style="color: #339933; font-weight: bold;">::</span> Param <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-</span> <span style="color: #339933; font-weight: bold;">&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>
uneFonction param arg <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> <span style="color: green;">&#40;</span>arg <span style="color: #339933; font-weight: bold;">+</span> param<span style="color: green;">&#41;</span>
&nbsp;
somme <span style="color: #339933; font-weight: bold;">=</span> pure <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;$&gt;</span> unEntier <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> unAutreEntier
affichage <span style="color: #339933; font-weight: bold;">=</span> uneFonction <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> somme
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Vaut 94 :</span>
evaluationDansUnCOntexte <span style="color: #339933; font-weight: bold;">=</span> affichage <span style="color: red;">42</span></pre></td></tr></table></div>

<h4>Quelques règles que null ne doit ignorer</h4>
<p>Comme on dit, <i>dura lex, sed lex</i>. Les foncteurs devaient respecter certaines règles, et il en est de même des foncteurs applicatifs. Une fois habitué aux foncteurs applicatifs, ces règles semblent découler du bon sens. Ce sont des invariants que DOIVENT respecter vos instances d'Applicative. Si vous ne les respectez pas, c'est que ce que vous voulez faire n'est pas un foncteur applicatif, et n'a donc aucune raison d’être instance d'Applicative.</p>
<p>Sans trop rentrer dans les détails, les voici, brièvement commentés :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code34'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81034"><td class="code" id="p810code34"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- Neutre :</span>
pure <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v <span style="color: #339933; font-weight: bold;">=</span> v
<span style="color: #5d478b; font-style: italic;">--Cela signifit qu'appliquer la fonction identité</span>
<span style="color: #5d478b; font-style: italic;">-- (id = (\x -&gt; x) ) a un élément v via &lt;*&gt; le laisse inchangé.</span>
<span style="color: #5d478b; font-style: italic;">--C'est une sorte de &quot;neutre à gauche&quot;.</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Composition :</span>
pure <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">.</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> u <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> v <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> w <span style="color: #339933; font-weight: bold;">=</span> u <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> <span style="color: green;">&#40;</span>v <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> w<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">--Cela signifie que composer les fonctions à l'intérieur de u et v</span>
<span style="color: #5d478b; font-style: italic;">-- via l'opérateur .  (C'est la partie pure (.) &lt;*&gt; u &lt;*&gt; v) revient à calculer u sur le résultat de v.</span>
<span style="color: #5d478b; font-style: italic;">--Comme on compare deux fonctions sur leur valeur, et que l'on parle de résultat, on doit introduire</span>
<span style="color: #5d478b; font-style: italic;">-- un certain mister w, et on vérifie que quelque soit ce w, on a bien que u calculer sur v calculé sur w donne bien le meme résultat que la composé (pure (.) &lt;*&gt; u &lt;*&gt; v) calculé sur w.</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Morphisme :</span>
pure f <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> pure x <span style="color: #339933; font-weight: bold;">=</span> pure <span style="color: green;">&#40;</span>f x<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">--Cette égalité garanti que : placer f dans un contexte minimal, placer x dans un contexte</span>
<span style="color: #5d478b; font-style: italic;">-- minimal, puis &quot;mouliner&quot; donne le même résultat que f x placé dans un contexte minimal. En quelque sorte,</span>
<span style="color: #5d478b; font-style: italic;">-- on transforme l'opérateur $ :: (a-&gt;b) -&gt; a -&gt; b en l'opérateur &lt;*&gt; :: f (a-&gt;b) -&gt; f a -&gt; f b.</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- ' Échange '</span>
u <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> pure y <span style="color: #339933; font-weight: bold;">=</span> pure <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">$</span> y<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;*&gt;</span> u
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- C'est une sorte de commutativité du pauvre. On ne peut pas vraiment échanger les arguments à droite et à gauche, car l'un est une fonction, l'autre une valeur. Mais on peut transformer une évaluation &quot;f y&quot; en &quot;$ f y &quot;, ce qui permet de changer l'ordre des arguments.</span></pre></td></tr></table></div>

<h3>Monades</h3>
<p>Les monades, c'est le cran au dessus. On ne veut plus seulement mapper des fonctions <i>f: a -> b</i> à l'intérieur d'un <i>Fonc a</i>, ni évaluer des fonctions <i>Fonc (a -> b)</i> dans un contexte <i> F a</i>. Maintenant, on dispose de fonctions qui travaillent sur une valeur, et produisent un résultat dans un contexte. Des fonctions e type <i>f :: a -> Fonc b</i>. Si l'on essayais de les mapper comme des foncteurs sur un <i>Fonc a</i>, on se retrouverais avec du <i>Fonc (Fonc b)</i>, et ce n'est pas du tout ce que l'on veut. Il nous faut donc une fonction capable de recoller ces "Fonc Fonc" en "Fonc". C'est ce que fournisse les monades.</p>
<p>Tout de suite, la classe monade :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code35'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81035"><td class="code" id="p810code35"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #cccc00; font-weight: bold;">Monad</span></a> m <span style="color: #06c; font-weight: bold;">where</span>
  <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;&gt;=</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> m a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> m b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> m b <span style="color: #5d478b; font-style: italic;">-- On l’appelle aussi &quot;bind&quot;</span>
  <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&gt;&gt;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> m a <span style="color: #339933; font-weight: bold;">-&gt;</span> m b <span style="color: #339933; font-weight: bold;">-&gt;</span> m b <span style="color: #5d478b; font-style: italic;">-- C'est une sorte d'opérateur de concaténation.</span>
<span style="color: #5d478b; font-style: italic;">-- &gt;&gt; Ignore le premier argument et renvoi la valeur du second.</span>
<span style="color: #5d478b; font-style: italic;">-- On veras plus tard qu'en fait, c'est utile, avec la monade IO.</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> m a <span style="color: #5d478b; font-style: italic;">-- C'est notre bon vieux pure, sous un autre nom.</span>
  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fail"><span style="font-weight: bold;">fail</span></a> <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> m a <span style="color: #5d478b; font-style: italic;">-- On ne l'utiliseras pas, et on en parleras pas.</span>
<span style="color: #5d478b; font-style: italic;">-- Sachez toute fois que ca renvoi moralement un &quot;contexte sans information&quot;.</span>
<span style="color: #5d478b; font-style: italic;">-- Par exemple une liste vide, un Nothing, etc.</span></pre></td></tr></table></div>

<p>Les deux opérateurs principaux sont bind et return. Voyons comment on pourrait, partant d'une monade, la faire instance d'Applicative :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code36'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81036"><td class="code" id="p810code36"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> Monade Fonc <span style="color: #06c; font-weight: bold;">where</span>
  pure <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a>
  <span style="color: #5d478b; font-style: italic;">-- L'astuce est de construire une fonction f' :: (a -&gt; m b) que l'on puisse utiliser à la place de f.</span>
  <span style="color: #5d478b; font-style: italic;">-- On la construit grace à &quot;pure . f&quot;.</span>
  <span style="color: #5d478b; font-style: italic;">-- Mais comme f est elle meme dans un contexte, il faut faire cette transformation dans le contexte.</span>
  mf <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">&lt;*&gt;</span><span style="color: green;">&#41;</span> mv <span style="color: #339933; font-weight: bold;">=</span> mf <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\f <span style="color: #339933; font-weight: bold;">-&gt;</span> mv <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a><span style="color: #339933; font-weight: bold;">.</span>f<span style="color: green;">&#41;</span>
<span style="color: #5d478b; font-style: italic;">-- On done mf à manger a la grosse fonction de droite. La grosse fonction de droite récupère la fonction f, la transforme en une f' par return.f. On donne donc la valeur v contenue dans mv à manger a la fonction f' grâce à &gt;&gt;=.</span></pre></td></tr></table></div>

<p>Bon, si vous avez suivi jusque là, soit vous connaissez déjà le haskell, soit vous êtes des sur-hommes. Voyons en pratique ce qu'apportent les monades, et pourquoi est-ce que bien utilisé, elles offres une nouvelle façon de résoudre certains problèmes bien connu du monde impératif.</p>
<h4>Être ou ne pas être?</h4>
<p>Dans un "vrai" programme, on n'a pas toujours une valeur a retourner pour une fonction. Que faire si l'on demande le premier élément d'une liste vide? Et si jamais on veut convertir une chaine en un nombre, qui par malheur n'est pas un nombre? En bref, comment gérer une erreur correspondant à l’absence d'un résultat?</p>
<p>La réponse est la monade maybe. Commençons par des fonctions qui renvoient peut-être une valeur :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code37'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81037"><td class="code" id="p810code37"><pre class="haskell" style="font-family:monospace;">maybeHead <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> a
maybeHead <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> Nothing
maybeHead <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a> : <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:tail"><span style="font-weight: bold;">tail</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> Just <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:head"><span style="font-weight: bold;">head</span></a>
&nbsp;
maybeList <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: green;">&#93;</span>
maybeList <span style="color: green;">&#40;</span>first<span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:last"><span style="font-weight: bold;">last</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> first <span style="color: #339933; font-weight: bold;">&lt;=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:last"><span style="font-weight: bold;">last</span></a> <span style="color: #06c; font-weight: bold;">then</span> <span style="color: green;">&#91;</span>first<span style="color: #339933; font-weight: bold;">..</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:last"><span style="font-weight: bold;">last</span></a><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">else</span> Nothing
&nbsp;
maybeRange <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a><span style="color: green;">&#41;</span>
maybeRange False <span style="color: #339933; font-weight: bold;">=</span> Nothing
maybeRange True <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: red;">23</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">42</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>On voudrais maintenant récupérer le premier élément de la liste pour les valeur donné par maybeRange, si la liste existe, bien sure. C'est la que les monades interviennent! Grâce au monades, on peut composer les deux fonctions, bien qu'un <i>Maybe [Int]</i> ne soit pas un <i>[Int]</i>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code38'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81038"><td class="code" id="p810code38"><pre class="haskell" style="font-family:monospace;">resultat <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
resultat choice <span style="color: #339933; font-weight: bold;">=</span> maybeRange choice <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> maybeList <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> maybeHead</pre></td></tr></table></div>

<p>Si l'une des étapes ne produit pas de résultat (un Nothing), alors l’absence de résultat seras propagé et on obtiendras un Nothing.</p>
<p>Cette méthode a de nombreux avantages par rapport aux deux vielles solutions bien connues :<br />
1) Le "code d'erreur", c'est à dire placer nullptr quand on pointeur n'existe pas, ou encore "-1" ou 0 pour signaler une erreur. L'inconvénient de cette méthode est d'obliger le développeur à vérifier chacune des valeurs de retour avec un if, généralement pour sortir de la fonction, souvent en retournant un nouveau code d’erreur pour signaler que le résultat produit n'est pas "vraiment" un résultat, mais une absence de résultat.</p>
<p>L'utilisation de la monade Maybe permet d'éviter ces testes répété. Si une seul des fonctions ne peut pas fournir de résultat, alors les applications suivantes seront toute ignoré et, bien entendu, ces fonctions ne seront pas évaluées, donc pas de cout en temps de calcul.</p>
<p>2) Les exceptions. Cela consiste à interrompre l'exécution normale du programme pour remonter a travers toute la pile d'appels, en espérant que quelqu'un seras assez gentil pour s'occuper de cette erreur. Cela a un cout en terme de performances, et doit être réserver pour les évènements exceptionnels. L'impossibilité de produire un résultat est rarement exceptionnel, c'est plutôt chose commune.</p>
<p>Le chainage de monade Maybe a l'avantage de ne pas déclencher un erreurs qui pourrait se perdre et aller jusqu'à interrompre le programme. Que les valeurs soient présente ou non, le comportement est toujours "simple" à prédire. Et plus un code est simple, moins il y a de risque qu'une erreurs s'introduise.</p>
<p>En règle générale, dès que le résultat peut ne pas être fournit, vous devriez utiliser la monade Maybe. Si parfois une certaine fonction f que vous voulez chainer produit toujours un résultat, alors vous pouvez la placer au milieu d'une chaine de >>= en écrivant <i>return.f</i>. Vous pouvez aussi une bonne vielle fmap, car toute les monades sont des foncteurs.</p>
<p>Nb : Peut-être avez vous besoin de conserver une information sur l'origine de l'erreur. Ceci est possible grâce à la monade Either.</p>
<p>Pour finir, voici l'instance de cette monade :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code39'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81039"><td class="code" id="p810code39"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #cccc00; font-weight: bold;">Monad</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <span style="color: #06c; font-weight: bold;">where</span>  
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> x <span style="color: #339933; font-weight: bold;">=</span> Just x  
        Nothing <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f <span style="color: #339933; font-weight: bold;">=</span> Nothing  
        Just x <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f  <span style="color: #339933; font-weight: bold;">=</span> f x  
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fail"><span style="font-weight: bold;">fail</span></a> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> Nothing</pre></td></tr></table></div>

<h4>Un calcul pas très déterministe</h4>
<p>Nous avions vu comment les listes comme foncteurs applicatifs permettent de résoudre élégamment la question du déplacement d'un cavalier. Mais dans un échiquier fini, on ne savais pas trop comment gérer les bords.</p>
<p>Les listes, vu comme monade, nous permettent de combiner des fonctions de type <i> a -> [b]</i>. L'idée est que vous disposez de diverse fonctions qui prennent une valeur, et produise divers résultats possible. Vous voulez alors appliquer des fonctions sur chacun de ces résultats. On peut donc parler de calcul non-déterministe : une valeur donne plusieurs résultats possible.</p>
<p>On peut donc réaliser un remake du cavalier, en se servant de ce calcul non déterministe, puisqu'à partir d'une position, on a diverses positions possibles</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code40'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81040"><td class="code" id="p810code40"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--Quelques types pour plus de lisibilité,</span>
<span style="color: #5d478b; font-style: italic;">-- histoire de rappeler que les types sont</span>
<span style="color: #5d478b; font-style: italic;">-- aussi là pour fournir des informations sémantiques.</span>
<span style="color: #06c; font-weight: bold;">type</span> Ligne <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
<span style="color: #06c; font-weight: bold;">type</span> Collone <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
<span style="color: #06c; font-weight: bold;">type</span> Position <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>Ligne<span style="color: #339933; font-weight: bold;">,</span> Collone<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Fonction utilisé pour ne garder que les positions dans l'échiquier</span>
dansLechiquier <span style="color: #339933; font-weight: bold;">::</span> Position <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a>
dansLechiquier <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> l `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span style="font-weight: bold;">elem</span></a>` <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span>8<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">&amp;&amp;</span> c `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:elem"><span style="font-weight: bold;">elem</span></a>` <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span>8<span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Produit une liste des positions possibles</span>
deplacerCavalier <span style="color: #339933; font-weight: bold;">::</span> Position <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>Position<span style="color: green;">&#93;</span>
deplacerCavalier <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> c<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:filter"><span style="font-weight: bold;">filter</span></a> dansLechiquier liste
  <span style="color: #06c; font-weight: bold;">where</span> liste <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span> 
                <span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span>c<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">2</span><span style="color: green;">&#41;</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Donne la liste des positions possible du cavalier, partant de (4, 5), et apprès trois déplacements.</span>
resultat <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">5</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> deplacerCavalier <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> deplacerCavalier <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> deplacerCavalier</pre></td></tr></table></div>

<p>En fait, une autre façon de faire serais de mapper la fonction deplacerCavalier dans la liste de positions, produisant un "[[Position]]", puis d'appeler concat sur cette liste, la recollant en une "[Position]". C'est d'ailleurs se que fait l'opérateur >>= !</p>
<p>L'utilisation des listes sous leur forme de monade n'est pas limité à cette exemple. Je peux mentionner un exemple qui vous paraitras plus concret. Disons que vous voulez enregistrer une image, et que vous disposez d'une fonction qui vous donne les couleurs r, g, b, a sous la forme d'une liste de nombres, c'est à dire <i>getPixel (x, y) :: [Word8]</i> (Word8 est un nombre codé sur 8 bits). Sachant que pour enregistrer l'image, vous devez fournir un tableau, qui peut être très facilement construit à partir de la liste des valeurs qu'il vas contenir. (Le compilateur est très malins, et le programme compilé ne s’amusera pas à produire une liste, la construire en mémoire, puis la placer dans le tableau. Pas d'inquiétude, le compilateur est très douer à ce niveau.)<br />
La monade [] permettent d'écrire en une ligne, de façon très élégante, la création d'un tableau où les valeurs sont bien la succession des valeurs de getPixel calculé à chaque coordonné. Biensur, on aurais pu utiliser concat et map, mais c'est justement ce que fait l'opérateur bind. Tout ça pour ire que les listes vue comme monade ne sont pas un gadget, mais bien un outil.</p>
<p>Voici la définission de l'instance pour les curieux :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code41'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81041"><td class="code" id="p810code41"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Monad"><span style="color: #cccc00; font-weight: bold;">Monad</span></a> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #06c; font-weight: bold;">where</span>  
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> x <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span>  
        xs <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:concat"><span style="font-weight: bold;">concat</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> f xs<span style="color: green;">&#41;</span>  
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fail"><span style="font-weight: bold;">fail</span></a> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span></pre></td></tr></table></div>

<h4>Dou? Doo? Do, c'est le gout!</h4>
<p>La notation do est une sorte de super sucre syntaxique. Seulement, les monades sont tellement amère que vous aurez vraiment besoin de ce sucre, je vous l'assure.<br />
La notation do permet décrire facilement le chainage d'actions, et le fait de récupèrer des valeurs dans un contexte.</p>
<p>Considèrons léxample suivant tiré de Learn You Haskell for Great Good (Non, je n'ai pas la moindre part chez eux.) :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code42'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81042"><td class="code" id="p810code42"><pre class="haskell" style="font-family:monospace;">    foo <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>  
    foo <span style="color: #339933; font-weight: bold;">=</span> Just <span style="color: red;">3</span>   <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> 
          Just <span style="background-color: #3cb371;">&quot;!&quot;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\y <span style="color: #339933; font-weight: bold;">-&gt;</span> 
          Just <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> x <span style="color: #339933; font-weight: bold;">++</span> y<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>On récupère deux valeurs de monades à travers x et y, puis l'on place le resultat de show x ++ y dans un contexte. C'est lours a écrire, demande l'imbrication de fonctions... Avec la notation do, cela evient :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code43'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81043"><td class="code" id="p810code43"><pre class="haskell" style="font-family:monospace;">foo <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>  
foo <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>  
        x <span style="color: #339933; font-weight: bold;">&lt;-</span> Just <span style="color: red;">3</span>  
        y <span style="color: #339933; font-weight: bold;">&lt;-</span> Just <span style="background-color: #3cb371;">&quot;!&quot;</span>  
        Just <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> x <span style="color: #339933; font-weight: bold;">++</span> y<span style="color: green;">&#41;</span>  
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Ou encore :</span>
foo <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>  
foo <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>  
        x <span style="color: #339933; font-weight: bold;">&lt;-</span> Just <span style="color: red;">3</span>  
        y <span style="color: #339933; font-weight: bold;">&lt;-</span> Just <span style="background-color: #3cb371;">&quot;!&quot;</span>  
        <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> x <span style="color: #339933; font-weight: bold;">++</span> y<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Dans les deux premières lignes, on récupère x et y depuis Just 3 et Just "!", puis on fait notre traitement.</p>
<p>Hey! Mais ca resemble a des listes en compréhension tout ca! Revoyons l'exemple précédent avec des listes :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code44'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81044"><td class="code" id="p810code44"><pre class="haskell" style="font-family:monospace;">foo <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>  
foo <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">3</span><span style="color: green;">&#93;</span>   <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> 
          <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;.&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;!&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;?&quot;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\y <span style="color: #339933; font-weight: bold;">-&gt;</span> 
          <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> x <span style="color: #339933; font-weight: bold;">++</span> y<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Le résultat produit est toute les facons de coller l'un des signes de pontuations après l'un des nombres. C'est exactement la même chose que :</p>
<pre lang="haskell" colla"+">
foo :: Maybe String  
foo = do
          x <- [1, 2, 3]
          y <- [".", "!", "?"]
          return (show x ++ y)

-- Ou encore :
foo = [show x ++ y | x <- [1, 2, 3] | y <- [".", "!", "?"]]
</pre>
<p>Voilà donc l'explication des liste en compréhention! Et oui, il nous auras fallut arriver jusqu'ici pour pouvoir enfin dire ce qu'est une liste en compréhension. C'est simplement une notation spécifique au liste d'un bloque do. C'est donc de la manipulation de monade que vous faites à chaque fois que vous écrivez une liste en compréhension. Si c'est si pratique avec les listes, vous vous doutez bien que pouvoir le faire avec divers structures, comme des arbres, est tout aussi pratique.</p>
<p>Vous vous demandez alors comment ajouter les conditions, comme dans <i>[x^2 | x <- [1..20], x `mod` 2 == 0]</i> ? Et bien vous pouvez utilisez la fonction <i>guard</i>, qui produiras une liste vide si la condition n'est pas vérifiée :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code45'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81045"><td class="code" id="p810code45"><pre class="haskell" style="font-family:monospace;">foo <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">do</span>
      x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span>20<span style="color: green;">&#93;</span>
      guard <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> x `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mod"><span style="font-weight: bold;">mod</span></a>` <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span>
      <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> x<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span></pre></td></tr></table></div>

<h4>Le retour de (->) r</h4>
<p>Nous avions utilié le foncteur applicatif (->) r pour représenter des caluls qui dépendent d'un contexte. On savais donc appliquer des fonctions <i>r -> (a->b)</i> sur des valeurs <i>r -> a</i>. Seulement, il est plus commun de partir d'une valeur, et produire un resultat qui dépend du contexte. On voudrais donc une monade, pour pouvoir combiner des fonctions de type <i>a -> (r -> b)</i> (Les parentaises sont là pour faire ressortir que l'on considère (->) r comme un foncteur/ une monade, mais biensur facultatives).</p>
<p>Comme (->) r est l'une des monades les plus abstraites, regardons un example concret, où le contexte est la position d'une caméra dans un raytracer.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code46'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81046"><td class="code" id="p810code46"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--On définit une caméra.</span>
<span style="color: #5d478b; font-style: italic;">--Une caméra contient la position depuis la quelle</span>
<span style="color: #5d478b; font-style: italic;">-- les rayons sont lancé, la distance à la quel se trouve</span>
<span style="color: #5d478b; font-style: italic;">-- le plan, et la taille de celui ci.</span>
<span style="color: #06c; font-weight: bold;">type</span> Position <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">type</span> Direction <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a><span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">type</span> Distance <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Double"><span style="color: #cccc00; font-weight: bold;">Double</span></a>
<span style="color: #06c; font-weight: bold;">type</span> Coordonnee <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
<span style="color: #06c; font-weight: bold;">type</span> Largeur <span style="color: #339933; font-weight: bold;">=</span> Coordonnee
<span style="color: #06c; font-weight: bold;">type</span> Hauteur <span style="color: #339933; font-weight: bold;">=</span> Coordonnee
<span style="color: #06c; font-weight: bold;">type</span> Plan <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>Largeur<span style="color: #339933; font-weight: bold;">,</span> Hauteur<span style="color: green;">&#41;</span>
<span style="color: #06c; font-weight: bold;">data</span> Camera <span style="color: #339933; font-weight: bold;">=</span> Camera Position Distance Plan
&nbsp;
getRay <span style="color: #339933; font-weight: bold;">::</span> Cooronnee <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Camera <span style="color: #339933; font-weight: bold;">-&gt;</span> Direction<span style="color: green;">&#41;</span>
getRay <span style="color: green;">&#40;</span>i<span style="color: #339933; font-weight: bold;">,</span> j<span style="color: green;">&#41;</span> cam <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>Camera <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> y<span style="color: #339933; font-weight: bold;">,</span> z<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> cam <span style="color: #06c; font-weight: bold;">in</span> normalize <span style="color: green;">&#40;</span>i <span style="color: #339933; font-weight: bold;">-</span> x<span style="color: #339933; font-weight: bold;">,</span> j <span style="color: #339933; font-weight: bold;">-</span> y<span style="color: #339933; font-weight: bold;">,</span> <span style="color: #339933; font-weight: bold;">-</span>z<span style="color: green;">&#41;</span>
  <span style="color: #06c; font-weight: bold;">where</span>
    normalize <span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">,</span> y<span style="color: #339933; font-weight: bold;">,</span> z<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> norme <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:sqrt"><span style="font-weight: bold;">sqrt</span></a><span style="color: green;">&#40;</span>x<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">+</span> y<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">+</span> z<span style="color: #339933; font-weight: bold;">^</span><span style="color: red;">2</span><span style="color: green;">&#41;</span> <span style="color: #06c; font-weight: bold;">in</span> <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">/</span> norm<span style="color: #339933; font-weight: bold;">,</span> y <span style="color: #339933; font-weight: bold;">/</span> norm<span style="color: #339933; font-weight: bold;">,</span> z <span style="color: #339933; font-weight: bold;">/</span> norm<span style="color: green;">&#41;</span>
&nbsp;
rayTraceScene <span style="color: #339933; font-weight: bold;">::</span> Scene <span style="color: #339933; font-weight: bold;">-&gt;</span> Direction <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Camera <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>Object<span style="color: #339933; font-weight: bold;">,</span> Distance<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
rayTraceScene <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #5d478b; font-style: italic;">-- Imagineons que l'on fait le necessaire pour raytracer une scène.</span>
&nbsp;
computeColor <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>Object<span style="color: #339933; font-weight: bold;">,</span> Distance<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> Camera <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>Word8<span style="color: green;">&#93;</span>
computeColor <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #5d478b; font-style: italic;">-- Calcule la couleur en tenant compte de l'angle de la caméra, etc.</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Pour getPixel, on préfèreras souvant spécialiser dabord la Caméra,</span>
<span style="color: #5d478b; font-style: italic;">-- puis appeler cette spécialisation sur toute les coordonnées de l'image.</span>
<span style="color: #5d478b; font-style: italic;">-- C'est pourquoi le type est &quot;Camera -&gt; Coordonnee -&gt; [Word8]&quot; plutot</span>
<span style="color: #5d478b; font-style: italic;">-- que &quot;Coordonnee -&gt; Camera -&gt; [Word8]&quot;. On perd donc la possibiliter</span>
<span style="color: #5d478b; font-style: italic;">-- d'utiliser getPixel avec la monade &quot;(-&gt;) Camera&quot;.</span>
getPixel <span style="color: #339933; font-weight: bold;">::</span> Camera <span style="color: #339933; font-weight: bold;">-&gt;</span> Coordonee <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>Word8<span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
getPixel cam coords <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> coords <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> getRay <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>rayTraceScene uneScene<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> computeColor<span style="color: green;">&#41;</span> cam</pre></td></tr></table></div>

<h5>Point culture</h5>
<p>Comprenez bien que les monades ne sont pas indispensable, et que l'on fesait des monade avant la création de la classe Monade. C'est simplement de nouveaux outils à votre disposition pour résoudre des problèmes, et ils permettent parfois de résoudre certains problèmes de façon très élégante et concise (ce qui est un escellent point pour l'évolutivité du code).</p>
<p>Sachez que les monades aussi doivent respecter certaines règles, tous comme les foncteurs et les foncteurs applicatifs. Cela assure qu'une monade seras bien un foncteur (applicatif), de la façon décrite plus haut. Les voici :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code47'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81047"><td class="code" id="p810code47"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- Neutre à gauche</span>
<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> a <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f <span style="color: #339933; font-weight: bold;">=</span> f a
<span style="color: #5d478b; font-style: italic;">-- Neutre à droite</span>
m <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:return"><span style="font-weight: bold;">return</span></a> <span style="color: #339933; font-weight: bold;">=</span> m
<span style="color: #5d478b; font-style: italic;">-- Associativité</span>
<span style="color: green;">&#40;</span>m <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> f<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> g <span style="color: #339933; font-weight: bold;">=</span> m <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">-&gt;</span> f x <span style="color: #339933; font-weight: bold;">&gt;&gt;=</span> g<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Vous trouverez des liens dans les références pour plus de détails.</p>
<p>On n'a pas aborder la monade IO. Cette monade permet de ramener les actions propre a l'impératif, les "effets de bord", dans le langage haskell. L'astuce diabolique est la suivante : Puisque qu'un programme haskell ne peut produire d'effet de bord, un programme haskell décriras comment composer, jusxtaposer, et transformer les résultats produits par des programmes impératif. Utiliser la monade IO, c'est jouer avec la composition et la juxtaposition de programmes. C'est alors que l'opérateur ">>" prend tout son sens! Il permet de juxtaposer deux programmes impératifs et ne retenir le resultat que du second, par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p810code48'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p81048"><td class="code" id="p810code48"><pre class="haskell" style="font-family:monospace;">afficherMessage <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> <span style="background-color: #3cb371;">&quot;Bonjour,&quot;</span> <span style="color: #339933; font-weight: bold;">&gt;&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:putStrLn"><span style="font-weight: bold;">putStrLn</span></a> <span style="background-color: #3cb371;">&quot;monde!&quot;</span></pre></td></tr></table></div>

<p>.</p>
<p>En dire plus sur cette monade n'a d'intéret que pour les personne voulant écrire des programmes en haskell. Si c'est votre cas, je vous invite à lire, au choix, Learn You Haskell for Great Good ou (plus technique et plus ... "professionnel") Real World Haskell, chez O'Reilly.</p>
<h4>Références :</h4>
<p>La loi des monades : <a href="http://www.haskell.org/haskellwiki/Monad_laws">http://www.haskell.org/haskellwiki/Monad_laws</a><br />
Either - <a href="http://hackage.haskell.org/packages/archive/category-extras/0.53.1/doc/html/Control-Monad-Either.html">http://hackage.haskell.org/packages/archive/category-extras/0.53.1/doc/html/Control-Monad-Either.html</a><br />
Hoogle (moteur de recherche, très efficace pour rechercher une fonction à partir de son type) - <a href="http://www.haskell.org/hoogle/">http://www.haskell.org/hoogle/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zenol.fr/site/2013/05/03/le-haskell-un-langage-au-label-pure-troisieme-partie/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Le Haskell, un langage au label pure. Seconde partie.</title>
		<link>http://zenol.fr/site/2013/04/26/le-haskell-un-langage-au-label-pure-seconde-partie/</link>
		<comments>http://zenol.fr/site/2013/04/26/le-haskell-un-langage-au-label-pure-seconde-partie/#comments</comments>
		<pubDate>Thu, 25 Apr 2013 22:00:47 +0000</pubDate>
		<dc:creator>Zenol</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Useful]]></category>
		<category><![CDATA[compréhension]]></category>
		<category><![CDATA[data-driven]]></category>
		<category><![CDATA[foncteurs]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[listes]]></category>

		<guid isPermaLink="false">http://zenol.fr/site/?p=754</guid>
		<description><![CDATA[Nous continuons de découvrir des paysages fonctionnels à travers le Haskell. Cette fois, nous nous éloignons des généralités et rentrons dans le vif du sujet en nous intéressant à des aspects plus propre au haskell (bien que d'autres langages fonctionnels implémentent des fonctionnalités similaires). La première partie est disponible ici. La troisième là. 2 - <a href='http://zenol.fr/site/2013/04/26/le-haskell-un-langage-au-label-pure-seconde-partie/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Nous continuons de découvrir des paysages fonctionnels à travers le Haskell. Cette fois, nous nous éloignons des généralités et rentrons dans le vif du sujet en nous intéressant à des aspects plus propre au haskell (bien que d'autres langages fonctionnels implémentent des fonctionnalités similaires).</p>
<p>La <a href="http://zenol.fr/site/2013/04/16/le-haskell-un-language-au-label-pure-13/" title="Le Haskell, un langage au label pure. Première partie.">première partie est disponible ici</a>. La <a href="http://zenol.fr/site/2013/05/03/le-haskell-un-langage-au-label-pure-troisieme-partie/" title="Le Haskell, un langage au label pure. Troisième partie.">troisième là</a>.</p>
<h2>2 - La force de haskell</h2>
<h3>Les listes infinies (et listes en compréhension)</h3>
<p>Les listes infinies sont une des nombreuses possibilités offertes par un langage paresseux. C'est souvent d'elles dont on entend le plus parler pour présenter le haskell, alors qu'il ne s'agit pourtant que d'une fonctionnalité original parmi tant d'autres. Cela est surement du au fait que la notion d'infinie éveille la curiosité des développeurs habitué à un monde impératif où les tableaux, les listes, et toute structure mémoire est finie.</p>
<p>Le principe est relativement simple : vous définissez une liste, que ce soit de façon récursive ou simplement par compréhension (on vas voir ce que cela signifie dans quelques lignes), puis seulement les éléments de la liste dont le programme auras besoin seront évalué. Les autres ne seront jamais calculés.</p>
<p>Comment faire de telles listes? La façon la plus simple est la définition de liste par compréhension, c'est à dire une définition de la forme "L'ensemble des f(trucs) pour les quels P(truc) est vraie". Où f est une formule sur "trucs" et où P est une proposition, disons une fonction qui renvoie True ou False.</p>
<p>Par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code59'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75459"><td class="code" id="p754code59"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- &quot;x &lt;- l&quot; signifit &quot;pour x se baladant dans la liste l&quot;</span>
<span style="color: #5d478b; font-style: italic;">-- l1 = [1, 2, 3, 4, 5]</span>
l1 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">5</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
<span style="color: #5d478b; font-style: italic;">-- L'opérateur c/c++ != s'écrit /= </span>
<span style="color: #5d478b; font-style: italic;">-- l2 = [1, 2, 4, 5]</span>
l2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">5</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> x <span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">3</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- l3 = [2, 4, 8, 16, 32]</span>
l2 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">^</span>x <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">5</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- l4 = [2, 4, 16, 32]</span>
l4 <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">^</span>x <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">4</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: red;">5</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> x <span style="color: #339933; font-weight: bold;">/=</span> <span style="color: red;">3</span><span style="color: green;">&#93;</span></pre></td></tr></table></div>

<p>En fait, c'est une sorte de sucre syntaxique. Le soucis, c'est que ce que cache les listes en compréhension ne seras aborder que dans la troisième partie. On se contenteras donc de la signification et de la façon dont on l'utilise.</p>
<p>Pour en revenir a nos listes en compréhension infinie, on peut penser à "L'ensemble des entiers qui sont paire" par exemple. Voici quelques listes infinies :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code60'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75460"><td class="code" id="p754code60"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--La liste de tous les entiers à partir de 42 peut s'écrire [42..]</span>
liste<span style="color: #339933; font-weight: bold;">_</span>infinie<span style="color: #339933; font-weight: bold;">_</span>des<span style="color: #339933; font-weight: bold;">_</span>entiers <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">--Pour calculer x % 2 (c/c++), on écrit &quot;x `mod` 2&quot;, ou encore &quot;mod x 2&quot;.</span>
&nbsp;
liste<span style="color: #339933; font-weight: bold;">_</span>des<span style="color: #339933; font-weight: bold;">_</span>entiers<span style="color: #339933; font-weight: bold;">_</span>paires <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">*</span> x <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
<span style="color: #5d478b; font-style: italic;">-- On peut rajouter des conditions séparé par des virgules</span>
liste<span style="color: #339933; font-weight: bold;">_</span>des<span style="color: #339933; font-weight: bold;">_</span>entiers<span style="color: #339933; font-weight: bold;">_</span>paires' <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> x `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:mod"><span style="font-weight: bold;">mod</span></a>` <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span><span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- On peut utiliser plusieurs variables se baladant dans différentes listes :</span>
liste<span style="color: #339933; font-weight: bold;">_</span>de<span style="color: #339933; font-weight: bold;">_</span>produit <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span>x <span style="color: #339933; font-weight: bold;">*</span> y <span style="color: #339933; font-weight: bold;">|</span> x <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span>5<span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> y <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span></pre></td></tr></table></div>

<p>Une autre façon de définir une liste est d'utiliser la récusivité. Voici quelques exemples.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code61'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75461"><td class="code" id="p754code61"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">--&quot;map f l&quot; applique la fonction f sur chaque élément de la liste l</span>
<span style="color: #5d478b; font-style: italic;">-- [a, b] est du sucre syntaxique pour a : b : [].</span>
entiers <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">1</span> : <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map"><span style="font-weight: bold;">map</span></a> <span style="color: green;">&#40;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> entiers<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Regardons se qui se passe si l'on demande les trois premiers éléments de la liste, ce qui se fait avec "take 3 l". D'abord, il lit "1 :" et connais donc le premier élément. Pour avoir le second, il lit (map (1+) entiers). Il applique donc map sur la liste, mais de façon paresseuse, c'est a dire qu'en ne calculant que l'application sur le premier terme. Il obtient donc (1+)(1) = 2. puis, pour avoir le troisième élément, il doit appliquer (1+) au deuxième élément de la liste entier. Ça tombe bien, on vient de le calculer, c'est 2. On a donc pour troisième élément (1+)(2) = 3.</p>
<p>De cette façon, on aurais aussi pu définir la liste des entiers pairs, la liste des nombres de fibonacci (même si on ne voit pas bien l’intérêt dans un programme), ou la liste des nombres premiers (plus difficile).</p>
<p>Bon, c'est très beau tout ça, mais est-ce que ça sert vraiment (parce que, faire des listes infinies pour faire des listes infinies...)? Oui, ça sert, et voici un exemple simple et concret. Disons que vous participez au Google Code Jam. Vous devez fournir des réponse en respectant un certain format. Plus précisément, on vous donne une entrée de n éléments à traiter, par exemple n lignes contenant chacune un nombre, et vous devez fournir le résultat de votre traitement sous la forme "Case #i: <vos données>\n". En C++, on aurais itéré sur la liste de résultat (ou directement l'entrée) et provoqué l'écriture de "Case #i" sur la sortie standard juste avant celle de vos données. En haskell, on peut (doit?) faire ça différemment(J'ai trouvé cette idée sur une vidéo <a href="http://www.youtube.com/watch?v=_tgv3HVgOMc">youtube</a>) :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code62'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75462"><td class="code" id="p754code62"><pre class="haskell" style="font-family:monospace;">boilerPlate <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a><span style="color: green;">&#93;</span>
boilerPlate <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;Case #&quot;</span> <span style="color: #339933; font-weight: bold;">++</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:show"><span style="font-weight: bold;">show</span></a> n <span style="color: #339933; font-weight: bold;">++</span> <span style="background-color: #3cb371;">&quot;: &quot;</span> <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&lt;-</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">..</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
&nbsp;
standardOutput <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a><span style="color: green;">&#93;</span>
<span style="color: #5d478b; font-style: italic;">-- zipWith f l1 l2 recole les deux listes l1 et l2 en utilisant la fonction f sur les éléments de chacune des deux listes.</span>
<span style="color: #5d478b; font-style: italic;">-- La liste produite par zipWith fait la longueur de la plus courte.</span>
<span style="color: #5d478b; font-style: italic;">-- Par exemple, zipWith (+) [1, 2, 3] [1, 1, 1, 1, 1] = [1+1, 2+1, 3+1]</span>
standardOutput <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:zipWith"><span style="font-weight: bold;">zipWith</span></a> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">++</span><span style="color: green;">&#41;</span> boilerPlate
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Une fois votre sortie sous la forme d'une liste de String, il vous suffit de la donner à standardOutput pour obtenir le formatage attendu</span></pre></td></tr></table></div>

<p>On voit ici que la liste infinie boilerPlate contient tous les formatages possibles. Bien entendu, à chaque exécution, il n'y auras qu'un nombre finie d'entrées et de sorties, donc une partie finie de la liste qui seras utilisé.</p>
<h3>Data-driven programming</h3>
<p>En haskell, manipuler des listes ou des structures similaires est chose courante, et il y a un bon nombre de fonction dédiés. Par exemple map f l qui permet d'appliquer f sur les éléments de l, zip et zipWith qui permettent de souder deux liste en une unique (soit sous forme de liste de couple, soit en utilisant la fonction que vous fournissez). Mais ce n'est pas tout. Nous ne parlerons par exemple pas de foldr et foldl qui permettent a partir d'une liste d'éléments de l'écraser en un nouvel élément grâce à une fonction que vous fournissez (leurs usages est multiple. On peut implémenter facilement la somme/produit des éléments d'une liste, la conversion d'une liste de mot en une seul chaine de caractère, la transformation d'une liste en un arbre binaire de recherche, etc).</p>
<p>Vous devriez avoir remarquer qu'en haskell, on aime bien concevoir de petites fonctions travaillant sur un élément de type a, et produisant un élément de type b, puis appliquer ces petites fonctions sur les éléments de structures de donnés plus ou moins complexe. Cela a de nombreux avantages :<br />
-Il est plus simple de concevoir une fonction de type Int->String qu'une fonction de type [Int] -> [(String, Int)], par exemple.<br />
-On est plus générique ; Si l'on sais transformer du a en b, alors on sais transformer du [a] en [b] et du Tree a en Tree b (où Tree a est un arbre binaire où chaque noeud contient un élément de type a).<br />
-En cas de changement de structure mémoire, par exemple pour des raisons de performances, on minimise l'impacte sur le code à modifier. Si l'on souhaite passer de liste à des arbres, seul le traitement effectué sur les listes devras être ré-écrit pour les arbres, mais rien d'autre.</p>
<p>On se retrouve donc à se concentrer plus sur les types qu'on manipule que la façon dont on les manipule. C'est à dire que l'on dispose d'un nombre important de façon simple de transformer certaines données en d'autres, et les points cruciaux sont alors de:<br />
-Bien choisir la façon dont seront représentés les données traitées<br />
-Trouver les structures de données intermédiaires au cours du déroulement d'un algorithme.</p>
<p>Si l'on sais exactement de quels donnés l'on part, et quels donnés on droit produire, il ne reste alors plus qu'à décrire les transformations nécessaire pour passer de l'une à l'autre. Par exemple, faire un programme de reconnaissance de caractère, c'est simplement transformer une image en une chaine de caractère. Pour peut que l'on parvienne à réduire l'écart entre les structures de donnés considérés (par exemple une image, puis une liste de rectangles de pixels représentant des lignes, puis une liste de liste de rectangles de pixels représentant des mots) il devient très simple de décrire la transformation (on a réussi a réduire le problème a savoir découper les lignes, découper les mots, découper les lettres puis reconnaitre une lettre).</p>
<p>Si ce type de raisonnement peut conduire à du code catastrophique dans un langage objet, en haskell c'est très certainement l'une des routes les plus sur. Tout, dans le langage, s'adapte parfaitement à cette conduite, et particulièrement le système de types et de classes.</p>
<p>En C++ un type représente un ensemble de fonctionnalités. En haskell un type n'est rien d'autre qu'un ensemble possible de valeur. On peut tout de fois préciser qu'un type peut être manipulé d'une certaine façon (ordonné, comparé, affiché...). On pourrait dire que deux éléments d'un type que l'on a construit peuvent être affiché, ou encore comparé, en le faisant instance d'une classe. Cela correspond a la surcharge de fonctions/opérateurs du C++ ; après avoir déclarer une structure <i>struct St;</i>, on peut surcharger l'opérateur de comparaison pour ce nouveau type par <i>bool operator < (St &#038;a, St &#038;b);</i>. Vient alors l'idée de ce que doit être quelque chose "d'affichable" ou de "comparable". C'est un type pour le quel on doit avoir certaines fonctions de définies. En java, il y à la notion d'interface, où l'on veut imposer l’existence de certaines méthodes. Malheureusement, on ne peut le faire que lors de la déclaration d'un type, et l'implémentation de cette interface est faite "à l'intérieur" du type. En haskell, pas de fonctions membres, mais des fonctions tout cours. Ce qui fait que n'importe quel type pourra devenir instance de n'importe quel classe (terme haskell désignant un jeu de fonctions) et à l'instant où vous le désirerais. Le sens d'une classe en haskell est donc plus proche de celle de la théorie des ensembles (une collection d'objets [ici de types] qui respectent certaines conditions [ici, pouvoir être comparé, affiché...]) ou si vous voulait vraiment une analogie en langage impératif, des interfaces du java. Ce n'est certainement pas celui des classes C++.</p>
<p>Quand on prétend qu'un type est instance d'une classe, on doit fournir l'implémentation des fonctions de la classe, mais pas nécessairement toutes. Les classes fournissent souvent une implémentation des fonctions, souvent en terme récursif. Par exemple on pourrais définir a /= b à partir de == et a == b à partir de /=. De cette façon, il suffit de définir l'une des deux fonctions pour pouvoir immédiatement utiliser les deux opérateurs. (Le compilateur s'assurant que cela n'engendre pas de surcout en terme de performances).</p>
<p>Voici par exemple la classe Eq, décrivant deux objets pouvant être comparé :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code63'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75463"><td class="code" id="p754code63"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span>  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span style="color: #cccc00; font-weight: bold;">Eq</span></a> a  <span style="color: #06c; font-weight: bold;">where</span>
<span style="color: #06c; font-weight: bold;">class</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span style="color: #cccc00; font-weight: bold;">Eq</span></a> a <span style="color: #06c; font-weight: bold;">where</span>
    <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">==</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a>
    <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/=</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Bool"><span style="color: #cccc00; font-weight: bold;">Bool</span></a>
    x <span style="color: #339933; font-weight: bold;">==</span> y <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">/=</span> y<span style="color: green;">&#41;</span>
    x <span style="color: #339933; font-weight: bold;">/=</span> y <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:not"><span style="font-weight: bold;">not</span></a> <span style="color: green;">&#40;</span>x <span style="color: #339933; font-weight: bold;">==</span> y<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>On rend un type instance d'une classe de la façon suivante (exemple honteusement tirer de "Learn you haskell for a great good" :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code64'); return false;">View Code</a> HASKEL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75464"><td class="code" id="p754code64"><pre class="haskel" style="font-family:monospace;">data TrafficLight = Red | Yellow | Green
&nbsp;
instance Eq TrafficLight where
    Red == Red = True
    Green == Green = True
    Yellow == Yellow = True
    _ == _ = False</pre></td></tr></table></div>

<p>On définit l'égalité grace au filtrage par motif, en définissant seulement l'opérateur ==.</p>
<p>Bon, à vrais dire, pour les classes comme Eq (comparable) et Show (affichable), on peut laisser haskell s'en charger comme un grand :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code65'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75465"><td class="code" id="p754code65"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> TrafficLight <span style="color: #339933; font-weight: bold;">=</span> Red <span style="color: #339933; font-weight: bold;">|</span> Yellow <span style="color: #339933; font-weight: bold;">|</span> Green <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Show"><span style="color: #cccc00; font-weight: bold;">Show</span></a><span style="color: #339933; font-weight: bold;">,</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Eq"><span style="color: #cccc00; font-weight: bold;">Eq</span></a><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>En fait, quand on parleras de foncteurs, foncteurs applicatifs ou monades, on parleras de type qui sont instance respectivement de Functor (Prelude.Functor), de Applicative (Control.Applicative) et de Monad (Control.Monad).</p>
<h3>Les foncteurs</h3>
<p>Les foncteurs (à nouveau, le terme est à prendre au sens e la théorie des catégories) constituent le point de départ vers les monades. Faisons un petit détour par les maths et définissons ce qu'est un foncteur (il n'est pas nécessaire de comprendre ce paragraphe pour la suite, c'est pour la culture).</p>
<p><b>La catégorie des types :</b> Une catégorie <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span> est une collection d'ensembles. Ici, on regarderas la collection de tous les types hasell possible. Un ensemble seras donc un type. Ses éléments seront les valeurs qui sont de ce type. Par exemple Int seras un semble et 1, 4, 6 sont des éléments de cette ensemble. Il n'y a que deux éléments dans l'ensemble Bool, et une infinité d'éléments pour Integer. Pour que ce soit une catégorie, il faut qu'étant donné deux ensembles de notre collection <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_7fc56270e7a70fa81a5935b72eacbe29.gif' style=' ' class='tex' alt="A" /></span> et <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_9d5ed678fe57bcca610140957afab571.gif' style=' padding-bottom:1px;' class='tex' alt="B" /></span>, il existes une ensemble d'applications de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_a225487f82944eb21d7a2b5c47039cb5.gif' style=' ' class='tex' alt="A \to B" /></span>. Dans notre cas ce seras toute les <i>fonctions</i> de type <i> A -> B </i>. On parle des "flèches de A vers B". Pour la culture, on note l'ensemble de ces applications <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_eaab7fc4ad3190ccb26c6724c93b05f6.gif' style=' ' class='tex' alt="Hom_{\mathcal{C}}(A, B)" /></span>.</p>
<p>Attention, pour que ce soit vraiment une catégorie, il faut quelques conditions sur ces flèches :</p>
<p>1) Si <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_951196ca354c5c72b8356494f97c3b5d.gif' style=' ' class='tex' alt=" A " /></span> est un élément de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_fbadc7d1be7fc72deb6bc46eadec2378.gif' style=' ' class='tex' alt=" \mathcal{C} " /></span>, alors il faut que l'identité soit une flèche. Dans notre cas, on veut que la fonction <i>id x = x</i> de type <i>A -> A</i> soit bien une fonction haskell. Ce qui est le cas, puisque je viens de vous donner le code haskell qui permet de la définir <img src='http://zenol.fr/site/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>2) Si <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_fa809a425c3bd48b1bf2bf78f7c62dca.gif' style=' ' class='tex' alt=" f: A \to B " /></span> et <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_bb4afda3c047f239372e88d5652d9d8b.gif' style=' ' class='tex' alt=" g : B \to C " /></span> sont deux flèches (respectivement de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_951196ca354c5c72b8356494f97c3b5d.gif' style=' ' class='tex' alt=" A " /></span> dans <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_73bc9851270421c3a7e7dd37621d0dda.gif' style=' padding-bottom:1px;' class='tex' alt=" B " /></span> et de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_73bc9851270421c3a7e7dd37621d0dda.gif' style=' padding-bottom:1px;' class='tex' alt=" B " /></span> dans <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_699e902f5598ca623370f833cffb1a57.gif' style=' ' class='tex' alt=" C " /></span>), alors la composé <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_cbc67a2856173afbe891a0daf8c75a4e.gif' style=' ' class='tex' alt="g \circ f" /></span> est une flèche de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_951196ca354c5c72b8356494f97c3b5d.gif' style=' ' class='tex' alt=" A " /></span> dans <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_699e902f5598ca623370f833cffb1a57.gif' style=' ' class='tex' alt=" C " /></span>. Dans le cas qui nous intéresse, cette règle est bien respectée car si <i>f</i> et <i>g</i> sont deux fonctions haskell, alors la composé est la fonction <i>\x -> g (f x)</i>, que l'on peut aussi écrire <i>f . g</i>.</p>
<p>Donc, pour résumer : La collection de tous les types haskell est une catégorie. Si <i>a</i> et <i>b</i> sont deux types haskell, l'ensemble de toute les fonctions de <i> a -> b </i> sont appelés les flèches entre <i>a</i> et <i>b</i>.</p>
<p><b>Les foncteurs (covariants) : </b> Un foncteur <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_800618943025315f869e4e1f09471012.gif' style=' padding-bottom:1px;' class='tex' alt="F" /></span> d'une catégorie <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span> vers une catégorie <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_eefecd9cb0345697ddaac0281588a08d.gif' style=' padding-bottom:1px;' class='tex' alt="\mathcal{D}" /></span> est :</p>
<p>1) Pour chaque semble <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_7fc56270e7a70fa81a5935b72eacbe29.gif' style=' ' class='tex' alt="A" /></span> de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span>, un ensemble de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_bb957af080a6d4a120fb47b881365f14.gif' style=' ' class='tex' alt="$\mathcal{D}" /></span> qu'on noteras <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_18760c0431a2f869b624f1448cab54b4.gif' style=' ' class='tex' alt="F(A)" /></span>.</p>
<p>2) Pour chaque flèche $f : A \to B$ entre des ensembles de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span>, une flèche <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_6313ca6d0ad1dc73d9e8d4a68fb07757.gif' style=' ' class='tex' alt="F(A) \to F(B)" /></span> qu'on noteras <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_0415bdac96c0175c9ae3810fe95784b9.gif' style=' ' class='tex' alt="F(f)" /></span>.</p>
<p>3) Il faut que <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_309fda96955a650d963ee0e7adceca02.gif' style=' ' class='tex' alt="F(g \circ f) = F (g) \circ F(f)" /></span> et que <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_237d1a6079b02287c668d1e20f591d20.gif' style=' ' class='tex' alt="F(id) = id" /></span>. C'est à dire que composer des flèches avant transformation est la même chose que les composer après, et l'identité <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_ad86e3a34fa9935870aaa8fb43478f9c.gif' style=' ' class='tex' alt="id: A \to A" /></span> (flèche qui ne fait rien) est bien envoyer sur l'identité <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_b24b2f3128fd94abfffb568f1cdae1c2.gif' style=' ' class='tex' alt="id: F(A) -> F(A)" /></span>.</p>
<p>Un foncteur est donc une façon de transformer une catégorie <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span> en une partie (sous-catégorie) de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_eefecd9cb0345697ddaac0281588a08d.gif' style=' padding-bottom:1px;' class='tex' alt="\mathcal{D}" /></span>.</p>
<p></i>Point culture (pour les curieux) : Les foncteurs contravariants sont simplement des foncteurs qui "renversent" les flèches, c'est à dire en transforment A -> B en F(A) <- F(B).</i></p>
<p>Ici, ce qui nous intéresse sont les foncteurs de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span> dans <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_dcf5a9d1d0921e88bbbb7524c84d99af.gif' style=' ' class='tex' alt="\mathcal{C}" /></span> (on dit des endofoncteurs). À partir de maintenant, on ne considère plus que la catégorie <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_650161b834989ccee6ba4dc3b4f11cac.gif' style=' ' class='tex' alt="\mathcal{T}" /></span> des types haskell. Un foncteur <i>Fonc</i>, en haskell, est un foncteur de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_650161b834989ccee6ba4dc3b4f11cac.gif' style=' ' class='tex' alt="\mathcal{T}" /></span> dans <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_650161b834989ccee6ba4dc3b4f11cac.gif' style=' ' class='tex' alt="\mathcal{T}" /></span>. C'est à dire :</p>
<p>1) Une façon à tout type <i>a</i> d'associer un type <i>Fonc a</i>. Ainsi <i>Fonc</i> est un constructeur de type, par exemple <i>Liste</i> ou <i>Arbre</i> des exemples précédents. C'est peut-être le bon moment d'aller feuilleter quelques lien sur les constructeurs de type et leur "kind". Disons simplement qu'un type comme int ou bool est de kind "*" mais que Liste et Arbre sont de kind "* -> *". Cela signifit que ces deux dernier mangent un type <i>T</i> et fabrique des nouveaux types <i>Liste T</i> et <i>Arbre T</i>. Liste n'est donc pas un type, mais un constructeur de type.</p>
<p>2) Une façon à toute fonction <i>f :: a -> b</i> d'associer une fonction <i>f' :: Fonc a -> Fonc b</i></p>
<p>3) Cette façon de faire doit transformer l'identité <i> (\x -> x) :: a -> a </i> en l'identité <i>(\x -> x) :: Fonc a -> Fonc a</i></p>
<p>3) Cette façon de faire doit passer à la composition, c'est à dire que si l'on transforme <i>f :: a -> b</i> en <i>f' :: Fonc a -> Fonc b</i> et <i>g :: b -> c</i> en <i> g' :: Fonc b -> Fonc c</i>, alors <i> g . f </i> seras transformé en <i>g' . f'</i>.</p>
<p>Pour qu'un constructeur de type <i>Fonc</i> soit un foncteur, on le fait instance de la classe Functor définie comme suit :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code66'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75466"><td class="code" id="p754code66"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">class</span>  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #cccc00; font-weight: bold;">Functor</span></a> f  <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a>        <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>a <span style="color: #339933; font-weight: bold;">-&gt;</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> f a <span style="color: #339933; font-weight: bold;">-&gt;</span> f b</pre></td></tr></table></div>

<p>Remarquez que l'on peut lire <i>fmap :: (a -> b) -> (f a -> f b)</i> ce qui signifit : fmap(fonctorial mapping) prend une fonction de type <i>a -> b</i> et la transforme en une fonction de type <i>f a -> f b</i>. On a donc bien une transformation d'une flèche de a vers b en une flèche de <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_0a5e66456e67a23dbe0ea9a422ee153f.gif' style=' ' class='tex' alt="f a" /></span> vers <span class='MathJax_Preview'><img src='http://zenol.fr/site/wp-content/plugins/latex/cache/tex_5ba97fe01b73405c32ba42a84ba0ae2e.gif' style=' ' class='tex' alt="f b" /></span>. Si l'on a donc un constructeur de type qui est instance de Functor, on a bien un endofoncteur de la catégorie des types. Maintenant que nous avons le sentiment que toutes nos considérations théoriques nous ont apporté une compréhension profonde du sujet, nous allons pouvoir les oublier et passer à la pratique.</p>
<p>A quoi sert un foncteur : Un constructeur de type fonctoriel, c'est un constructeur de type où l'on sauras maper des fonctions. Si notre type Liste devient instance de Functor, et que l'on a un <i>Liste Int</i>, on peut construire rapidement une liste de tous ces nombres représenté par des chaines de caractères. Il suffit de disposer d'une fonction <i>Int -> String</i>. Haskell nous en fournis une, c'est <i>show</i>. Alors, on n'a plus qu'a appliquer cette fonction sur chacun des éléments de la liste par <i> map show liste</i>.</p>
<p>Regardons comment rendre Liste instance de Functor :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code67'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75467"><td class="code" id="p754code67"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">instance</span>  <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Functor"><span style="color: #cccc00; font-weight: bold;">Functor</span></a> Liste  <span style="color: #06c; font-weight: bold;">where</span>
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f Vide <span style="color: #339933; font-weight: bold;">=</span> Vide
    <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f <span style="color: green;">&#40;</span>Element h t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> Element <span style="color: green;">&#40;</span>f h<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> f t<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Maintenant, on peut mapper des fonctions sur des listes</span>
estPositif n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">&gt;=</span> <span style="color: red;">0</span><span style="color: green;">&#41;</span>
&nbsp;
listeEntiers <span style="color: #339933; font-weight: bold;">=</span> Cons <span style="color: red;">1</span> <span style="color: green;">&#40;</span>Cons <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">5</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Cons <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: red;">30</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Cons <span style="color: red;">4</span> Vide<span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span>
listeEstPositif <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> estPositif listeEntiers
<span style="color: #5d478b; font-style: italic;">-- listeEstPositif = Cons True (Cons False (Cons False (Cons True Vide)))</span></pre></td></tr></table></div>

<p>Si vous réfléchissez bien, ça ressemble beaucoup à ce que vous faites à chaque fois que vous appliquez un traitement aux éléments d'un container ; vous parcourez une liste, et vous appliquez votre procédure à chaque élément. L'avantage d'avoir une unique fonction fmap implémenté pour chaque type, c'est que si vous décidez de modifier votre container, vous n'aurez que très peut de changement à faire. Il suffiras de rendre le nouveau container instance de Functor, alors qu'en C++, si vous utilisiez auparavant des containers de la STL, il vous faudra vous assurer que votre nouvelle structure fournie elle aussi des itérateurs, ce qui peut être assez lourd à fournir, voir impossible si vous n'êtes pas auteur de la classe.</p>
<p>Parmis les instances de Functor il y a donc les listes (les vrais listes []), mais aussi Maybe. On peut donc utiliser Maybe pour utiliser des valeurs dans un contexte. Par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p754code68'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p75468"><td class="code" id="p754code68"><pre class="haskell" style="font-family:monospace;">divideBy <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Maybe"><span style="color: #cccc00; font-weight: bold;">Maybe</span></a> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
divideBy n m <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> m <span style="color: #339933; font-weight: bold;">==</span> <span style="color: red;">0</span> <span style="color: #06c; font-weight: bold;">then</span> Nothing <span style="color: #06c; font-weight: bold;">else</span> <span style="color: green;">&#40;</span>n `<a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:div"><span style="font-weight: bold;">div</span></a>` m<span style="color: green;">&#41;</span>
&nbsp;
&nbsp;
doSomething <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
doSomething n <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">+</span> <span style="color: red;">32</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">*</span> <span style="color: red;">5</span>
&nbsp;
&nbsp;
res <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:fmap"><span style="font-weight: bold;">fmap</span></a> doSomething <span style="color: green;">&#40;</span>divideBy <span style="color: red;">3</span> <span style="color: red;">7</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Il est très important de bien saisir l’intérêt des foncteurs (qui n'est pas cantonné aux langages fonctionnels), et leur fonctionnement pour la suite. La dernière partie ne traiteras que de leurs généralisations : les foncteurs applicatifs et les monades.</p>
<p></i>Point culture : Et si l'on veux un foncteur contravariant en haskell? On peut prendre par exemple le constructeur de type "type Func a = a -> Int" et la fonction "map :: (a -> b) -> (b -> Int) -> (a -> Int) ; map f fa = fb . f". On construit bien une fonction de type "a -> Int" à partir d'une fonction de type "b -> Int". On a donc "inversé les flèches", puisque l'on part de "a -> b" pour obtenir du "Func b -> Func a".</i></p>
<h3> Références : </h3>
<p>Catégories : <a href="http://fr.wikipedia.org/wiki/Th%C3%A9orie_des_cat%C3%A9gories">http://fr.wikipedia.org/wiki/Th%C3%A9orie_des_cat%C3%A9gories</a><br />
Foncteurs : <a href="http://fr.wikipedia.org/wiki/Foncteur">http://fr.wikipedia.org/wiki/Foncteur</a><br />
Apprendre Haskell vous fera le plus grand bien : <a href="http://lyah.haskell.fr/">http://lyah.haskell.fr/</a> ou <a href="http://learnyouahaskell.com/">http://learnyouahaskell.com/</a><br />
Real World Haskell : <a href="http://book.realworldhaskell.org/read/">http://book.realworldhaskell.org/read/</a><br />
Haskell "kind" : <a href="http://www.haskell.org/haskellwiki/Kind">http://www.haskell.org/haskellwiki/Kind (type :k Maybe and :k Bool on GHCI)</a><br />
La classe functor : <a href="http://en.wikibooks.org/wiki/Haskell/The_Functor_class">http://en.wikibooks.org/wiki/Haskell/The_Functor_class</a></p>
]]></content:encoded>
			<wfw:commentRss>http://zenol.fr/site/2013/04/26/le-haskell-un-langage-au-label-pure-seconde-partie/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Le Haskell, un langage au label pure. Première partie.</title>
		<link>http://zenol.fr/site/2013/04/16/le-haskell-un-language-au-label-pure-13/</link>
		<comments>http://zenol.fr/site/2013/04/16/le-haskell-un-language-au-label-pure-13/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 10:42:23 +0000</pubDate>
		<dc:creator>Zenol</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Useful]]></category>
		<category><![CDATA[curryfication]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://zenol.fr/site/?p=741</guid>
		<description><![CDATA[Mauvais jeux de mots mit à part, ce très court et succin billet (vous me voyez venir) va traiter du haskell. Haskell, de monsieur Haskell Brook Curry, un monsieur très épicé, est un langage fonctionnel. Comme beaucoup de langages fonctionnel, cela représente un choque culturel que de les apréhender. Mais le haskell semble avoir un <a href='http://zenol.fr/site/2013/04/16/le-haskell-un-language-au-label-pure-13/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Mauvais jeux de mots mit à part, ce très court et succin billet (vous me voyez venir) va traiter du haskell. Haskell, de monsieur Haskell Brook Curry, un monsieur très épicé, est un langage fonctionnel. Comme beaucoup de langages fonctionnel, cela représente un choque culturel que de les apréhender. Mais le haskell semble avoir un petit quelque chose que n'ont pas les autres langages fonctionnels. À travers les trois prochains billets, j'espère vous convaincre que, si un jour, dans votre vie, vous avez l'occasion de creuser un language fonctionnel, alors vous devriez creuser le haskell.</p>
<p>La <a href="http://zenol.fr/site/2013/04/26/le-haskell-un-langage-au-label-pure-seconde-partie/" title="Le Haskell, un langage au label pure. Seconde partie.">seconde partie est disponible ici</a>.</p>
<p>On y vas tout en douceur. Curryfication et typage, pour ceux qui n'ont pas déjà découvert les joies (les dollipranes?) qu'apportent ces derniers. Si vous avez déjà eu à toucher à des languages fonctionnels, vous souhaiterez surement passer directement à la seconde partie où j'aborerais les diverses fonctionnalités original de ce language, comme les fameuses liste infinies dont tout le monde parle, mais dont trop peu se doutent de l'utilité. On parleras de data driven programming, de fonctions sans effets de bord et de parallélisation des calculs en haskell (avec un VRAI exemple!). Enfin, apogée, moment de transe et de plaisir innégalé, on abordera la huitième merveille : les monades.</p>
<p>Allons y pou la première partie.</p>
<h2>1 - Ce que tout le monde sais... ou pas.</h2>
<p>Faisons un petit retour sur ce que l'on retrouve dans tous les langages fonctionnels, et ce qui les rend si différents.</p>
<p>Histoire de fixer la syntaxe, une fonction qui prend deux nombres et retourne leur produit sécrit :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code88'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74188"><td class="code" id="p741code88"><pre class="haskell" style="font-family:monospace;">mult <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #cccc00; font-weight: bold;">Num</span></a> n<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> n <span style="color: #339933; font-weight: bold;">-&gt;</span> n <span style="color: #339933; font-weight: bold;">-&gt;</span> n
mult a b <span style="color: #339933; font-weight: bold;">=</span> a <span style="color: #339933; font-weight: bold;">*</span> b</pre></td></tr></table></div>

<p>Une fonction qui ajoute 42 à un entier :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code89'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74189"><td class="code" id="p741code89"><pre class="haskell" style="font-family:monospace;">addOne <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #cccc00; font-weight: bold;">Num</span></a> n<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> n <span style="color: #339933; font-weight: bold;">-&gt;</span> n
addOne a <span style="color: #339933; font-weight: bold;">=</span> a <span style="color: #339933; font-weight: bold;">+</span> <span style="color: red;">42</span></pre></td></tr></table></div>

<p>La première ligne est le typage de la fonction (souvent facultatif), et la seconde est le corps de la fonction. En C++, ca donnerais :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code90'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74190"><td class="code" id="p741code90"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
T mult<span style="color: #008000;">&#40;</span>T a, T b<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> a <span style="color: #000040;">*</span> b<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
T addOne<span style="color: #008000;">&#40;</span>T a<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> a <span style="color: #000040;">+</span> <span style="color: #0000dd;">42</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>En fait, le "n" qui apparait dans le type peut être remplacé par n'importe quel lettre/mot qui vous plait. Cela signifie un type quelconque, qui respecte la contrainte "peut être multiplié" décrite par "(Num n) =>".</p>
<p>La présence de "Num" correspond à la nécessiter d'avoir, dans le code c++, une surcharge de l'opérateur *. Remarquez qu'une fonction en haskell correspond naturellement a une fonction template, à moins de forcer les types à être moins "puissant", comme par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code91'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74191"><td class="code" id="p741code91"><pre class="haskell" style="font-family:monospace;">mult <span style="color: #339933; font-weight: bold;">::</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">-&gt;</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
mult a b <span style="color: #339933; font-weight: bold;">=</span> a <span style="color: #339933; font-weight: bold;">*</span> b</pre></td></tr></table></div>

<p>Qui ne sais plus que multiplier des entiers.</p>
<h3>Curryfication</h3>
<p>La curryfication est essentiellement un changement de point de vue. On peux considérer une fonctions prenant deux nombres, et retournant une valeur. Mais on peut aussi changer de point de vue et considérer une fonction prenant un nombre, et renvoyant une nouvelle fonction, prenant un nombre, et retournant une valeur.</p>
<p>Cela explique l'étrange notation pour le type de la fonction mult. En fait, l'opérateur "->" est associatif à droite, c'est à dire qu'il fallait lire :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code92'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74192"><td class="code" id="p741code92"><pre class="haskell" style="font-family:monospace;">mult <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #cccc00; font-weight: bold;">Num</span></a> n<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> n <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span>n <span style="color: #339933; font-weight: bold;">-&gt;</span> n<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Où <i>X -> Y</i> indique que la fonction prend du type X et retourne du type Y. Ainsi, <i>n -> n</i> signifit prend un type n quelconque et renvoi quelque chose du même type. C'est le type d'une fonction. Si l'on veux un exemple plus concret, <i>Int-> (Float->Double)</i> signifie une fonction qui prend un entier, puis renvoi une nouvelle fonction. Cette dernière prend un float et le transforme en un double.</p>
<p>Partant de ce point de vue, il est très simple de fixer les premiers arguments d'une fonction curryfiée. L'application d'un élément à un autre étant associatif à gauche, il suffit de l'appeler avec une partie de ses arguments. (Et oui, puisque écrire mult a b signifie alors ((mult a) b). C'est à dire appliquer mult à a pour obtenir une fonction qui "prend un entier et le multiplie par a", puis évaluer cette fonction sur l'entier b.)</p>
<p>Par exemple :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code93'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74193"><td class="code" id="p741code93"><pre class="haskell" style="font-family:monospace;">nouvelle<span style="color: #339933; font-weight: bold;">_</span>fonction <span style="color: #339933; font-weight: bold;">=</span> mult <span style="color: red;">42</span></pre></td></tr></table></div>

<p>correspond à fixer la première valeur de la fonction mul à 42.</p>
<p>En C++, il faudrais écrire</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code94'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74194"><td class="code" id="p741code94"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
T nouvelle_fonction<span style="color: #008000;">&#40;</span>T b<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> mult<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">42</span>, b<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Pour finir, puisque l'on est en plein dans la manipulation de fonctions, on peut écrire une fonction comme une valeur, et la stocker dans une "variable".</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code95'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74195"><td class="code" id="p741code95"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- Pour écrire une fonction &quot;anonyme&quot;, qui prend trois arguments et renvoi 42, on peut écrire</span>
<span style="color: #5d478b; font-style: italic;">-- (\x y z -&gt; 42)</span>
<span style="color: #5d478b; font-style: italic;">-- Où x y z seront les trois arguments, et ce qui suit -&gt; est la &quot;valeur de retour&quot;.</span>
<span style="color: #5d478b; font-style: italic;">-- On aurais donc pu écrire la fonction mult de la façon suivante :</span>
mult <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>\a b <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: #339933; font-weight: bold;">*</span> b<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Num"><span style="color: #cccc00; font-weight: bold;">Num</span></a> n<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=&gt;</span> n <span style="color: #339933; font-weight: bold;">-&gt;</span> n <span style="color: #339933; font-weight: bold;">-&gt;</span> n
&nbsp;
<span style="color: #5d478b; font-style: italic;">--On peut aussi écrire \a -&gt; \b -&gt; à la place de \a b, grâce à la curryfication.</span></pre></td></tr></table></div>

<p>Le typage est biensur facultatif.</p>
<p>On a alors deux petites fonctions bien pratique pour manipuler des fonctions ; l'identité et la composition :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code96'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74196"><td class="code" id="p741code96"><pre class="haskell" style="font-family:monospace;"><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a> <span style="color: #339933; font-weight: bold;">=</span> \x <span style="color: #339933; font-weight: bold;">-&gt;</span> x
a <span style="color: #339933; font-weight: bold;">.</span> b <span style="color: #339933; font-weight: bold;">=</span> \x <span style="color: #339933; font-weight: bold;">-&gt;</span> a <span style="color: green;">&#40;</span>b x<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>On peut alors composer la multiplication par 2 et l'adition à 3 :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code97'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74197"><td class="code" id="p741code97"><pre class="haskell" style="font-family:monospace;">add a b <span style="color: #339933; font-weight: bold;">=</span> a <span style="color: #339933; font-weight: bold;">+</span> b
superFct <span style="color: #339933; font-weight: bold;">=</span> mult <span style="color: red;">2</span> <span style="color: #339933; font-weight: bold;">.</span> add <span style="color: red;">3</span></pre></td></tr></table></div>

<p>Enfin, on aurais pu écrire :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code98'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74198"><td class="code" id="p741code98"><pre class="haskell" style="font-family:monospace;">mult <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span>
add <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span>
superFct <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">.</span> <span style="color: green;">&#40;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<h3> Typage</h3>
<p>Les types, en langage fonctionnel, disent beaucoup de choses, et sont lourd de sens.<br />
En haskell, une liste d'entiers se note <i>[Int]</i>. Si a est un type, alors une liste de a est de type <i>[a]</i>. Les chaines de caractères sont par example des <i>[Char]</i>.</p>
<p>Alors, que devrait faire une fonction de type <i>(a -> b) -> [a] -> [b]</i> ? Et bien, elle prend une fonction transformant du a en b, une liste de a, et produit une liste de b. Le bon sens veux que cette fonction applique la première fonction sur chaque élément de la liste a, pour produire la liste b.</p>
<p>Encore un, pour la route. Que dire du type <i>[[a]] -> [a]</i>? On prend une liste de liste, et on produit une liste. La première chose qui nous vient à l'esprit est de concaténer toute ces listes les unes à la suite des autres.</p>
<p>Voyez tout ce qu'on peut deviner grâce aux types. On peu aussi construire des alias de type. Par exemple, plutôt que décrire <i>[Char]</i>, on peut écrire <i>String</i>, définit par :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code99'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p74199"><td class="code" id="p741code99"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Char"><span style="color: #cccc00; font-weight: bold;">Char</span></a><span style="color: green;">&#93;</span></pre></td></tr></table></div>

<p>C'est très utile pour annoter une fonction. Par exemple, on peut imaginer le scénario suivant :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code100'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741100"><td class="code" id="p741code100"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> Nom <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>
<span style="color: #06c; font-weight: bold;">type</span> Prenom <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:String"><span style="color: #cccc00; font-weight: bold;">String</span></a>
<span style="color: #06c; font-weight: bold;">type</span> Identifiant <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
&nbsp;
getSomeone <span style="color: #339933; font-weight: bold;">::</span> Nom <span style="color: #339933; font-weight: bold;">-&gt;</span> Prenom <span style="color: #339933; font-weight: bold;">-&gt;</span> Identifiant</pre></td></tr></table></div>

<p>Grâce à ces alias, vous devinez tous le comportement de la fonction getSomeone via son type.</p>
<p>Mais le meilleur est à venir : les constructeurs de type.<br />
Attention, un constructeur en haskell est tout sauf un constructeur C++. Ça se rapprocherais plutot de la structure, et encore...</p>
<p>Pou faire bref, les constructeur permettent de fabriquer des instances d'un type. Considèrons quelques types :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code101'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741101"><td class="code" id="p741code101"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Personne <span style="color: #339933; font-weight: bold;">=</span> ConstructeurDePersonne Nom Prenom Identifiant
<span style="color: #06c; font-weight: bold;">data</span> Reponse <span style="color: #339933; font-weight: bold;">=</span> Oui <span style="color: #339933; font-weight: bold;">|</span> Non
<span style="color: #06c; font-weight: bold;">data</span> LaValeur <span style="color: #339933; font-weight: bold;">=</span> Entier <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> <span style="color: #339933; font-weight: bold;">|</span> Flotant <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Float"><span style="color: #cccc00; font-weight: bold;">Float</span></a>
<span style="color: #06c; font-weight: bold;">data</span> ListeEntier <span style="color: #339933; font-weight: bold;">=</span> Element <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> Liste <span style="color: #339933; font-weight: bold;">|</span> Vide
<span style="color: #06c; font-weight: bold;">data</span> ArbreEntier <span style="color: #339933; font-weight: bold;">=</span> Noeud <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a> Arbre Arbre <span style="color: #339933; font-weight: bold;">|</span> Vide</pre></td></tr></table></div>

<p>Le premier type, Personne, est une structure à trois champs. Le second, est un type booléen, qui peut soit être construit par le constructeur Oui, soit par le constructeur Non. Le troisième est une union pouvant contenir un Int ou un Float, et les derniers sont respectivement un type liste d'entier et arbre binaire d'entier. Le système de types permet de considérer des objets plus proche des données représentée, et de s'abstraire de l'implémentation.</p>
<p>Donnons une correspondance C++ des deux derniers exemples dans leur version "polymorphique", où Liste et Arbre deviennent des constructeurs de type, et prennent donc en paramètre le type qu'ils doivent contenir.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code102'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741102"><td class="code" id="p741code102"><pre class="haskell" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">data</span> Liste a <span style="color: #339933; font-weight: bold;">=</span> Element a Liste <span style="color: #339933; font-weight: bold;">|</span> Vide
<span style="color: #06c; font-weight: bold;">data</span> Arbre a <span style="color: #339933; font-weight: bold;">=</span> Noeud a Arbre Arbre <span style="color: #339933; font-weight: bold;">|</span> Vide
&nbsp;
<span style="color: #06c; font-weight: bold;">type</span> ListeEntier <span style="color: #339933; font-weight: bold;">=</span> Liste <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a>
<span style="color: #06c; font-weight: bold;">type</span> ArbreEntier <span style="color: #339933; font-weight: bold;">=</span> Arbre <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#t:Int"><span style="color: #cccc00; font-weight: bold;">Int</span></a></pre></td></tr></table></div>

<p>L'équivalent C++ :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code103'); return false;">View Code</a> CPP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741103"><td class="code" id="p741code103"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Liste <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">union</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">struct</span> Element <span style="color: #008000;">&#123;</span>
      T value<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">struct</span> List <span style="color: #000040;">*</span>next<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> firstConstructor<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">struct</span> Vide <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span> secondConstructor<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">struct</span> Arbre <span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">union</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">struct</span> Noeud <span style="color: #008000;">&#123;</span>
      T value<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">struct</span> Arbre <span style="color: #000040;">*</span>left<span style="color: #008080;">;</span>
      <span style="color: #0000ff;">struct</span> Arbre <span style="color: #000040;">*</span>right<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> firstConstructor<span style="color: #008080;">;</span>
    <span style="color: #0000ff;">struct</span> Vide <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span> secondConstructor<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Un petit exemple d'utilisation :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code104'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741104"><td class="code" id="p741code104"><pre class="haskell" style="font-family:monospace;">personnage <span style="color: #339933; font-weight: bold;">=</span> ConstructeurDePersonne <span style="background-color: #3cb371;">&quot;Dent&quot;</span> <span style="background-color: #3cb371;">&quot;Arthur&quot;</span> <span style="color: red;">42</span>
estCeVrai <span style="color: #339933; font-weight: bold;">=</span> Oui
valeur <span style="color: #339933; font-weight: bold;">=</span> Entier <span style="color: red;">42</span>
uneListe <span style="color: #339933; font-weight: bold;">=</span> Element <span style="color: red;">1</span> <span style="color: green;">&#40;</span> Element <span style="color: red;">2</span> <span style="color: green;">&#40;</span> Element <span style="color: red;">3</span> <span style="color: green;">&#40;</span> Element <span style="color: red;">4</span> Vide <span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span> <span style="color: green;">&#41;</span>
racine <span style="color: #339933; font-weight: bold;">=</span> Noeud <span style="color: red;">4</span> <span style="color: green;">&#40;</span>Noeud <span style="color: red;">2</span> Vide Vide<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Noeud <span style="color: red;">6</span> Vide Vide<span style="color: green;">&#41;</span></pre></td></tr></table></div>

<h3> Patttern matching </h3>
<p>Si l'on peut construire des types, on peut aussi les détruire, où plus précisément, les dès-construire. Partant d'un objet de type liste, on peut vouloir récupérer le première élément, si la liste n'est pas vide. Cela se fait en faisant matcher un pattern sur l'instance d'un type :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code105'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741105"><td class="code" id="p741code105"><pre class="haskell" style="font-family:monospace;">obtenirNom <span style="color: green;">&#40;</span>ConstructeurDePersonne nom <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> nom
&nbsp;
obtenirPrenom <span style="color: green;">&#40;</span>ConstructeurDePersonne <span style="color: #339933; font-weight: bold;">_</span> prenom <span style="color: #339933; font-weight: bold;">_</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span>  prenom
&nbsp;
obtenirID <span style="color: green;">&#40;</span>ConstructeurDePersonne <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">_</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <a href="http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:id"><span style="font-weight: bold;">id</span></a>
&nbsp;
recupererValeur <span style="color: green;">&#40;</span>Entier v<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> v
&nbsp;
premierElement <span style="color: green;">&#40;</span>Element v sousListe<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> v
premierElement Empty <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span></pre></td></tr></table></div>

<p>Il est aussi possible de déconstruire un objet au milieu d'une fonction, sous réserve d’être certain que l'objet respecte le pattern spécifié :</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p741code106'); return false;">View Code</a> HASKELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p741106"><td class="code" id="p741code106"><pre class="haskell" style="font-family:monospace;">recupererValeur' truc <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> <span style="color: green;">&#40;</span>Entier v<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> truc <span style="color: #06c; font-weight: bold;">in</span> v</pre></td></tr></table></div>

<p>Nb : Le caractère ' est un caractère valide comme un autre pour un nom de fonction/variable (en fait, en haskell, il n'y a pas de variable au sens usuel).</p>
<p>Voilà, ce seras tout pour aujourd'hui <img src='http://zenol.fr/site/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Références :<br />
Quelques détails sur les constructeurs : http://www.haskell.org/haskellwiki/Constructor</p>
]]></content:encoded>
			<wfw:commentRss>http://zenol.fr/site/2013/04/16/le-haskell-un-language-au-label-pure-13/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Petite application androide : MinecraftEnchantmentCalculator</title>
		<link>http://zenol.fr/site/2013/04/11/peite-application-androide/</link>
		<comments>http://zenol.fr/site/2013/04/11/peite-application-androide/#comments</comments>
		<pubDate>Thu, 11 Apr 2013 10:42:22 +0000</pubDate>
		<dc:creator>Zenol</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Développement]]></category>
		<category><![CDATA[Useful]]></category>
		<category><![CDATA[adt]]></category>
		<category><![CDATA[androide]]></category>
		<category><![CDATA[google play]]></category>
		<category><![CDATA[minecraft]]></category>
		<category><![CDATA[Minecraft Enchantment Calculator]]></category>

		<guid isPermaLink="false">http://zenol.fr/site/?p=737</guid>
		<description><![CDATA[Aujourd'hui, pas de longues réflexions éxistancielles sur le monde, ni de gros pavés de code. Aujourd'hui, c'est placement de produit. Je me fait un peu la main avec la suite de développement androide (Android Development Tools ; essentiellement éclipse configuré pour développer des applis androide). Autant le dire tout de suite, le java n'est vraiment <a href='http://zenol.fr/site/2013/04/11/peite-application-androide/' class='excerpt-more'>[...]</a>]]></description>
				<content:encoded><![CDATA[<p>Aujourd'hui, pas de longues réflexions éxistancielles sur le monde, ni de gros pavés de code. Aujourd'hui, c'est placement de produit.</p>
<p>Je me fait un peu la main avec la suite de développement androide (Android Development Tools ; essentiellement éclipse configuré pour développer des applis androide). Autant le dire tout de suite, le java n'est vraiment pas ma tasse de thé (Je bois beaucoup de thé, et je ne supporte pas très bien le café. Ça me rend nerveu et je me met à sautiller de partout. Pas très productif...).</p>
<p>Alors, le produit, car il y à produit à placer, est une petite application servant à calculer la probabilité d'obtenir certains enchantements sur certains équipement dans le très célèbre jeu Minecraft (au quel, oui, je jou souvent). Rien de transcendant. L'interface graphique est ce qu'il y à de plus plat, et le calcule est une simple simulation du tirage çelon l'algorithme du jeu (Informations tirés du wiki officiel. Je ne me suis pas amusé à fouiller le jeu original.) répété un petit milier de fois. J'ai vu des projets plus passionant, mais bon, il faut bien se faire la main sur quelque chose, n'est-ce pas?</p>
<p>En tout cas, il fait bien le travail qu'on lui demande. Ça peu toujours servir, si vous cherchez un enchantement spécifique, et que vous voulez optimiser vos chances de l'obtenir.</p>
<p>Vous trouverez l'application sur google play : <a href="http://play.google.com/store/apps/details?id=fr.zenol.minecraftenchantmentcalculator">http://play.google.com/store/apps/details?id=fr.zenol.minecraftenchantmentcalculator</a></p>
<p>N'esitez pas à tester l'application et rédiger votre avis sur la page du store.</p>
<p>(Oui, promit, le prochain article, on feras du code.)</p>
]]></content:encoded>
			<wfw:commentRss>http://zenol.fr/site/2013/04/11/peite-application-androide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
