118 linhas
Sem EOL
8,6 KiB
HTML
118 linhas
Sem EOL
8,6 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-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">"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 x =
|
|
<span class="kw">try</span> f x <span class="kw">with</span>
|
|
| <span class="uident">Invalid_argument</span> s -> <span class="kw">raise</span> (<span class="uident">Failure</span> s)
|
|
| <span class="uident">Failure</span> s -> <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>
|
|
| [] -> <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 (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>
|
|
| [] -> []
|
|
| x <span class="operator">::</span> xs -> <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> -> <span class="string">"zero"</span> | _ -> <span class="string">""</span>) ;
|
|
f = (<span class="kw">function</span> <span class="uident">None</span> -> <span class="kw">false</span> | <span class="uident">Some</span> x -> x > <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) -> f.forbid (b,a)) ;
|
|
f = (<span class="kw">fun</span> (a,b) -> 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 -> f.forbid x <> <span class="string">""</span>) l <span class="kw">with</span>
|
|
| <span class="uident">None</span> -> <span class="string">""</span>
|
|
| <span class="uident">Some</span> x -> 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 -> r && 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) -> <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="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> |