This commit is contained in:
Mythrann 2021-02-09 17:41:02 +01:00
parent ce60781f91
commit 21b519e731
20 changed files with 1310 additions and 62 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,26 +1,72 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ada2" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>Exercice de tracés avec des caractères</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../ystyle.css" rel="stylesheet"/><script src="../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">Exercice de tracés avec des caractères</h1><nav><a class="caml_c" title="Back to main page" href="tps.html" data-eliom-c-onclick="AVUMznauqliT"></a></nav></div><main><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('5abee968')"><span class="arrow" id="arrow-5abee968">&#x25b8;</span>Objectifs</h2><div class="yfold-content" id="content-5abee968" data-yfold-default="show"><ul class="goals"><li>Écrire quelques procédures qui affichent des formes géométriques en utilisant le caractère dièse #. Par exemple, un pseudo-carré et un damier : <pre>#####
<html class="ada2" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exercice de tracés avec des caractères</title>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<link media="all" href="../ystyle.css" rel="stylesheet" />
<script src="../Scripts/yfold.js"></script>
</head>
<body>
<div class="header" id="header">
<h1 id="title">Exercice de tracés avec des caractères</h1>
<nav><a class="caml_c" title="Back to main page" href="tps.html" data-eliom-c-onclick="AVUMznauqliT"></a></nav>
</div>
<main>
<section class="yfold">
<h2 class="yfold-title" onclick="toggleYfold('5abee968')"><span class="arrow" id="arrow-5abee968">&#x25b8;</span>Objectifs</h2>
<div class="yfold-content" id="content-5abee968" data-yfold-default="show">
<ul class="goals">
<li>Écrire quelques procédures qui affichent des formes géométriques en utilisant le caractère dièse #. Par exemple, un pseudo-carré et un damier :
<pre>#####
# #
# #
# #
#####</pre><pre> ## ## ## ##
## ## ## ##
#####</pre>
<pre> ## ## ## ##
## ## ## ##
## ## ## ## ##
## ## ## ## ##
## ## ## ##
## ## ## ##
## ## ## ##
## ## ## ##
## ## ## ## ##
## ## ## ## ##
## ## ## ##
## ## ## ## </pre></li><li>Ces exercices vous familiariseront avec les concepts des séances de TP à venir.</li></ul><h4>Prérequis</h4><p>➪ Au moins quelques restes du semestre 1.</p></div><script>
//<![CDATA[
initYfold('5abee968') ;
//]]>
</script></section><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('053717c4')"><span class="arrow" id="arrow-053717c4">&#x25b8;</span>Étapes à suivre</h2><div class="yfold-content" id="content-053717c4" data-yfold-default="hide"><ul class="steps"><li>Le seul acteur dont vous aurez besoin est <a class="file caml_c" href="../Ada-S1/acteur-gada/gada-text_io.ads.html" data-eliom-c-onclick="akNvajSCrSGu">GAda.Text_IO</a>. </li><li>Créez un nouveau dossier <span class="file">Exos-Algo</span>. </li><li>Créez-y un fichier vide <span class="file">mission1.adb</span> que vous complétez avec la structure d'usage (page 5 du livret du semestre 1). <br/><small class="pcom">Si vraiment vous n'y parvenez pas, exclamez-vous : <q>O cursed spite that ever I was born to set it right!</q> (Hamlet, Act I, Scene 5) puis copiez le fichier mission1.adb qui se situe sur commetud (comme au premier TP du premier semestre).</small></li><li>Dans votre programme, qui s'appelle déjà <var>Mission1</var>, ajoutez une procédure <var>Tracer_Ligne</var> qui reçoit en argument un entier <var>Long</var> et qui trace une ligne de <var>Long</var> symboles dièses puis passe à la ligne. <br/>Par exemple, si Long vaut 5, la procédure affiche<samp>#####</samp><img class="space" title="Tracer_Ligne" style="width:305px;height:50px;" alt="Procedure Tracer_Ligne" src="../Gen/Tracer_Ligne-1-4e0-.png"/> Pour tester, invoquer la procédure dans le corps du programme principal avec les arguments 3, 8, puis 20. Vous obtenez alors le résultat suivant : <pre><samp>###
## ## ## ##
## ## ## ## </pre>
</li>
<li>Ces exercices vous familiariseront avec les concepts des séances de TP à venir.</li>
</ul>
<h4>Prérequis</h4>
<p>➪ Au moins quelques restes du semestre 1.</p>
</div>
<script>
//<![CDATA[
initYfold('5abee968');
//]]>
</script>
</section>
<section class="yfold">
<h2 class="yfold-title" onclick="toggleYfold('053717c4')"><span class="arrow" id="arrow-053717c4">&#x25b8;</span>Étapes à suivre</h2>
<div class="yfold-content" id="content-053717c4" data-yfold-default="hide">
<ul class="steps">
<li>Le seul acteur dont vous aurez besoin est <a class="file caml_c" href="../Ada-S1/acteur-gada/gada-text_io.ads.html" data-eliom-c-onclick="akNvajSCrSGu">GAda.Text_IO</a>. </li>
<li>Créez un nouveau dossier <span class="file">Exos-Algo</span>. </li>
<li>Créez-y un fichier vide <span class="file">mission1.adb</span> que vous complétez avec la structure d'usage (page 5 du livret du semestre 1). <br /><small class="pcom">Si vraiment vous n'y parvenez pas, exclamez-vous : <q>O cursed spite
that ever I was born to set it right!</q> (Hamlet, Act I, Scene 5) puis copiez le fichier mission1.adb qui se situe sur commetud (comme au premier TP du premier semestre).</small></li>
<li>Dans votre programme, qui s'appelle déjà <var>Mission1</var>, ajoutez une procédure <var>Tracer_Ligne</var> qui reçoit en argument un entier <var>Long</var> et qui trace une ligne de <var>Long</var> symboles dièses puis passe à la
ligne. <br />Par exemple, si Long vaut 5, la procédure affiche<samp>#####</samp><img class="space" title="Tracer_Ligne" style="width:305px;height:50px;" alt="Procedure Tracer_Ligne" src="../Gen/Tracer_Ligne-1-4e0-.png" /> Pour tester,
invoquer la procédure dans le corps du programme principal avec les arguments 3, 8, puis 20. Vous obtenez alors le résultat suivant :
<pre><samp>###
########
####################</samp></pre><small class="pcom">Corps de la procédure : 4 lignes Ada.</small></li><li>Ajoutez maintenant à votre programme une procédure <code class="inline">Tracer_Rectangle</code> qui reçoit deux arguments : <var>Largeur</var> et <var>Hauteur</var>, et qui trace (toujours avec des dièses) les contours d'un rectangle des dimensions indiquées (en nombre de dièses). <img class="space" title="Tracer_Rectangle" style="width:361px;height:50px;" alt="Procedure Tracer_Rectangle" src="../Gen/Tracer_Rectangle-2-266-.png"/> Pour tester, invoquer la procédure dans le corps du programme principal avec les paires d'arguments (5,5), (4,6), puis (14,2). Vous obtenez alors le résultat suivant (utiliser des espaces &quot; &quot; pour décaler les dièses les uns des autres quand c'est nécessaire.) : <pre><samp>#####
####################</samp></pre><small class="pcom">Corps de la procédure : 4 lignes Ada.</small>
</li>
<li>Ajoutez maintenant à votre programme une procédure <code class="inline">Tracer_Rectangle</code> qui reçoit deux arguments : <var>Largeur</var> et <var>Hauteur</var>, et qui trace (toujours avec des dièses) les contours d'un rectangle
des dimensions indiquées (en nombre de dièses). <img class="space" title="Tracer_Rectangle" style="width:361px;height:50px;" alt="Procedure Tracer_Rectangle" src="../Gen/Tracer_Rectangle-2-266-.png" /> Pour tester, invoquer la procédure
dans le corps du programme principal avec les paires d'arguments (5,5), (4,6), puis (14,2). Vous obtenez alors le résultat suivant (utiliser des espaces &quot; &quot; pour décaler les dièses les uns des autres quand c'est nécessaire.) :
<pre><samp>#####
# #
# #
# #
@ -34,11 +80,25 @@ initYfold('5abee968') ;
####
##############
##############</samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada.</small></li><li><aside class="yfold"><div class="content"><h4 class="yfold-title" onclick="toggleYfold('c1b7a4c8')"><span class="arrow" id="arrow-c1b7a4c8">&#x25b8;</span>Pour tester si un nombre est pair</h4><div class="yfold-content" id="content-c1b7a4c8" data-yfold-default="hide">On utilise le modulo :<ul><li><code class="inline">X mod 2</code> vaut 0 lorsque X est pair.</li><li><code class="inline">X mod 2</code> vaut 1 lorsque X est impair.</li></ul></div><script>
//<![CDATA[
initYfold('c1b7a4c8') ;
//]]>
</script></div></aside>Ajoutez une procédure <code class="inline">Tracer_Quadrillage</code> qui reçoit aussi en argument une largeur et une hauteur. Invoquée avec la paire d'arguments (19, 11), votre procédure doit afficher ceci : <pre><samp>###################
##############</samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada.</small>
</li>
<li>
<aside class="yfold">
<div class="content">
<h4 class="yfold-title" onclick="toggleYfold('c1b7a4c8')"><span class="arrow" id="arrow-c1b7a4c8">&#x25b8;</span>Pour tester si un nombre est pair</h4>
<div class="yfold-content" id="content-c1b7a4c8" data-yfold-default="hide">On utilise le modulo :<ul>
<li><code class="inline">X mod 2</code> vaut 0 lorsque X est pair.</li>
<li><code class="inline">X mod 2</code> vaut 1 lorsque X est impair.</li>
</ul>
</div>
<script>
//<![CDATA[
initYfold('c1b7a4c8');
//]]>
</script>
</div>
</aside>Ajoutez une procédure <code class="inline">Tracer_Quadrillage</code> qui reçoit aussi en argument une largeur et une hauteur. Invoquée avec la paire d'arguments (19, 11), votre procédure doit afficher ceci :
<pre><samp>###################
# # # # # # # # # #
###################
# # # # # # # # # #
@ -48,30 +108,58 @@ initYfold('c1b7a4c8') ;
# # # # # # # # # #
###################
# # # # # # # # # #
###################</samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada, un seul IF.</small></li><li>Faites de même une procédure <code class="inline">Tracer_Damier</code> qui, invoquée avec les arguments (18,10) affiche ceci : <pre><samp># # # # # # # # #
###################</samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada, un seul IF.</small>
</li>
<li>Faites de même une procédure <code class="inline">Tracer_Damier</code> qui, invoquée avec les arguments (18,10) affiche ceci :
<pre><samp># # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #</samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada, un seul IF.</small></li><li><span class="lbutton">Facultatif</span>Et enfin, écrivez une procédure <code class="inline">Tracer_Gros_Damier</code> qui, invoquée avec les arguments (18,10) affiche ceci : <pre><samp> ## ## ## ##
## ## ## ##
# # # # # # # # #
# # # # # # # # #</samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada, un seul IF.</small>
</li>
<li><span class="lbutton">Facultatif</span>Et enfin, écrivez une procédure <code class="inline">Tracer_Gros_Damier</code> qui, invoquée avec les arguments (18,10) affiche ceci :
<pre><samp> ## ## ## ##
## ## ## ##
## ## ## ## ##
## ## ## ## ##
## ## ## ##
## ## ## ##
## ## ## ##
## ## ## ##
## ## ## ## ##
## ## ## ## ##
## ## ## ##
## ## ## ## </samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada, un seul IF.</small></li></ul></div><script>
//<![CDATA[
initYfold('053717c4') ;
//]]>
</script></section><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('14138620')"><span class="arrow" id="arrow-14138620">&#x25b8;</span>Bilan</h2><div class="yfold-content" id="content-14138620" data-yfold-default="hide"><ul class="success"><li>Vous avez compris que pour parcourir une structure à deux dimensions (damier, matrice), il faut deux boucles FOR imbriquées.</li><li>Dans ces deux boucles FOR imbriquées, vous reconnaissez le compteur des lignes et le compteur des colonnes.</li></ul></div><script>
//<![CDATA[
initYfold('14138620') ;
//]]>
</script></section></main><footer><small class="pcom"><a class="caml_c" href="../yversion.html" data-eliom-c-onclick="7pkwUvkk3G6G">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../Images/html5.png"/></a></footer></body></html>
## ## ## ##
## ## ## ## </samp></pre><small class="pcom">Corps de la procédure : une dizaine de lignes Ada, un seul IF.</small>
</li>
</ul>
</div>
<script>
//<![CDATA[
initYfold('053717c4');
//]]>
</script>
</section>
<section class="yfold">
<h2 class="yfold-title" onclick="toggleYfold('14138620')"><span class="arrow" id="arrow-14138620">&#x25b8;</span>Bilan</h2>
<div class="yfold-content" id="content-14138620" data-yfold-default="hide">
<ul class="success">
<li>Vous avez compris que pour parcourir une structure à deux dimensions (damier, matrice), il faut deux boucles FOR imbriquées.</li>
<li>Dans ces deux boucles FOR imbriquées, vous reconnaissez le compteur des lignes et le compteur des colonnes.</li>
</ul>
</div>
<script>
//<![CDATA[
initYfold('14138620');
//]]>
</script>
</section>
</main>
<footer><small class="pcom"><a class="caml_c" href="../yversion.html" data-eliom-c-onclick="7pkwUvkk3G6G">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse"
style="width:73px;height:28px;" alt="INSA logo" src="../Images/logo-insa-light.jpg" /></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;"
alt="CSS3 logo" src="../Images/css3.png" /></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../Images/html5.png" /></a></footer>
</body>
</html>

