You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that for the exercise "An Implementation of List with an Efficient Concatenation" we can just implement the functions hd and tl this way:
let hd cl =
try Some (List.hd (to_list cl))
with _ -> None;;
let tl cl =
try Some (of_list (List.tl (to_list cl)))
with Failure _ -> None;;
Maybe List.hd and List.tl should be disallowed?
Is it really an accepted answer or souldn't we try to implement something with a full pattern matching over the variant type?
The text was updated successfully, but these errors were encountered:
@fccm thanks for your report. I transferred it to the learn-ocaml-corpus repo where it naturally belongs.
That's a good point indeed,
let hd cl =
try Some (List.hd (to_list cl))
with _ -> None;;
is definitely not the expected solution, at least in terms of complexity (especially because the presence of to_list).
Maybe List.hd and List.tl should be disallowed?
On the one hand, it is easy to disallow the use of any qualified identifier of module List, it suffices to write ast_sanity_check ~modules:["List"] code_ast @@ fun () -> in the test.ml grader file.
Given the teacher's solution.ml does not use any List.qualid, that looks fine to forbid this indeed (and edit the descr.html to make this constraint explicit).
On the other hand, that does not seem sufficient, e.g. because one could manually write:
let hd cl = match to_list cl with e :: _ -> Some e | [] -> None
which we might want to forbid as well because of to_list, which is not as trivial, but feasible also using Test_lib.
I tagged this issue as good-first-issue but feel free to propose a PR if you want — Cc @fccm@yurug
It seems that for the exercise "An Implementation of List with an Efficient Concatenation" we can just implement the functions hd and tl this way:
Maybe List.hd and List.tl should be disallowed?
Is it really an accepted answer or souldn't we try to implement something with a full pattern matching over the variant type?
The text was updated successfully, but these errors were encountered: