css-le-botlan/Y/OCaml/exam2020/Sujet-2/enonce.txt.html
2021-02-09 17:41:02 +01:00

171 lines
No EOL
8.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>