BIN
Y/Images/1st week.pdf Normal file

Binary file not shown.

BIN
Y/Images/2nd week.pdf Normal file

Binary file not shown.

4
Y/OCaml/exam-2017.html Normal file
View file

@ -0,0 +1,4 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="en" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>OCaml - Exam questions, 2017</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../ystyle.css" rel="stylesheet"/><script src="../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">OCaml - Exam questions, 2017</h1><nav><a class="caml_c" title="Back to main page" href="../ocaml.html" data-eliom-c-onclick="1juwZI9z76Kf"></a></nav></div><main><ul class="lessons"><li>Note that this year's exam is expected to be a bit more difficult.</li><li><b>First assessment: </b><ul><li><a class="caml_c" href="exam2017/enonce1.txt.html" data-eliom-c-onclick="G43FxodgdKSo">enonce1.txt</a></li><li><a class="caml_c" href="exam2017/types_vehicle.ml.html" data-eliom-c-onclick="D52L0MVMcS6t">types_vehicle.ml</a></li><li><a class="caml_c" href="exam2017/answers1.ml.html" data-eliom-c-onclick="WWk6WxH8uFiY">answers1.ml</a></li></ul><small class="pcom">If you wish to try by yourself on INSA computers, you will need to download these files: <a class="caml_c" href="exam2017/qqs1.ml" data-eliom-c-onclick="uf2Nsu4hbd5P">qqs1.ml</a> and <a class="caml_c" href="exam2017/exam1.ml" data-eliom-c-onclick="25qywZrSZLxf">exam1.ml</a>. </small></li><li><b>Second assessment: </b><ul><li><a class="caml_c" href="exam2017/enonce2.txt.html" data-eliom-c-onclick="DUb7sVO/rNjP">enonce2.txt</a></li><li><a class="caml_c" href="exam2017/types_vehicle.ml.html" data-eliom-c-onclick="qub/xTfFO1yE">types_vehicle.ml</a></li><li><a class="caml_c" href="exam2017/answers2.ml.html" data-eliom-c-onclick="hggBVwHUpOEW">answers2.ml</a></li></ul><small class="pcom">On INSA computers, you will need <a class="caml_c" href="exam2017/qqs2.ml" data-eliom-c-onclick="cEOYaScBze4T">qqs2.ml</a> and <a class="caml_c" href="exam2017/exam2.ml" data-eliom-c-onclick="xjMPFxQg6iaX">exam2.ml</a>. </small></li></ul>Note: there exist many solutions to each question.</main><footer><small class="pcom"><a class="caml_c" href="../yversion.html" data-eliom-c-onclick="VrM+0BLZoejr">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../Images/html5.png"/></a></footer></body></html>

4
Y/OCaml/exam-2019.html Normal file
View file

@ -0,0 +1,4 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="en" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>OCaml - Exam questions, 2019</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../ystyle.css" rel="stylesheet"/><script src="../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">OCaml - Exam questions, 2019</h1><nav><a class="caml_c" title="Back to main page" href="../ocaml.html" data-eliom-c-onclick="Ra/joMV+wZl2"></a></nav></div><main><ul class="lessons"><li><b>First assessment: </b><ul><li><a class="caml_c" href="exam2019/Sujet-1/enonce.txt.html" data-eliom-c-onclick="f6YGpUBYEfWZ">enonce.txt</a></li><li><a class="caml_c" href="exam2019/Sujet-1/types_score.ml.html" data-eliom-c-onclick="SlDuDu2ATU83">types_score.ml</a></li><li><a class="caml_c" href="exam2019/Sujet-1/answers.ml.html" data-eliom-c-onclick="3dWAJlKvbQNq">answers.ml</a></li></ul><small class="pcom">If you wish to try by yourself on INSA computers, you will need to download these files: <a class="caml_c" href="exam2019/Sujet-1/qqs.ml" data-eliom-c-onclick="6mVEFPGMBTmU">qqs.ml</a> and <a class="caml_c" href="exam2019/Sujet-1/exam.ml" data-eliom-c-onclick="tzO3wE56OPfJ">exam.ml</a>. </small></li><li><b>Second assessment: </b><ul><li><a class="caml_c" href="exam2019/Sujet-2/enonce.txt.html" data-eliom-c-onclick="22R+eMXIXSI4">enonce.txt</a></li><li><a class="caml_c" href="exam2019/Sujet-2/types_score.ml.html" data-eliom-c-onclick="6mboSEce1X94">types_score.ml</a></li><li><a class="caml_c" href="exam2019/Sujet-2/answers.ml.html" data-eliom-c-onclick="rLPSdaToJ7en">answers.ml</a></li></ul><small class="pcom">On INSA computers, you will need <a class="caml_c" href="exam2019/Sujet-2/qqs.ml" data-eliom-c-onclick="kTN8S6zSI/70">qqs.ml</a> and <a class="caml_c" href="exam2019/Sujet-2/exam.ml" data-eliom-c-onclick="88HWSauDON7e">exam.ml</a>. </small></li></ul>Note: there exist many possible solutions to each question.</main><footer><small class="pcom"><a class="caml_c" href="../yversion.html" data-eliom-c-onclick="K9TMw+N+dbpc">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../Images/html5.png"/></a></footer></body></html>

4
Y/OCaml/exam-2020.html Normal file
View file

