Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List.hd and List.tl for "An Implementation of List with an Efficient Concatenation" #36

Open
fccm opened this issue Jul 21, 2019 · 1 comment
Labels
good first issue Good for newcomers question Further information is requested

Comments

@fccm
Copy link

fccm commented Jul 21, 2019

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?

@EmileRolley EmileRolley added the question Further information is requested label Jun 22, 2021
@erikmd erikmd transferred this issue from ocaml-sf/learn-ocaml May 22, 2022
@erikmd erikmd added the good first issue Good for newcomers label May 22, 2022
@erikmd
Copy link
Member

erikmd commented May 22, 2022

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants