115 lines
No EOL
8.7 KiB
HTML
115 lines
No EOL
8.7 KiB
HTML
<!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">"qqs.ml"</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> _ _ _ -> ()) (<span class="kw">fun</span> () -> ())) <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">"HOME"</span> ^ <span class="string">"/Exam-OCaml/"</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> _ -> <span class="string">"exception"</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>
|
|
| [] -> <span class="uident">List</span>.rev acu
|
|
| x <span class="operator">::</span> xs ->
|
|
<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>
|
|
| [] -> <span class="uident">List</span>.rev acu
|
|
| x <span class="operator">::</span> xs -> <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) -> <span class="kw">if</span> a = b <span class="kw">then</span> <span class="string">"equal"</span> <span class="kw">else</span> <span class="string">""</span>) ;
|
|
f = (<span class="kw">fun</span> (a,b) -> a < 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> -> <span class="string">""</span> | <span class="uident">Some</span> x -> f.forbid x) ;
|
|
f = (<span class="kw">function</span> <span class="uident">None</span> -> <span class="kw">false</span> | <span class="uident">Some</span> x -> 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">""</span>
|
|
<span class="kw">else</span> <span class="kw">if</span> <span class="uident">List</span>.for_all (<span class="kw">fun</span> x -> f.forbid x <> <span class="string">""</span>) l <span class="kw">then</span> <span class="string">"all"</span> <span class="kw">else</span> <span class="string">""</span>
|
|
|
|
<span class="kw">and</span> <span class="letvar">f</span> l = <span class="uident">List</span>.exists (<span class="kw">fun</span> x -> f.forbid x = <span class="string">""</span> && 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) -> <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 -> 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> |