@ -0,0 +1,4 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="en" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>OCaml - Exam questions, 2020</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../ystyle.css" rel="stylesheet"/><script src="../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">OCaml - Exam questions, 2020</h1><nav><a class="caml_c" title="Back to main page" href="../ocaml.html" data-eliom-c-onclick="EQAb8IMjNUBO"></a></nav></div><main><ul class="lessons"><li><b>First assessment: </b><ul><li><a class="caml_c" href="exam2020/Sujet-1/enonce.txt.html" data-eliom-c-onclick="pLIrMVdGCFRZ">enonce.txt</a></li><li><a class="caml_c" href="exam2020/Sujet-2/types_filter.ml.html" data-eliom-c-onclick="TGe9/BOactAo">types_filter.ml</a></li><li><a class="caml_c" href="exam2020/Sujet-2/types_tree.ml.html" data-eliom-c-onclick="zs7l8RHkup2v">types_tree.ml</a></li><li><a class="caml_c" href="exam2020/Sujet-1/answers.ml.html" data-eliom-c-onclick="BwKcwJBuYPHs">answers.ml</a></li></ul><small class="pcom">If you wish to try by yourself on INSA computers, you will need to download these files: <a class="caml_c" href="exam2020/Sujet-1/qqs.ml" data-eliom-c-onclick="cb/6FIisg0rQ">qqs.ml</a> and <a class="caml_c" href="exam2020/Sujet-1/exam.ml" data-eliom-c-onclick="OpooB8i2quTR">exam.ml</a>. </small></li><li><b>Second assessment: </b><ul><li><a class="caml_c" href="exam2020/Sujet-2/enonce.txt.html" data-eliom-c-onclick="ifJ6ecGW6phB">enonce.txt</a></li><li><a class="caml_c" href="exam2020/Sujet-2/types_filter.ml.html" data-eliom-c-onclick="uM9kqWn5bf1u">types_filter.ml</a></li><li><a class="caml_c" href="exam2020/Sujet-2/types_tree.ml.html" data-eliom-c-onclick="OVsXl4WY3EfW">types_tree.ml</a></li><li><a class="caml_c" href="exam2020/Sujet-2/answers.ml.html" data-eliom-c-onclick="HXYon1ZrR6mr">answers.ml</a></li></ul><small class="pcom">On INSA computers, you will need <a class="caml_c" href="exam2020/Sujet-2/qqs.ml" data-eliom-c-onclick="3Fz/YOScsfOu">qqs.ml</a> and <a class="caml_c" href="exam2020/Sujet-2/exam.ml" data-eliom-c-onclick="PJgPgOKQP35N">exam.ml</a>. </small></li></ul>Note: there exist many possible solutions to each question.</main><footer><small class="pcom"><a class="caml_c" href="../yversion.html" data-eliom-c-onclick="MgoO1NfRLEo0">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,118 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>answers.ml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">answers.ml</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-1/answers.ml" data-eliom-c-onclick="6Rcyxi5o5/Vu">Download file: answers.ml</a></div><code class="page block"><span class="highcomment">(*** Keep these lines. ***)</span>
<span class="kw">let</span> <span class="letvar">qfile</span> = <span class="string">&quot;qqs.ml&quot;</span> <span class="semi">;;</span>
<span class="kw">let</span> <span class="letvar">formatter</span> = <span class="uident">Format</span>.(make_formatter (<span class="kw">fun</span> _ _ _ -&gt; ()) (<span class="kw">fun</span> () -&gt; ())) <span class="semi">;;</span>
<span class="uident">Topdirs</span>.dir_use formatter (<span class="kw">if</span> <span class="uident">Sys</span>.file_exists qfile <span class="kw">then</span> qfile <span class="kw">else</span> <span class="uident">Sys</span>.getenv <span class="string">&quot;HOME&quot;</span> ^ <span class="string">&quot;/Exam-OCaml/&quot;</span> ^ qfile) <span class="semi">;;</span>
<span class="highcomment">(************************************)</span>
<span class="highcomment">(***** QUESTION 1 *****)</span>
<span class="kw">let</span> <span class="letvar">f1</span> f x =
<span class="kw">try</span> f x <span class="kw">with</span>
| <span class="uident">Invalid_argument</span> s -&gt; <span class="kw">raise</span> (<span class="uident">Failure</span> s)
| <span class="uident">Failure</span> s -&gt; <span class="kw">raise</span> (<span class="uident">Invalid_argument</span> s)
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f1 does not have the expected type. *)</span>
<span class="kw">let</span> () = q1 { ff = f1 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 2 *****)</span>
<span class="kw">let</span> <span class="letvar">f2</span> a b l =
<span class="comment">(* 2 - Go until first b *)</span>
<span class="kw">let</span> <span class="kw">rec</span> loop2 acu = <span class="kw">function</span>
| [] -&gt; <span class="uident">List</span>.rev acu
| x <span class="operator">::</span> xs -&gt; <span class="kw">if</span> x = b <span class="kw">then</span> <span class="uident">List</span>.rev (b <span class="operator">::</span> acu) <span class="kw">else</span> loop2 (x <span class="operator">::</span> acu) xs
<span class="kw">in</span>
<span class="comment">(* 1 - Find first a *)</span>
<span class="kw">let</span> <span class="kw">rec</span> loop1 = <span class="kw">function</span>
| [] -&gt; []
| x <span class="operator">::</span> xs -&gt; <span class="kw">if</span> x = a <span class="kw">then</span> loop2 [a] xs <span class="kw">else</span> loop1 xs
<span class="kw">in</span>
loop1 l
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f2 does not have the expected type. *)</span>
<span class="kw">let</span> () = q2 { ff = f2 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 3 *****)</span>
<span class="kw">let</span> <span class="letvar">f3</span> tree =
<span class="kw">let</span> <span class="kw">rec</span> loop acu (<span class="uident">Node</span> (x,l)) = <span class="uident">List</span>.fold_left loop (x <span class="operator">::</span> acu) l <span class="kw">in</span>
loop [] tree
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f3 does not have the expected type. *)</span>
<span class="kw">let</span> () = q3 { ff = f3 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 4 *****)</span>
<span class="kw">let</span> <span class="letvar">f4</span> =
{ forbid = (<span class="kw">function</span> <span class="uident">Some</span> <span class="number">0</span> -&gt; <span class="string">&quot;zero&quot;</span> | _ -&gt; <span class="string">&quot;&quot;</span>) ;
f = (<span class="kw">function</span> <span class="uident">None</span> -&gt; <span class="kw">false</span> | <span class="uident">Some</span> x -&gt; x &gt; <span class="number">0</span> ) }
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f4 does not have the expected type. *)</span>
<span class="kw">let</span> () = q4 { ff = f4 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 5 *****)</span>
<span class="kw">let</span> <span class="letvar">f5</span> f =
{ forbid = (<span class="kw">fun</span> (a,b) -&gt; f.forbid (b,a)) ;
f = (<span class="kw">fun</span> (a,b) -&gt; f.f (b,a)) }
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f5 does not have the expected type. *)</span>
<span class="kw">let</span> () = q5 { ff = f5 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 6 *****)</span>
<span class="kw">let</span> <span class="letvar">f6</span> f =
<span class="kw">let</span> <span class="letvar">forbid</span> l =
<span class="kw">match</span> <span class="uident">List</span>.find_opt (<span class="kw">fun</span> x -&gt; f.forbid x &lt;&gt; <span class="string">&quot;&quot;</span>) l <span class="kw">with</span>
| <span class="uident">None</span> -&gt; <span class="string">&quot;&quot;</span>
| <span class="uident">Some</span> x -&gt; f.forbid x
<span class="kw">and</span> <span class="letvar">f</span> l = <span class="uident">List</span>.fold_left (<span class="kw">fun</span> r x -&gt; r &amp;&amp; f.f x) <span class="kw">true</span> l
<span class="kw">in</span>
{ forbid ; f }
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f6 does not have the expected type. *)</span>
<span class="kw">let</span> () = q6 { ff = f6 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 7 *****)</span>
<span class="kw">let</span> <span class="letvar">f7</span> x graph =
<span class="kw">let</span> <span class="kw">rec</span> loop acu x =
<span class="kw">let</span> <span class="letvar">succ</span> = <span class="uident">List</span>.filter_map
(<span class="kw">fun</span> (a,b) -&gt; <span class="kw">if</span> a = x <span class="kw">then</span> <span class="uident">Some</span> b <span class="kw">else</span> <span class="uident">None</span>) graph
<span class="kw">in</span>
<span class="kw">if</span> <span class="uident">List</span>.mem x acu <span class="kw">then</span> acu
<span class="kw">else</span> <span class="uident">List</span>.fold_left (<span class="kw">fun</span> acu y -&gt; loop acu y) (x <span class="operator">::</span> acu) succ
<span class="kw">in</span>
loop [] x
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f7 does not have the expected type. *)</span>
<span class="kw">let</span> () = q7 { ff = f7 } <span class="semi">;;</span>
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="dUUUsFp0Geii">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,176 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>enonce.txt</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">enonce.txt</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-1/enonce.txt" data-eliom-c-onclick="ssIdvvVR4JoX">Download file: enonce.txt</a></div><code class="page block">=======================================================================================
=== ===
=== Sujet de l'examen ===
=== ===
=======================================================================================
- Pour valider chaque fonction, vous devez obligatoirement effectuer le test automatique
qui vous indiquera si votre fonction est correcte.
Le test automatique est déjà présent dans le programme à compléter ('✔✔✔ Check your answer').
- Les fonctions n'ont pas besoin d'être tail-recursive, sauf lorsque c'est demandé explicitement.
- Vous avez le droit d'utiliser les fonctions de la librairie standard (p. ex. List.filter), mais sans y être obligé.
En particulier, vous pouvez utiliser List.rev pour construire une liste inverse si besoin.
- Vous pouvez créer et utiliser autant de fonctions auxiliaires que vous souhaitez.
- Les énoncés sont en anglais afin de rester familier avec le vocabulaire des leçons et exercices.
- Le barème pour chaque question est indiqué de manière approximative avec des étoiles ★★.
=======================================================================================
----- Question 1 ★ -----
Write a function f1 : ∀ α β . (α → β) → (α → β)
This is about exceptions. These exceptions already exist in standard Ocaml:
- exception Invalid_argument of string e.g. Invalid_argument &quot;this is baaaaad!&quot;
- exception Failure of string e.g. Failure &quot;this is also baaaaad!&quot;
f1 expects a function g. It returns a new function g2 such that :
- g2 behaves exactly like g, but
- if g raises the exception Failure, then g2 raises the exception Invalid_argument with the same string
- if g raises the exception Invalid_argument, then g2 raises the exception Failure with the same string
In short, g2 is like g, but the exceptions Invalid_argument and Failure have been switched.
Examples
f1 (fun () -&gt; 0) () ~~&gt; 0
f1 (fun () -&gt; raise Not_found) () ~~&gt; fails with Not_found
f1 (fun () -&gt; raise (Failure &quot;toto&quot;)) () ~~&gt; fails with Invalid_argument &quot;toto&quot;
f1 (fun () -&gt; raise (Invalid_argument &quot;zaza&quot;)) () ~~&gt; fails with Failure &quot;zaza&quot;
----- Question 2 ★★ -----
Write a function f2 : ∀ α . ααα list → α list
f2 expects three arguments: x, y, and a list l.
f2 must return the part of the list l which is between x and y.
More precisely :
- if x does not occur in the list l, return the empty list.
- if x occurs in l, return the part of the list between the first x and the first y occurring strictly after x.
- if x occurs in l, but no y is found strictly after x, returns the list from x until the end.
Examples
let l = [ 1 ; 2 ; 3 ; 3 ; 4 ; 5 ; 6 ; 7 ] (* Notice: 3 occurs twice. *)
f2 9 5 l ~~&gt; []
f2 5 5 l ~~&gt; [ 5 ; 6 ; 7 ]
f2 3 5 l ~~&gt; [ 3 ; 3 ; 4 ; 5 ]
f2 3 0 l ~~&gt; [ 3 ; 3 ; 4 ; 5 ; 6 ; 7 ]
f2 3 3 l ~~&gt; [ 3 ; 3 ]
f2 5 5 l ~~&gt; [ 5 ; 6 ; 7 ]
f2 1 3 l ~~&gt; [ 1 ; 2 ; 3 ]
----- Question 3 ★★ -----
Open types_tree.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a function f3 : ∀ α . α tree → α list
f2 expects a tree (as defined in Types_tree).
f2 must return the list of values found in the tree, in any order.
Examples
Take a look at test tree number 4304:
q3_qvalue 4304 ;; (* A tree with six nodes should be printed (assuming I have the same tree than you). *)
Its values are [ -13 ; 0 ; 3 ; 0 ; -3 ; 1 ]
Then, you can directly test your function with
q3_invok f3 4304 ;;
Another test: (q3_qvalue 1702) has 8 nodes : [ -8 ; 4 ; 2 ; 0 ; -1 ; -3 ; 0 ; -1 ]
----- Question 4 ★ -----
Open types_filter.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a value f4 : (int option) filter
f4 is a filter of options of integers.
It forbids only one value : Some 0, with the message &quot;zero&quot;.
It accepts only values of the form Some x where x is positive.
It rejects None and Some x where x is negative.
You just have to define the filter. It will be applied automatically.
You don't have to return result values like Accept, Reject or Forbid.
Examples
if filter f4 is applied to Some 0 ~~&gt; Forbid &quot;zero&quot;
if filter f4 applied to Some 1 ~~&gt; Accept
if filter f4 applied to None ~~&gt; Reject
----- Question 5 ★ -----
Open types_filter.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a function f5 : ∀ α β . (α×β) filter → (β×α) filter
f5 takes a filter g working on pairs and returns a new filter also working on pairs.
(a,b) is forbidden by f5 if and only if (b,a) is forbidden by g.
(a,b) is accepted by f5 if and only if (b,a) is accepted by g.
Examples
In the tests, we use predefined filters, mentionned at the end of types_filter.ml
f5 filter_pair applied to (-5,0) ~~&gt; Reject
f5 filter_pair applied to (0,-5) ~~&gt; Forbid &quot;negative&quot;
f5 filter_pair applied to (2,2) ~~&gt; Accept
----- Question 6 ★★ -----
Open types_filter.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a function f6 : ∀ α . α filter → (α list) filter
f6 takes a filter g working on values of type 'a. It returns a new filter operating on lists of 'a.
f6 forbids a list if and only if the list contains at least one element forbidden by g.
Then, it returns the message associated to the first forbidden element in the list
f6 accepts a list if and only if g accepts all the elements of the list.
Examples
In the tests, we use predefined filters, mentionned at the end of types_filter.ml
f6 filter_even applied to [] ~~&gt; Accept
f6 filter_even applied to [8,2,-3] ~~&gt; Forbid &quot;negative&quot;
f6 filter_even applied to [8,2,4] ~~&gt; Accept
f6 filter_even applied to [8,2,5] ~~&gt; Reject
----- Question 7 ★★★ -----
Write a function f7 : int → (int × int) list → int list
f7 expects a node n and a directed graph.
The node n is only a number, e.g. 12.
The graph is given as a list of all arcs, e.g. [ (12, 4) ; (12, 3) ] meaning that there are two arcs from node 12 to nodes 3 and 4.
The graph may contain loops : (7,7)
The graph may contain cycles : (3,6) (6,1) (1,3)
f7 must return the list of all nodes that can be reached from node n, including n itself, in any order.
For instance, in the cycle above, the nodes [ 3 ; 6 ; 1 ] can be reached from node 3.
Nodes must occur at most once in the result. No duplicates.
Be careful, this is a graph, your recursion must not loop forever.
Examples
f7 0 [ ] ~~&gt; 0
f7 1 [ (1, 2) ; (2, 3) ; (3, 4) ; (4, 5) ; (5, 6) ] ~~&gt; [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ]
f7 0 [ (3, 9) ; (4, 2) ; (2, 5) ; (0, 4) ; (4, 9) ] ~~&gt; [ 0 ; 4 ; 9 ; 2 ; 5 ]
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="kWfroaIf6eAk">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,44 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>types_filter.ml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">types_filter.ml</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-2/types_filter.ml" data-eliom-c-onclick="KA7fhEbmWuTc">Download file: types_filter.ml</a></div><code class="page block">
<span class="comment">(* This is a filter: it tests values of type 'a.
* A filter contains two functions: forbid and f (see the type definition).
*
* We say that a filter:
* - forbids a value x if forbid x returns a non-empty string.
* - accepts a value x if f x returns true
* - rejects a value x if f x returns false
*
* The forbid test is done first: if a value is forbidden, it cannot be accepted or rejected.
*
* Example with an int filter :
* if forbid (-1) returns &quot;a negative number, this is insane!&quot;, this means that -1 is a forbidden value.
* if forbid 12 returns &quot;&quot;, this means that 12 is not forbidden.
*
* if f 12 returns true, this means that 12 is accepted.
* if f 17 returns false, this means that 17 is rejected (assuming 17 is not forbidden either: forbid 17 returns &quot;&quot;)
*)</span>
<span class="kw">type</span> 'a filter =
{ forbid: ('a -&gt; string) ;
f: ('a -&gt; bool) }
<span class="comment">(* When a filter is applied to a value, it returns one of these. *)</span>
<span class="kw">type</span> result = <span class="uident">Forbid</span> <span class="kw">of</span> string | <span class="uident">Accept</span> | <span class="uident">Reject</span>
<span class="comment">(* Forbid contains the message returned by the forbid function.
* It cannot be empty. *)</span>
<span class="comment">(* In some tests, we use some predefined filters :
*
* filter_even : int filter, forbids : negative numbers, accepts : even numbers (2,4,6,8, ...)
* filter_odd : int filter, forbids : 0, accepts : odd numbers (1,3,-5,-7,...)
*
* filter_pair : (int*int) filter, forbids pairs (a,b) when a is a negative number, accepts pairs with equal values, e.g. (4,4)
*
*)</span>
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="tMmOzWhMzM9u">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>types_tree.ml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">types_tree.ml</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-2/types_tree.ml" data-eliom-c-onclick="N0ywg/aF+w2W">Download file: types_tree.ml</a></div><code class="page block">
<span class="comment">(* This type defines a tree.
* Each node has a value of type 'a,
* and it can have any number of childs (subtrees). *)</span>
<span class="kw">type</span> 'a tree = <span class="uident">Node</span> <span class="kw">of</span> 'a * ('a tree list)
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="iZCrQ9k2ThrK">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,115 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>answers.ml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">answers.ml</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-2/answers.ml" data-eliom-c-onclick="MA57T0P7NXH5">Download file: answers.ml</a></div><code class="page block"><span class="highcomment">(*** Keep these lines. ***)</span>
<span class="kw">let</span> <span class="letvar">qfile</span> = <span class="string">&quot;qqs.ml&quot;</span> <span class="semi">;;</span>
<span class="kw">let</span> <span class="letvar">formatter</span> = <span class="uident">Format</span>.(make_formatter (<span class="kw">fun</span> _ _ _ -&gt; ()) (<span class="kw">fun</span> () -&gt; ())) <span class="semi">;;</span>
<span class="uident">Topdirs</span>.dir_use formatter (<span class="kw">if</span> <span class="uident">Sys</span>.file_exists qfile <span class="kw">then</span> qfile <span class="kw">else</span> <span class="uident">Sys</span>.getenv <span class="string">&quot;HOME&quot;</span> ^ <span class="string">&quot;/Exam-OCaml/&quot;</span> ^ qfile) <span class="semi">;;</span>
<span class="highcomment">(************************************)</span>
<span class="highcomment">(***** QUESTION 1 *****)</span>
<span class="kw">let</span> <span class="letvar">f1</span> f tos x =
<span class="kw">try</span> tos (f x)
<span class="kw">with</span> _ -&gt; <span class="string">&quot;exception&quot;</span>
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f1 does not have the expected type. *)</span>
<span class="kw">let</span> () = q1 { ff = f1 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 2 *****)</span>
<span class="kw">let</span> <span class="letvar">f2</span> a b l =
<span class="comment">(* Ignore part until b *)</span>
<span class="kw">let</span> <span class="kw">rec</span> loop2 acu = <span class="kw">function</span>
| [] -&gt; <span class="uident">List</span>.rev acu
| x <span class="operator">::</span> xs -&gt;
<span class="kw">if</span> x = b <span class="kw">then</span> <span class="uident">List</span>.rev (<span class="uident">List</span>.rev_append xs acu)
<span class="kw">else</span> loop2 acu xs
<span class="kw">in</span>
<span class="comment">(* Keep first part until a *)</span>
<span class="kw">let</span> <span class="kw">rec</span> loop1 acu = <span class="kw">function</span>
| [] -&gt; <span class="uident">List</span>.rev acu
| x <span class="operator">::</span> xs -&gt; <span class="kw">if</span> x = a <span class="kw">then</span> loop2 acu xs <span class="kw">else</span> loop1 (x <span class="operator">::</span> acu) xs
<span class="kw">in</span>
loop1 [] l
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f2 does not have the expected type. *)</span>
<span class="kw">let</span> () = q2 { ff = f2 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 3 *****)</span>
<span class="kw">let</span> <span class="letvar">f3</span> (<span class="uident">Node</span> (x,_) <span class="kw">as</span> tree) =
<span class="kw">let</span> <span class="kw">rec</span> loop acu (<span class="uident">Node</span> (x, l)) = <span class="uident">List</span>.fold_left loop (max x acu) l <span class="kw">in</span>
loop x tree
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f3 does not have the expected type. *)</span>
<span class="kw">let</span> () = q3 { ff = f3 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 4 *****)</span>
<span class="kw">let</span> <span class="letvar">f4</span> =
{ forbid = (<span class="kw">fun</span> (a,b) -&gt; <span class="kw">if</span> a = b <span class="kw">then</span> <span class="string">&quot;equal&quot;</span> <span class="kw">else</span> <span class="string">&quot;&quot;</span>) ;
f = (<span class="kw">fun</span> (a,b) -&gt; a &lt; b) }
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f4 does not have the expected type. *)</span>
<span class="kw">let</span> () = q4 { ff = f4 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 5 *****)</span>
<span class="kw">let</span> <span class="letvar">f5</span> f =
{ forbid = (<span class="kw">function</span> <span class="uident">None</span> -&gt; <span class="string">&quot;&quot;</span> | <span class="uident">Some</span> x -&gt; f.forbid x) ;
f = (<span class="kw">function</span> <span class="uident">None</span> -&gt; <span class="kw">false</span> | <span class="uident">Some</span> x -&gt; f.f x) }
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f5 does not have the expected type. *)</span>
<span class="kw">let</span> () = q5 { ff = f5 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 6 *****)</span>
<span class="kw">let</span> <span class="letvar">f6</span> f =
<span class="kw">let</span> <span class="letvar">forbid</span> l =
<span class="kw">if</span> l = [] <span class="kw">then</span> <span class="string">&quot;&quot;</span>
<span class="kw">else</span> <span class="kw">if</span> <span class="uident">List</span>.for_all (<span class="kw">fun</span> x -&gt; f.forbid x &lt;&gt; <span class="string">&quot;&quot;</span>) l <span class="kw">then</span> <span class="string">&quot;all&quot;</span> <span class="kw">else</span> <span class="string">&quot;&quot;</span>
<span class="kw">and</span> <span class="letvar">f</span> l = <span class="uident">List</span>.exists (<span class="kw">fun</span> x -&gt; f.forbid x = <span class="string">&quot;&quot;</span> &amp;&amp; f.f x) l
<span class="kw">in</span>
{ forbid ; f }
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f6 does not have the expected type. *)</span>
<span class="kw">let</span> () = q6 { ff = f6 } <span class="semi">;;</span>
<span class="highcomment">(***** QUESTION 7 *****)</span>
<span class="kw">let</span> <span class="letvar">f7</span> x graph =
<span class="kw">let</span> <span class="kw">rec</span> loop acu x =
<span class="kw">let</span> <span class="letvar">succ</span> = <span class="uident">List</span>.filter_map
(<span class="kw">fun</span> (a,b) -&gt; <span class="kw">if</span> a = x <span class="kw">then</span> <span class="uident">Some</span> b <span class="kw">else</span> <span class="uident">None</span>) graph
<span class="kw">in</span>
<span class="kw">if</span> <span class="uident">List</span>.mem x acu <span class="kw">then</span> acu
<span class="kw">else</span> <span class="uident">List</span>.fold_left (<span class="kw">fun</span> acu y -&gt; loop acu y) (x <span class="operator">::</span> acu) succ
<span class="kw">in</span>
loop [] x
<span class="semi">;;</span>
<span class="comment">(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f7 does not have the expected type. *)</span>
<span class="kw">let</span> () = q7 { ff = f7 } <span class="semi">;;</span>
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="0d0fYaH4VO/h">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,171 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>enonce.txt</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">enonce.txt</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-2/enonce.txt" data-eliom-c-onclick="q5wDVBLTmDXO">Download file: enonce.txt</a></div><code class="page block">=======================================================================================
=== ===
=== Sujet de l'examen ===
=== ===
=======================================================================================
- Pour valider chaque fonction, vous devez obligatoirement effectuer le test automatique
qui vous indiquera si votre fonction est correcte.
Le test automatique est déjà présent dans le programme à compléter ('✔✔✔ Check your answer').
- Les fonctions n'ont pas besoin d'être tail-recursive, sauf lorsque c'est demandé explicitement.
- Vous avez le droit d'utiliser les fonctions de la librairie standard (p. ex. List.filter), mais sans y être obligé.
En particulier, vous pouvez utiliser List.rev pour construire une liste inverse si besoin.
- Vous pouvez créer et utiliser autant de fonctions auxiliaires que vous souhaitez.
- Les énoncés sont en anglais afin de rester familier avec le vocabulaire des leçons et exercices.
- Le barème pour chaque question est indiqué de manière approximative avec des étoiles ★★.
=======================================================================================
----- Question 1 ★ -----
Write a function f1 : ∀ α β . (α → β) → (β → string) → (α → string)
This is about exceptions.
f1 expects two functions: g and tos (tos means to_string).
It returns a new function g2 of type 'a -&gt; string such that :
- if g x returns a value y, then g2 x returns y as a string (y is converted to string using tos)
- if g x raises an exception, then g2 x returns the string &quot;exception&quot;.
Examples
let soi = string_of_int
f1 (fun () -&gt; 0) soi () ~~&gt; &quot;0&quot;
f1 (fun () -&gt; raise Not_found) soi () ~~&gt; &quot;exception&quot;
----- Question 2 ★★ -----
Write a function f2 : ∀ α . ααα list → α list
f2 expects three arguments: x, y, and a list l.
f2 must return the list l without the part which is between x and y.
More precisely :
- if x does not occur in the list l, return l.
- if x occurs in l, return the list l without the part between the first x and the first y occurring strictly after x.
- if x occurs in l, but no y is found strictly after x, returns the list from the beginning until the first x (without x).
Examples
let l = [ 1 ; 2 ; 3 ; 3 ; 4 ; 5 ; 6 ; 7 ] (* Notice: 3 occurs twice. *)
f2 9 5 l ~~&gt; l
f2 5 5 l ~~&gt; [ 1 ; 2 ; 3 ; 3 ; 4 ]
f2 3 5 l ~~&gt; [ 1 ; 2 ; 6 ; 7 ]
f2 3 0 l ~~&gt; [ 1 ; 2 ]
f2 3 3 l ~~&gt; [ 1 ; 2 ; 4 ; 5 ; 6 ; 7 ]
f2 1 3 l ~~&gt; [ 3 ; 4 ; 5 ; 6 ; 7 ]
----- Question 3 ★★ -----
Open types_tree.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a function f3 : ∀ α . α tree → α
f2 expects a tree (as defined in Types_tree).
f2 must return the maximum value found in the tree (remember that comparisons are polymorphic).
Examples
Take a look at test tree number 4304:
q3_qvalue 4304 ;; (* A tree with six nodes should be printed, with maximal value 3 (assuming I have the same tree than you). *)
Then, you can directly test your function with
q3_invok f3 4304 ;;
Another test: (q3_qvalue 1702) has 8 nodes, max value is 4.
----- Question 4 ★ -----
Open types_filter.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a value f4 : (int × int) filter
f4 is a filter of pair of integers.
It forbids pairs with equal elements, like (5,5), with the message &quot;equal&quot;.
It accepts pairs (x,y) such that x is smaller than y.
You just have to define the filter. It will be applied automatically.
You don't have to return result values like Accept, Reject or Forbid.
Examples
if filter f4 is applied to (2,2) ~~&gt; Forbid &quot;equal&quot;
if filter f4 applied to (2,3) ~~&gt; Accept
if filter f4 applied to (3,2) ~~&gt; Reject
----- Question 5 ★ -----
Open types_filter.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a function f5 : ∀ α . α filter → (α option) filter
f5 takes a filter g and returns a new filter working on options.
None is not forbidden. Some x is forbidden if and only if x is forbidden by g.
None is rejected. Some x is accepted if and only if x is accepted by f.
Examples
In the tests, we use predefined filters, mentionned at the end of types_filter.ml
f5 filter_even applied to Some (-5) ~~&gt; Forbid &quot;negative&quot;
f5 filter_even applied to Some (5) ~~&gt; Reject
f5 filter_even applied to Some (6) ~~&gt; Accept
f5 filter_even applied to None ~~&gt; Reject
----- Question 6 ★★ -----
Open types_filter.ml (with pluma) and read the type definitions (but do NOT copy it in your program).
Write a function f6 : ∀ α . α filter → (α list) filter
f6 takes a filter g working on values of type 'a. It returns a new filter operating on lists of 'a.
f6 forbids a list if and only if the list is not empty and all its elements are forbidden by g.
In that case, forbid returns the message &quot;all&quot;.
f6 accepts a list if and only if g accepts at least one element of the list.
Examples
In the tests, we use predefined filters, mentionned at the end of types_filter.ml
f6 filter_even applied to [] ~~&gt; Reject
f6 filter_even applied to [-8,-2,-3] ~~&gt; Forbid &quot;all&quot;
f6 filter_even applied to [-8,-5,4] ~~&gt; Accept
f6 filter_even applied to [3, 8, 7] ~~&gt; Accept
f6 filter_even applied to [3, -6, 7] ~~&gt; Reject
----- Question 7 ★★★ -----
Write a function f7 : int → (int × int) list → int list
f7 expects a node n and a directed graph.
The node n is only a number, e.g. 12.
The graph is given as a list of all arcs, e.g. [ (12, 4) ; (12, 3) ] meaning that there are two arcs from node 12 to nodes 3 and 4.
The graph may contain loops : (7,7)
The graph may contain cycles : (3,6) (6,1) (1,3)
f7 must return the list of all nodes that can be reached from node n, including n itself, in any order.
For instance, in the cycle above, the nodes [ 3 ; 6 ; 1 ] can be reached from node 3.
Nodes must occur at most once in the result. No duplicates.
Be careful, this is a graph, your recursion must not loop forever.
Examples
f7 0 [ ] ~~&gt; 0
f7 1 [ (1, 2) ; (2, 3) ; (3, 4) ; (4, 5) ; (5, 6) ] ~~&gt; [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ]
f7 0 [ (3, 9) ; (4, 2) ; (2, 5) ; (0, 4) ; (4, 9) ] ~~&gt; [ 0 ; 4 ; 9 ; 2 ; 5 ]
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="uWNETmkjFPCk">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,77 @@
(*** Keep these lines. ***)
let qfile = "qqs.ml" ;;
let formatter = Format.(make_formatter (fun _ _ _ -> ()) (fun () -> ())) ;;
Topdirs.dir_use formatter (if Sys.file_exists qfile then qfile else Sys.getenv "HOME" ^ "/Exam-OCaml/" ^ qfile) ;;
(************************************)
(***** QUESTION 1 *****)
(* Modify this incorrect definition of f1 *)
let f1 x = failwith "f1 not done "
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f1 does not have the expected type. *)
let () = q1 { ff = f1 } ;;
(***** QUESTION 2 *****)
(* Modify this incorrect definition of f2 *)
let f2 x = failwith "f2 not done "
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f2 does not have the expected type. *)
let () = q2 { ff = f2 } ;;
(***** QUESTION 3 *****)
(* Modify this incorrect definition of f3 *)
let f3 x = failwith "f3 not done "
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f3 does not have the expected type. *)
let () = q3 { ff = f3 } ;;
(***** QUESTION 4 *****)
(* Modify this incorrect definition of f4 *)
let f4 = undone_filter
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f4 does not have the expected type. *)
let () = q4 { ff = f4 } ;;
(***** QUESTION 5 *****)
(* Modify this incorrect definition of f5 *)
let f5 x = failwith "f5 not done "
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f5 does not have the expected type. *)
let () = q5 { ff = f5 } ;;
(***** QUESTION 6 *****)
(* Modify this incorrect definition of f6 *)
let f6 x = failwith "f6 not done "
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f6 does not have the expected type. *)
let () = q6 { ff = f6 } ;;
(***** QUESTION 7 *****)
(* Modify this incorrect definition of f7 *)
let f7 x = failwith "f7 not done "
;;
(* ✔✔✔ Check your answer - keep the following line untouched.
If you get a type error here, it means that your function f7 does not have the expected type. *)
let () = q7 { ff = f7 } ;;

View file

@ -0,0 +1,67 @@
#use "topfind" ;;
#require "ocheck" ;;
open Ocheck_base ;;
open Ocheck ;;
let chk = Checker.auto_init () ;;
open Qf1
type t1 = tf1_4 = { ff: 'a 'b . ('a -> 'b) -> ('b -> string) -> ('a -> string) }
let q1 : t1 -> unit = qf1_4.out_launch "f1" chk
let q1_qvalue = qf1_4.qenum_args (* int: test index *)
let q1_invok = qf1_4.invok (* int: test index *)
let q1_arg2s = qf1_4.arg2s
let q1_res2s = qf1_4.res2s
open Qf1
type t2 = tf1_2 = { ff: 'a . 'a -> 'a -> 'a list -> 'a list }
let q2 : t2 -> unit = qf1_2.out_launch "f2" chk
let q2_qvalue = qf1_2.qenum_args (* int: test index *)
let q2_invok = qf1_2.invok (* int: test index *)
let q2_arg2s = qf1_2.arg2s
let q2_res2s = qf1_2.res2s
open Qf2
type t3 = tf2_1 = { ff: 'a . 'a tree -> 'a }
let q3 : t3 -> unit = qf2_1.out_launch "f3" chk
let q3_qvalue = qf2_1.qenum_args (* int: test index *)
let q3_invok = qf2_1.invok (* int: test index *)
let q3_arg2s = qf2_1.arg2s
let q3_res2s = qf2_1.res2s
let pp fmt v = Format.pp_print_string fmt Qf2.(sotree soi v) ;;
#install_printer pp ;;
open Qf3
type t4 = tf3_1 = { ff: (int * int) filter }
let q4 : t4 -> unit = qf3_1.out_launch "f4" chk
let q4_qvalue = qf3_1.qenum_args (* int: test index *)
let q4_invok = qf3_1.invok (* int: test index *)
let q4_arg2s = qf3_1.arg2s
let q4_res2s = qf3_1.res2s
open Qf3
type t5 = tf3_3 = { ff: 'a . 'a filter -> ('a option) filter }
let q5 : t5 -> unit = qf3_3.out_launch "f5" chk
let q5_qvalue = qf3_3.qenum_args (* int: test index *)
let q5_invok = qf3_3.invok (* int: test index *)
let q5_arg2s = qf3_3.arg2s
let q5_res2s = qf3_3.res2s
open Qf3
type t6 = tf3_6 = { ff: 'a . 'a filter -> ('a list) filter }
let q6 : t6 -> unit = qf3_6.out_launch "f6" chk
let q6_qvalue = qf3_6.qenum_args (* int: test index *)
let q6_invok = qf3_6.invok (* int: test index *)
let q6_arg2s = qf3_6.arg2s
let q6_res2s = qf3_6.res2s
open Qf2
type t7 = tf2_3 = { ff: int -> (int * int) list -> int list }
let q7 : t7 -> unit = qf2_3.out_launch "f7" chk
let q7_qvalue = qf2_3.qenum_args (* int: test index *)
let q7_invok = qf2_3.invok (* int: test index *)
let q7_arg2s = qf2_3.arg2s
let q7_res2s = qf2_3.res2s

View file

@ -0,0 +1,44 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>types_filter.ml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">types_filter.ml</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-2/types_filter.ml" data-eliom-c-onclick="KA7fhEbmWuTc">Download file: types_filter.ml</a></div><code class="page block">
<span class="comment">(* This is a filter: it tests values of type 'a.
* A filter contains two functions: forbid and f (see the type definition).
*
* We say that a filter:
* - forbids a value x if forbid x returns a non-empty string.
* - accepts a value x if f x returns true
* - rejects a value x if f x returns false
*
* The forbid test is done first: if a value is forbidden, it cannot be accepted or rejected.
*
* Example with an int filter :
* if forbid (-1) returns &quot;a negative number, this is insane!&quot;, this means that -1 is a forbidden value.
* if forbid 12 returns &quot;&quot;, this means that 12 is not forbidden.
*
* if f 12 returns true, this means that 12 is accepted.
* if f 17 returns false, this means that 17 is rejected (assuming 17 is not forbidden either: forbid 17 returns &quot;&quot;)
*)</span>
<span class="kw">type</span> 'a filter =
{ forbid: ('a -&gt; string) ;
f: ('a -&gt; bool) }
<span class="comment">(* When a filter is applied to a value, it returns one of these. *)</span>
<span class="kw">type</span> result = <span class="uident">Forbid</span> <span class="kw">of</span> string | <span class="uident">Accept</span> | <span class="uident">Reject</span>
<span class="comment">(* Forbid contains the message returned by the forbid function.
* It cannot be empty. *)</span>
<span class="comment">(* In some tests, we use some predefined filters :
*
* filter_even : int filter, forbids : negative numbers, accepts : even numbers (2,4,6,8, ...)
* filter_odd : int filter, forbids : 0, accepts : odd numbers (1,3,-5,-7,...)
*
* filter_pair : (int*int) filter, forbids pairs (a,b) when a is a negative number, accepts pairs with equal values, e.g. (4,4)
*
*)</span>
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="tMmOzWhMzM9u">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="fr" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>types_tree.ml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../../../ystyle.css" rel="stylesheet"/><script src="../../../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">types_tree.ml</h1></div><main><div class="download-link"><a class="caml_c" href="../../../Ressources/Ocaml/Corriges-Web/Sujet2020/Sujet-2/types_tree.ml" data-eliom-c-onclick="N0ywg/aF+w2W">Download file: types_tree.ml</a></div><code class="page block">
<span class="comment">(* This type defines a tree.
* Each node has a value of type 'a,
* and it can have any number of childs (subtrees). *)</span>
<span class="kw">type</span> 'a tree = <span class="uident">Node</span> <span class="kw">of</span> 'a * ('a tree list)
</code></main><footer><small class="pcom"><a class="caml_c" href="../../../yversion.html" data-eliom-c-onclick="iZCrQ9k2ThrK">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../../../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../../../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../../../Images/html5.png"/></a></footer></body></html>

61
Y/OCaml/project.html Normal file
View file

@ -0,0 +1,61 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="en" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>OCaml - mini project</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="../ystyle.css" rel="stylesheet"/><script src="../Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">OCaml - mini project</h1><nav><a class="caml_c" title="Back to main page" href="../ocaml.html" data-eliom-c-onclick="bOmVzvczyxF3"></a></nav></div><main><section class="yfold"><h3 class="yfold-title" onclick="toggleYfold('8a36b6ec')"><span class="arrow" id="arrow-8a36b6ec">&#x25b8;</span>Setup and compilation</h3><div class="yfold-content" id="content-8a36b6ec" data-yfold-default="hide"><ul class="steps"><li>To begin with, create a directory for this project.</li><li>In order to save time, a few modules have already been written. <br/>Download these files <small class="pcom">(save link target as...)</small>: <span class="file"><a class="caml_c" href="project-files/graph.mli" data-eliom-c-onclick="0uSqfBGDN549">graph.mli</a></span>, <span class="file"><a class="caml_c" href="project-files/graph.ml" data-eliom-c-onclick="AkPsJ43v5R2W">graph.ml</a></span>, <span class="file"><a class="caml_c" href="project-files/gfile.mli" data-eliom-c-onclick="BcpQIw927mux">gfile.mli</a></span>, <span class="file"><a class="caml_c" href="project-files/gfile.ml" data-eliom-c-onclick="irnXIzp0LvLL">gfile.ml</a></span>, <span class="file"><a class="caml_c" href="project-files/ftest.ml" data-eliom-c-onclick="PBCKLM+8oRx9">ftest.ml</a></span>. <br/>We will explain these files soon.</li><li>Since you like downloading stuff, you may also save these files: <span class="file"><a class="caml_c" href="project-files/graph1" data-eliom-c-onclick="o8X2TiD3cChj">graph1</a></span>, <span class="file"><a class="caml_c" href="project-files/graph1.svg" data-eliom-c-onclick="uca+xD0CCdCZ">graph1.svg</a></span>. </li><li>The main file is <span class="hfile">ftest.ml</span> so, we try to compile it, as taught in lesson 6: <br/><code class="command">ocamlc -o ftest ftest.ml</code><small class="pcom">(try it) </small><code class="inline">-o ftest</code> indicates the name of the executable.<br/><samp class="errmsg">Error: Unbound module Gfile</samp></li><li>Look at the beginning of <span class="file">ftest.ml</span>: we open a module <code class="inline">Gfile</code>. <br/>Lesson learnt: <q>The modules used by the program must be compiled <b>before</b> the program.</q></li><li><code class="command">ocamlc -c graph.ml gfile.ml</code><code class="inline">-c</code> means compile only, no executable is built.<br/><samp class="errmsg">Error: Could not find the .cmi file for interface graph.mli</samp></li><li>A file named <span class="file">graph.mli</span> exists. It is the <b>interface</b> of module <code class="block"><span class="uident">Graph</span>
</code><br/>Lesson learnt: <q>foo.mli must be compiled <b>before</b> foo.ml.</q></li><li><code class="command">ocamlc -c graph.mli graph.ml gfile.mli gfile.ml</code><br/><small class="pcom">something works, at last.</small></li><li>See which files have been produced: <code class="command">ls -l</code><small class="pcom"> (or more professionally, ls -alhv)</small><br/><div class="examples"><div class="example"><h5>Compilation of C files</h5><table><tr><td class="col1"><span class="hfile">file.c</span></td><td class="col2"> compiled into </td><td class="col3"><span class="hfile">file.o</span></td><td class="col4"><small class="pcom"> (by gcc)</small></td></tr></table></div><div class="example"><h5>Compilation of OCaml files</h5><table><tr><td class="col1"><span class="hfile">file.ml</span></td><td class="col2"> compiled into </td><td class="col3"><span class="hfile">file.cmo</span></td><td class="col4"><small class="pcom"> (by ocamlc)</small></td></tr><tr><td class="col1"><span class="hfile">file.mli</span></td><td class="col2"> compiled into </td><td class="col3"><span class="hfile">file.cmi</span></td><td class="col4"><small class="pcom"> (by ocamlc)</small></td></tr></table></div></div></li><li><code class="command">ocamlc -o ftest ftest.ml</code><br/><samp class="errmsg">Error: Required module `Gfile' is unavailable</samp></li><li>This error does not come from the <i>compiler</i> but from the <i>linker</i>. <br/>Lesson learnt: <q>When linking (building an executable), the modules must be explicitly given.</q></li><li><code class="command">ocamlc -o ftest graph.cmo gfile.cmo ftest.ml</code><br/>Note: <q>When linking, the modules must be explicitly given <b> in the order of dependencies.</b></q></li><li>You have obtained an executable <span class="file">ftest</span>. </li><li><code class="command">./ftest</code><small class="pcom"> (You get a usage message.)</small></li><li>Whenever you modify a file, you will have to recompile it as well as <b>all</b> the files that depend on it.</li></ul></div><script>
//<![CDATA[
initYfold('8a36b6ec') ;
//]]>
</script></section><section class="yfold"><h3 class="yfold-title" onclick="toggleYfold('9588387f')"><span class="arrow" id="arrow-9588387f">&#x25b8;</span>A few more things about compilation</h3><div class="yfold-content" id="content-9588387f" data-yfold-default="hide"><ul class="steps"><li>Recompiling everything is boring. This is why tools such as <a target="_blank" href="https://www.gnu.org/software/make/manual/html_node/index.html">GNU make</a><small class="pcom"> (for almost everything)</small> or <a target="_blank" href="https://www.tutorialspoint.com/ant/index.htm">ant</a><small class="pcom"> (for Java) </small> exist. <small class="pcom">Actually, the java compiler already takes care of dependencies.</small></li><li>A couple of build tools exist for OCaml too. Let us give a try to <b>ocamlbuild</b>. </li><li>First, remove all the compilation files: <code class="command">rm -f *.cmi *.cmo ftest</code><small class="pcom">(In order to avoid confusion between tools, ocamlbuild refuses to work if it finds compiled files.)</small></li><li>Use <code class="command">ocamlbuild ftest.byte</code> to build the bytecode executable, or <code class="command">ocamlbuild ftest.native</code> to build the native executable. (You need only one of them).<br/><small class="pcom">In case you ask, the compiled files can be found in a directory named <span class="file">_build</span>. </small></li><li><code class="command">./ftest.byte</code> or <code class="command">./ftest.native</code><small class="pcom">(still prints a usage message, though).</small></li></ul></div><script>
//<![CDATA[
initYfold('9588387f') ;
//]]>
</script></section><section class="yfold"><h3 class="yfold-title" onclick="toggleYfold('14504082')"><span class="arrow" id="arrow-14504082">&#x25b8;</span>Discover the project modules</h3><div class="yfold-content" id="content-14504082" data-yfold-default="hide"><ul class="steps"><li>You cannot compile the project using ocaml-top. Use any text editor instead, e.g. <code class="command">emacs</code><small class="pcom"> (configured as explained at the beginning of <a class="caml_c" href="sujet6.html" data-eliom-c-onclick="X4YxKfFrPGLk">OCaml - Lesson 6</a>) </small> or visual studio code, if you know how to launch it.<br/>You may enjoy A. Bit-Monnot's setup <b>configuration for VSCode</b> : <a target="_blank" href="https://github.com/arthur-bit-monnot/ocaml-maxflow-project">ocaml-maxflow-project on github</a>. </li><li>The base project contains two modules and a main program:<ul><li><span class="file">graph.mli</span> and <span class="file">graph.ml</span> which define a module <code class="block"><span class="uident">Graph</span>
</code></li><li><span class="file">gfile.mli</span> and <span class="file">gfile.ml</span> which define a module <code class="block"><span class="uident">Gfile</span>
</code></li><li><span class="file">ftest.ml</span>, the main program.</li></ul></li><li>Look at the interfaces of <code class="block"><span class="uident">Graph</span>
</code>(<span class="file">graph.mli</span>) and <code class="block"><span class="uident">Gfile</span>
</code><br/>All functions are already implemented in <span class="file">graph.ml</span> and <span class="file">gfile.ml</span><br/><b>You must not modify the module <code class="block"><span class="uident">Graph</span>
</code></b>, but you will have to create new modules and to modify Gfile. </li><li>Try to understand the graph file format (look at the example <span class="file">graph1</span> and read quickly gfile.ml)</li><li>To ease the writing of algorithms, write a new module <code class="block"><span class="uident">Tools</span>
</code>, with the following signature <small class="pcom">(the signature must be in file tools.mli)</small> :<br/><div class="examples"><div class="example"><h5>Interface file</h5><code class="block"><span class="kw">open</span> <span class="uident">Graph</span>
<span class="kw">val</span> clone_nodes: 'a graph -&gt; 'b graph
<span class="kw">val</span> gmap: 'a graph -&gt; ('a -&gt; 'b) -&gt; 'b graph
<span class="kw">val</span> add_arc: int graph -&gt; id -&gt; id -&gt; int -&gt; int graph
</code></div><div class="example"><h5>Implementation file</h5><code class="block"><span class="comment">(* Yes, we have to repeat open Graph. *)</span>
<span class="kw">open</span> <span class="uident">Graph</span>
<span class="comment">(* assert false is of type ∀α.α, so the type-checker is happy. *)</span>
<span class="kw">let</span> <span class="letvar">clone_nodes</span> gr = <span class="kw">assert</span> <span class="kw">false</span>
<span class="kw">let</span> <span class="letvar">gmap</span> gr f = <span class="kw">assert</span> <span class="kw">false</span>
...
</code></div></div><ul><li><code class="block">clone_nodes gr
</code> returns a new graph having the same nodes than gr, but no arc.<small class="pcom"> (code : one line)</small><br/>In order to find your errors more quickly, you may add an annotation : <code class="block"><span class="kw">let</span> <span class="letvar">clone_nodes</span> (gr:'a graph) = ...
</code></li><li><code class="block">gmap gr f
</code> maps all arcs of gr by function f.<small class="pcom"> (⩽3 lines)</small></li><li><code class="block">add_arc g id1 id2 n
</code> adds n to the value of the arc between id1 and id2. If the arc does not exist, it is created.</li></ul></li><li>In order to test, you may use an <a target="_blank" href="https://www-m9.ma.tum.de/graph-algorithms/flow-ford-fulkerson/index_en.html">online graph editor</a><span class="menu"> (Create a graph)</span>, and download your graphs.</li><li>In order to visualize graphs, we will use the famous <a target="_blank" href="http://www.graphviz.org/">Graphviz library</a>. <br/>Write a new function <code class="block">export
</code> in <code class="block"><span class="uident">Gfile</span>
</code> which writes a string graph in dot format (the format understood by graphviz). To understand the expected format, you just have to look at <a target="_blank" href="https://graphviz.gitlab.io/_pages/Gallery/directed/fsm.html">this example</a><small class="pcom"> (click on the picture to get the source file)</small>. <br/>To generate an image from a dot file, you can use: <br/><code class="command">dot -Tsvg your-dot-file &gt; some-output-file.svg</code></li></ul></div><script>
//<![CDATA[
initYfold('14504082') ;
//]]>
</script></section><section class="yfold"><h3 class="yfold-title" onclick="toggleYfold('41d55fee')"><span class="arrow" id="arrow-41d55fee">&#x25b8;</span>To be done</h3><div class="yfold-content" id="content-41d55fee" data-yfold-default="hide"><ul class="steps"><li><b>Minimal acceptable project: </b>you should at least implement the Ford-Fulkerson algorithm (in a separate module) and test it on several examples. <ul><li>You all remember the <a target="_blank" href="https://homepages.laas.fr/huguet/drupal/content/enseignement-3mic"> 3 MIC lecture on Graphs</a> by Marie-Jo Huguet.</li><li><a target="_blank" href="https://www-m9.ma.tum.de/graph-algorithms/flow-ford-fulkerson/index_en.html">An interactive presentation of the algorithm</a>. </li><li>A<a target="_blank" href="https://www-npa.lip6.fr/blin/enseignements/"> french presentation of flow problems</a> (see &quot;Algorithmes des graphes / Flots&quot;). </li></ul></li><li><b>Medium project: </b>find a use-case of this algorithm (e.g. network transportation, bandwidth, or bipartite matching) and write a program that solves the problem, given input files easier to write than graph files. <ul><li><a target="_blank" href="https://hackernoon.com/max-flow-algorithm-in-real-life-551ebd781b25">An example with money sharing.</a></li><li>Wikipedia offers several examples on its <a target="_blank" href="https://en.wikipedia.org/wiki/Maximum_flow_problem">max flow page</a>. </li><li><a target="_blank" href="https://medium.com/swlh/real-world-network-flow-cricket-elimination-problem-55a3036a5d60">The cricket elimination problem</a></li><li><a target="_blank" href="https://www.anishathalye.com/2015/09/24/algorithms-in-the-real-world-host-matching/">Finding where guests will sleep.</a></li><li><a target="_blank" href="https://www.geeksforgeeks.org/maximum-bipartite-matching/">Bipartite matching</a></li><li><a target="_blank" href="http://www.cs.toronto.edu/~sven/Papers/bipartite.pdf">Object recognition through bipartite matching</a></li></ul></li><li><b>Better project: </b>enhance the medium project by taking into account other constraints - and implementing the max-flow min-cost algorithm.<br/>As an example, your program could be used to match a set of people to a set of ressources (e.g. students to <i>projets tutorés</i>, or future students to universites as in late APB) taking into account people's preferences. Your system should be as fair as possible and avoid biases.</li></ul></div><script>
//<![CDATA[
initYfold('41d55fee') ;
//]]>
</script></section><section class="yfold"><h3 class="yfold-title" onclick="toggleYfold('ee09a9ff')"><span class="arrow" id="arrow-ee09a9ff">&#x25b8;</span>Some technical support</h3><div class="yfold-content" id="content-ee09a9ff" data-yfold-default="hide"><h4>Debugging</h4>In order to activate debugging: <ul class="steps"><li>You must create a file named <span class="hfile">_tags</span> in your project directory (this file is read by ocamlbuild).<br/>Add the following line: <pre>true: debug</pre></li><li>In the terminal, set the variable <var>OCAMLRUNPARAM</var> to <var>b</var><small class="pcom"> (means: backtrace)</small><br/><code class="command">export OCAMLRUNPARAM=&quot;b&quot;</code>then, you can launch your program.</li></ul><h4>Packages</h4><ul><li>If you use special modules, such as <var>Unix</var><small class="pcom"> (package name: unix)</small> or <var>Graphics</var><small class="pcom"> (package name: graphics)</small>, you need to tell ocamlbuild to use the corresponding package. In the file <span class="hfile">_tags</span>, add the following line: <pre>true: package(unix)</pre> or <pre>true: package(unix,graphics,...)</pre></li><li>In order to build, you have to use:<code class="command">ocamlbuild -use-ocamlfind myprog.native</code><br class="bbr"/><b>ocamlfind</b> (findlib) is a tool written by a prolific OCaml developper: <a target="_blank" href="http://www.gerd-stolpmann.de/buero/freie_sw_beitraege.html.en">Gerd Stolpmann</a>. It is a library manager for OCaml.</li></ul><h4>How to start writing the Ford-Fulkerson algorithm (alert: SPOILER)</h4><p>If you are stuck, you may start by writing a function which finds a path in a graph:</p><code class="block">
<span class="comment">(* A path is a list of nodes. *)</span>
<span class="kw">type</span> path = id list
<span class="comment">(* find_path gr forbidden id1 id2
* returns None if no path can be found.
* returns Some p if a path p from id1 to id2 has been found.
*
* forbidden is a list of forbidden nodes (they have already been visited)
*)</span>
find_path: int graph -&gt; id list -&gt; id -&gt; id -&gt; path option
</code></div><script>
//<![CDATA[
initYfold('ee09a9ff') ;
//]]>
</script></section></main><footer><small class="pcom"><a class="caml_c" href="../yversion.html" data-eliom-c-onclick="QkmB87Beytxc">Version information</a></small><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:73px;height:28px;" alt="INSA logo" src="../Images/logo-insa-light.jpg"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="../Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="../Images/html5.png"/></a></footer></body></html>

72
Y/ocaml.html Normal file
View file

@ -0,0 +1,72 @@
<!DOCTYPE html>
<!-- Page generated by OCaml with Ocsigen.
See http://ocsigen.org/ and http://caml.inria.fr/ for information -->
<html class="ocaml" lang="en" id="h" xmlns="http://www.w3.org/1999/xhtml"><head><title>Functional programming in OCaml</title><meta content="text/html; charset=utf-8" http-equiv="content-type"/><link media="all" href="ystyle.css" rel="stylesheet"/><script src="Scripts/yfold.js"></script></head><body><div class="header" id="header"><h1 id="title">Functional programming in OCaml</h1></div><main><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('f6c61644')"><span class="arrow" id="arrow-f6c61644">&#x25b8;</span>Introduction</h2><div class="yfold-content flex" id="content-f6c61644" data-yfold-default="hide"><section class="debug flex2 card"><h3 class="debug">About the course</h3><p class="debug">Intended for functional beginners already familiar with some programming language.</p><h4 class="debug">Goals</h4><p class="debug">If all goes well, by the end of this course, you shall be able to:</p><ul class="debug success"><li>Understand <b class="debug">lambda-terms</b> and write <b class="debug">pure functional programs</b>. </li><li>Design <b class="debug">recursive functions</b> to iterate over recursive <b class="debug">data types</b>. </li><li>Wonder how you have survived so far without <b class="debug">algebraic data types</b> and <b class="debug">parameterized types</b>. </li><li>Think in terms of <b class="debug">higher-order functions</b> in order to write reusable code. </li><li>Astonish yourself by writing <b class="debug">polymorphic functions</b> without even noticing. </li></ul><h4 class="debug">Evaluation</h4><p class="debug">The exam is on october, 12th: a list of exercises, on computer.</p></section><section class="debug flex1 card"><h3 class="debug">Setup</h3><ul class="debug steps"><li>To use OCaml at INSA, copy to your homedir <span class="debug file">/mnt/commetud/GEI/OCaml/.profile</span> and <b class="debug">read it.</b></li><li>Open a new terminal and launch utop: <code class="debug command">utop</code><br class="debug vskip"/>You should get a nice greeting message. Press CTRL+D to quit.</li><li>Alternatively, you can write small OCaml programs directly on <a class="debug" target="_blank" href="https://try.ocamlpro.com/">Try OCaml</a>. </li></ul></section></div><script>
//<![CDATA[
initYfold('f6c61644') ;
//]]>
</script></section><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('bf745052')"><span class="arrow" id="arrow-bf745052">&#x25b8;</span>Programming assignments (lessons)</h2><div class="yfold-content" id="content-bf745052" data-yfold-default="hide"><p>Each lesson must be worked <b>individually</b>, and completed before starting the next one.</p><aside class="answers"><h4>Answers</h4><ul><li>Lesson 1: <a class="caml_c" href="OCaml/all-answers/xkzb/lesson1.ml.html" data-eliom-c-onclick="1E4MGQOS9wC5">lesson1.ml</a></li><li>Lesson 2: <a class="caml_c" href="OCaml/all-answers/hitj/lesson2.ml.html" data-eliom-c-onclick="Grw/MZP+WPeg">lesson2.ml</a></li><li>Lesson 3: <a class="caml_c" href="OCaml/all-answers/lcdz/lesson3.ml.html" data-eliom-c-onclick="xypUTosu6RUM">lesson3.ml</a></li><li>Lesson 4: <a class="caml_c" href="OCaml/all-answers/pmyd/lesson4.ml.html" data-eliom-c-onclick="PCFy+czkv2Rs">lesson4.ml</a></li><li>Lesson 5: <a class="caml_c" href="OCaml/all-answers/fwiz/lesson5.ml.html" data-eliom-c-onclick="R4j/cMya1IJ+">lesson5.ml</a></li><li>Lesson 6: <a class="caml_c" href="OCaml/all-answers/dmug/lesson6.ml.html" data-eliom-c-onclick="vZsEvcolLYjP">lesson6.ml</a></li></ul><small class="pcom">Some solutions might be out of date, or unclear&#8212;<br/>signal them to Mr. Le Botlan, then.</small></aside><ul class="lessons"><li><a class="caml_c" href="OCaml/sujet1.html" data-eliom-c-onclick="a9t25dVDYCcf"><span class="lbutton">Lesson 1</span>The ocaml interpreter, ground types, built-in types.</a></li><li><a class="caml_c" href="OCaml/sujet2.html" data-eliom-c-onclick="v9VgLpqlyO9u"><span class="lbutton">Lesson 2</span>Functions, curryfication.</a></li><li><a class="caml_c" href="OCaml/sujet3.html" data-eliom-c-onclick="nVoR9pY+HmLK"><span class="lbutton">Lesson 3</span>Pattern matching, inner let.</a></li><li><a class="caml_c" href="OCaml/sujet4.html" data-eliom-c-onclick="x/OhOTSgZ4w2"><span class="lbutton">Lesson 4</span>Type structures.</a></li><li><a class="caml_c" href="OCaml/sujet5.html" data-eliom-c-onclick="B3smUwRNlwiX"><span class="lbutton">Lesson 5</span>Algorithms and more type structures</a></li><li><a class="caml_c" href="OCaml/sujet6.html" data-eliom-c-onclick="s69LFl2Iqlfj"><span class="lbutton">Lesson 6</span>Compilation, exceptions, side effects.</a></li></ul><br class="vskip"/><h4>For the curious, some complementary information</h4><ul><li><a target="_blank" href="https://reasonml.github.io/">Facebook's REASON</a>, or how to program in OCaml while pretending to write Javascript.</li><li>Xavier Leroy, proficient developper of the Ocaml compiler, tells <a target="_blank" href="http://gallium.inria.fr/blog/intel-skylake-bug/">how he ran into a bug in some Intel processors</a>. </li><li>Watch 👁<a target="_blank" href="https://youtu.be/-PX0BV9hGZY">Tail-call recursion, the musical</a>, which explains how call-stacks work.</li><li>Learn why OCaml's <a target="_blank" href="http://www.ocamlpro.com/2020/03/23/ocaml-new-best-fit-garbage-collector/">Garbage collector</a> is efficient.</li></ul></div><script>
//<![CDATA[
initYfold('bf745052') ;
//]]>
</script></section><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('39af12ce')"><span class="arrow" id="arrow-39af12ce">&#x25b8;</span>Exam-type exercises</h2><div class="yfold-content" id="content-39af12ce" data-yfold-default="hide"><aside class="answers"><h4>Answers</h4><ul><li>Ex. 1: <a class="caml_c" href="OCaml/all-answers/cnkv/exo1.ml.html" data-eliom-c-onclick="34UoL/1d4mYp">exo1.ml</a></li><li>Ex. 2: <a class="caml_c" href="OCaml/all-answers/viuq/exo2.ml.html" data-eliom-c-onclick="pRryOPK+/Lta">exo2.ml</a></li><li>Ex. 3: <a class="caml_c" href="OCaml/all-answers/mfmd/exo3.ml.html" data-eliom-c-onclick="DaTtfOf8fMb5">exo3.ml</a></li></ul></aside><ul><li>Exam questions are only intended to test your skills. They are not intended to be smart or enlightening. Thus, do not get surprised if examples are totally unmeaningful.</li><li>You do not have to write tail-recursive functions, unless explicitly required.</li></ul><ul class="lessons"><li><a class="caml_c" href="OCaml/exercises1.html" data-eliom-c-onclick="/knwUQOtRXuQ"><span class="lbutton">Ex. 1</span>Basic types, curried functions, polymorphism, pattern matching</a></li><li><a class="caml_c" href="OCaml/exercises2.html" data-eliom-c-onclick="fQtG/3W6LzgO"><span class="lbutton">Ex. 2</span>Lists, records, variants.</a></li><li><a class="caml_c" href="OCaml/exercises3.html" data-eliom-c-onclick="xYhuiXiN7rkq"><span class="lbutton">Ex. 3</span>Recursive structures, exceptions</a></li><li><a target="_blank" href="https://ocaml.org/learn/tutorials/99problems.html"><span class="lbutton">Ex. 99</span>A list of 99 problems of various difficulty.</a><small class="pcom"> (Some of them are easier than the exam, some are harder.)</small></li><li><a class="caml_c" href="OCaml/exam-2017.html" data-eliom-c-onclick="gcIDKoTkIR6N"><span class="lbutton">Ex. 2017</span>Exam questions, 2017.</a></li><li><a class="caml_c" href="OCaml/exam-2019.html" data-eliom-c-onclick="bvximRdiy7/t"><span class="lbutton">Ex. 2019</span>Exam questions, 2019.</a></li><li><a class="caml_c" href="OCaml/exam-2020.html" data-eliom-c-onclick="wNGyeoaEg24S"><span class="lbutton">Ex. 2020</span>Exam questions, 2020.</a></li></ul><br class="vskip"/><h4 id="get-prepared">Get prepared for the exam:</h4><p>The following works only on INSA computers (purposely).</p><ul><li>You must have configured your account to use ocaml (see Setup above) </li><li>In a terminal, launch <code class="command">/mnt/commetud/GEI/run-exam &amp;</code></li><li>Choose the <b>OCaml Test Exam</b>. The expected password is <kbd>exam</kbd></li><li>This test exam contains two easy questions.</li></ul></div><script>
//<![CDATA[
initYfold('39af12ce') ;
//]]>
</script></section><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('0836c5b9')"><span class="arrow" id="arrow-0836c5b9">&#x25b8;</span>Project</h2><div class="yfold-content" id="content-0836c5b9" data-yfold-default="hide"><p>The project is to be done by pairs of students having the same level.</p><h4>Requirements</h4><p>➪ You have completed all the lessons, and know how to write <a target="_blank" href="https://ocaml.org/learn/tutorials/modules.html">OCaml Modules. </a></p><h4>How it goes</h4><ul class="steps"><li>The goal is to implement an algorithm computing the max-flow of a flow graph, using the FordFulkerson algorithm, and optionally improve it to take into account other constraints (e.g. minimize cost).<br/>See the <a class="caml_c" href="OCaml/project.html" data-eliom-c-onclick="N1JNdzMrXJaN">details of the project</a>. </li><li>You are free to propose another project, by considering another graph algorithm &#8212; check with the teacher.</li></ul><h4>Evaluation</h4><ul><li>Before <b>december, 12th</b>, you have to send to your teacher (D. Le Botlan or A. Bit-Monnot) a link to your github project <small class="pcom">(or any other similar repository)</small>. </li><li>Manage to meet your teacher and show him a very quick demo (2 minutes). He will then ask you questions about your project (5-10 minutes).</li></ul>The final grade takes into account:<ul><li>your achievements (how much is in your project)</li><li>your code's modularity (do you use modules, abstract types?)</li><li>the level of abstraction and genericity (do you take advantage of polymorphism, parameterized types?)</li><li>code quality (comments, conciseness)</li><li>your answers to questions during the quick demo</li></ul></div><script>
//<![CDATA[
initYfold('0836c5b9') ;
//]]>
</script></section><section class="yfold"><h2 class="yfold-title" onclick="toggleYfold('577bfcbf')"><span class="arrow" id="arrow-577bfcbf">&#x25b8;</span>Getting help</h2><div class="yfold-content flex" id="content-577bfcbf" data-yfold-default="hide"><section class="debug flex2 card"><h3 class="debug">Resources</h3><ul class="debug resources"><li>Many resources about OCaml: <a class="debug url" target="_blank" href="https://ocaml.org/">//ocaml.org/</a></li><li><a class="debug" target="_blank" href="https://ocaml.org/learn/books.html">Books </a>to learn OCaml.</li><li>The very <b class="debug">official </b><a class="debug" target="_blank" href="http://caml.inria.fr/pub/docs/manual-ocaml/">OCaml manual</a>. </li><li>A well-written french book about functional programming (Bib'INSA): <br/><b class="debug">Mini Manuel de Programmation Fonctionnelle</b>&#8212;<i class="debug">Éric Violard</i>&#8212;DUNOD 978-2-10-070385-2</li><li><a class="debug" target="_blank" href="https://ocamlverse.github.io/">OCamlverse </a>: some documentation about OCaml.</li></ul></section><section class="debug flex3 card"><h3 class="debug">OCaml at home</h3><ul class="debug"><li>See <a class="debug" target="_blank" href="https://ocaml.org/docs/install.html">how to install</a>. <br/><small class="debug pcom">The recommended way is OPAM on linux.</small></li><li>Install packages text, utop and ocaml-top.</li><li>On windows, see how to install <a class="debug" target="_blank" href="https://www.typerex.org/ocaml-top.html">ocaml-top</a>. <small class="debug pcom"> (Have you considered switching to a decent, free OS ?)</small></li></ul></section><br/><section class="stupid-flex-separator mml"><h5>A stupid separator, because flex sucks in css.</h5></section><section class="yfold card flex3"><h3 class="yfold-title" onclick="toggleYfold('19360194')"><span class="arrow" id="arrow-19360194">&#x25b8;</span>Trouble?</h3><div class="yfold-content" id="content-19360194" data-yfold-default="hide"><h4>Packages</h4><ul><li>If you need to use a package, e.g. <code class="inline">text</code> in an interpreter, you must start your program as follows:<br/><code class="block"><span class="directive">#use</span> <span class="string">&quot;topfind&quot;</span> <span class="semi">;;</span> <span class="comment">(* Done once *)</span>
<span class="directive">#require</span> <span class="string">&quot;text&quot;</span> <span class="semi">;;</span> <span class="comment">(* For every package you need. *)</span>
</code></li></ul><h4>Pitfalls</h4><ul><li><b>Sequence</b>: when writing a sequence, do not put a <code class="block">;
</code> on the last statement.<br/><code class="block"><span class="kw">let</span> <span class="letvar">f</span> () =
<span class="uident">Printf</span>.printf <span class="string">&quot;Meet&quot;</span> ;
<span class="uident">Printf</span>.printf <span class="string">&quot;John&quot;</span> ;
<span class="uident">Printf</span>.printf <span class="string">&quot;Doe&quot;</span> ;
<span class="kw">let</span> <span class="letvar">g</span> x = x + <span class="number">1</span> <span class="comment">(* Error detected here. *)</span>
<span class="kw">let</span> <span class="letvar">h</span> x = x - <span class="number">1</span>
</code><ul><li>An error is detected at the end of the definition of g.</li><li>It is difficult to understand why the error appears there (try to find out).</li><li>The real error is the extra <code class="inline">;</code> at the end of <var>f</var><small class="pcom"> (after &quot;Doe&quot;)</small>. </li></ul>To avoid this problem, always finish a sequence with unit (and no semicolumn), in order to mark clearly the end of the sequence:<br/><code class="block"><span class="kw">let</span> <span class="letvar">f</span> () =
<span class="uident">Printf</span>.printf <span class="string">&quot;Meet&quot;</span> ;
<span class="uident">Printf</span>.printf <span class="string">&quot;John&quot;</span> ;
<span class="uident">Printf</span>.printf <span class="string">&quot;Doe&quot;</span> ;
()
<span class="kw">let</span> <span class="letvar">g</span> x = x + <span class="number">1</span> <span class="comment">(* No error. *)</span>
<span class="kw">let</span> <span class="letvar">h</span> x = x - <span class="number">1</span>
</code></li><li><b>Nested pattern-matching: </b>be careful when a <code class="block"><span class="kw">match</span>
</code> occurs inside another pattern-matching:<br/><code class="block"><span class="comment">(* This (curried) function expects two arguments. *)</span>
<span class="kw">let</span> <span class="letvar">f</span> x = <span class="kw">function</span>
| <span class="number">0</span> -&gt; <span class="kw">false</span>
| <span class="number">1</span> -&gt; <span class="kw">match</span> x <span class="kw">with</span>
| <span class="number">0</span> -&gt; <span class="kw">false</span>
| <span class="number">1</span> -&gt; <span class="kw">true</span>
| <span class="number">2</span> -&gt; <span class="kw">true</span>
| _ -&gt; <span class="kw">false</span>
</code><br/>It does not behave as you think. If you auto-indent the code, you get the real meaning: <br/><code class="block"><span class="kw">let</span> <span class="letvar">f</span> x = <span class="kw">function</span>
| <span class="number">0</span> -&gt; <span class="kw">false</span>
| <span class="number">1</span> -&gt; <span class="kw">match</span> x <span class="kw">with</span>
| <span class="number">0</span> -&gt; <span class="kw">false</span>
| <span class="number">1</span> -&gt; <span class="kw">true</span>
| <span class="number">2</span> -&gt; <span class="kw">true</span>
| _ -&gt; <span class="kw">false</span>
</code><br/>To fix it, use <code class="kw">begin</code>...<code class="kw">end</code> or parentheses:<br/><code class="block"><span class="kw">let</span> <span class="letvar">f</span> x = <span class="kw">function</span>
| <span class="number">0</span> -&gt; <span class="kw">false</span>
| <span class="number">1</span> -&gt; <span class="kw">begin</span> <span class="kw">match</span> x <span class="kw">with</span>
| <span class="number">0</span> -&gt; <span class="kw">false</span>
| <span class="number">1</span> -&gt; <span class="kw">true</span>
<span class="kw">end</span>
| <span class="number">2</span> -&gt; <span class="kw">true</span>
| _ -&gt; <span class="kw">false</span>
</code><br/>When a <mark>match is nested in another pattern-matching</mark>, it is good practice to put <code class="kw">begin</code>...<code class="kw">end</code> around it.</li></ul></div><script>
//<![CDATA[
initYfold('19360194') ;
//]]>
</script></section><section class="card"><h3>Tutorials</h3><ul><li><a target="_blank" href="http://baturin.org/docs/ocaml-faq/">What I wish I knew when learning OCaml.</a></li><li><a target="_blank" href="https://ocaml.org/learn/tutorials/structure_of_ocaml_programs.html">Local variables (<code class="inline">let .. in ..</code>)</a></li><li><a target="_blank" href="https://medium.com/@bobbypriambodo/getting-your-feet-wet-with-ocaml-ea1045b6efbc">Getting your feet wet with OCaml</a><small class="pcom"> (how to gently install and discover OCaml)</small>. </li><li><a target="_blank" href="https://www.fun-mooc.fr/courses/course-v1:parisdiderot+56002+session04/about">Le MOOC OCaml</a><small class="pcom"> (démarre en septembre 2019)</small></li></ul></section><span class="lbutton mml"> contact.lebotlan ❅ insa-toulouse.fr </span></div><script>
//<![CDATA[
initYfold('577bfcbf') ;
//]]>
</script></section></main><footer><a target="_blank" href="http://www.insa-toulouse.fr"><img title="INSA Toulouse" style="width:194px;height:42px;" alt="INSA logo" src="Images/logo-insa.jpg"/></a><a target="_blank" href="https://www.laas.fr/"><img title="LAAS" style="width:82px;height:42px;" alt="LAAS logo" src="Images/laas.png"/></a><a target="_blank" href="http://www.univ-toulouse.fr/"><img title="University of Toulouse" style="width:84px;height:42px;" alt="University of Toulouse logo" src="Images/univt.jpg"/></a><small class="pcom"><a class="caml_c" href="yversion.html" data-eliom-c-onclick="HbfuNl/oa5yN">Version information</a></small><a target="_blank" href="http://ocaml.org/"><img title="Powered by OCaml code" style="width:42px;height:42px;" alt="OCaml logo" src="Images/ocaml-small.png"/></a><a target="_blank" href="http://ocsigen.org/"><img title="Website generated by ocsigen &amp; eliom" style="width:38px;height:32px;" alt="Ocsigen logo" src="Images/ocsigen-small.png"/></a><a target="_blank" href="http://jigsaw.w3.org/css-validator/check/referer"><img title="Validate css stylesheet" style="width:28px;height:32px;" alt="CSS3 logo" src="Images/css3.png"/></a><a target="_blank" href="http://validator.w3.org/check/referer"><img title="Validate html5 content" style="width:32px;height:32px;" alt="HTML5 logo" src="Images/html5.png"/></a></footer></body></html>

View file

@ -209,7 +209,7 @@ footer .pcom a {
/* PAGE DE CODE */
code:not(.inline) {
code:not(.inline), code:not(.command) {
position: relative;
z-index: 2;
background: rgba(19,19,19,.8);
@ -356,7 +356,7 @@ span.file {
font-weight: bold;
}
var, samp, code.inline {
var, samp, code.inline, code.kw, code.command {
display: inline-block;
padding: 2px 8px;
background: #222;
@ -379,7 +379,7 @@ img.space {
margin-bottom: 1.5ex;
}
span.lbutton{
span.lbutton {
position:relative;
margin-right: 6px;
}