"this match case is unused"

Hello, I just started coding in ocaml and I am blocked on an exercise where I am supposed to create a function that count the number of times an x number appear on an odd rank in a list ( I don’t know if it’s understandable english is not my first language :confused: ) . So that’s what I came up with but the terminal sends ma a warning:
Warning 11: this match case is unused.
val nbf1 : 'a list -> 'a -> int = "

I think I’m supposed to ad an “in …” a the end of it but i’m not sure… f someone could help me figure it out.

Thanks in advance :slight_smile:

You mean that nbf1 [ 1 ; 2 ; 9 ; 5 ; 7 ; 9 ] 9 should give 1.
And nbf1 [ 9 ; 2 ; 9 ; 5 ; 9 ; 8 ] 9 should give 3.

With your code I see warning 8 (not exhaustive PM) and warning 11 (unused match case):

let nbf1 l x =
let rec nbf l x acc =
match l with 
  | [] -> acc
  | a::b::t -> if a = x then nbf t x (acc + 1) else nbf t x acc
  | a::b::[] -> if a = x then acc + 1 else acc  in 
nbf l x 0

Characters 37-169:
match l with
| [] -> acc
| a :: b :: t -> if a = x then nbf t x (acc + 1) else nbf t x acc
| a :: b :: [] -> if a = x then acc + 1 else acc
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
_ :: []
Characters 127-135:
| a :: b :: [] -> if a = x then acc + 1 else acc
Warning 11: this match case is unused.

Case 3 is case 2 with t = [], so it’s not a correct pattern-matching.

You should organize your match cases as follows:
empty list
list with one element
list with at least 2 elements with possibly empty tail:

let nbf1 l x =
let rec nbf l x acc =
match l with 
  | [] -> acc
  | a::[] -> if a = x then acc + 1 else acc
  | a::b::t -> if a = x then nbf t x (acc + 1) else nbf t x acc  in 
nbf l x 0

val nbf1 : 'a list -> 'a -> int = <fun>
# nbf1 [ 9 ; 2 ; 9 ; 5 ; 9 ; 8 ] 9
- : int = 3

I managed to get rid of the warning by adding a condition but it still doesn’t give me any results when I try the function with a certain list

You should use </> tool in the editor to highlight your OCaml program.

I encourage you to write your code in a strutured (and in a somehow modular way), as shown above.

I think I understood where my mistakes were thank you very much

I agree with I just find it hard to organize it on the terminal since I can’t use the tab button

At least use ledit
$ ledit ocaml
You can create an alias in your ~/.bashrc (or equivalent config file).

Or Utop.

For a serious work, it’s much better to use Emacs or Vim.

ocaml-top is certainly a good option to write clean and structured OCaml programs (but with no text edition features).

I am using ledit ocaml already but I’ll start using Emacs.

You can also use Visual Studio Code with the OCaml/Reason plugin. You will be able to benefit from autocomplete, and your programs will be typechecked on the fly with a tool called merlin, as long as you are writing files in a src directory. It basically means you will not have to recompile everything manually every time you add something to your program. The plugin also reads the other files in the directory, in case you are using elements that are defined in those other files. As a build tool, I suggest you use ocamlbuild or dune, which are powerful and pretty automatic.

1 Like

Thank you I’ll check these out too :smile:

As discussed in another topic a few months ago, VSC is enough (and nice) for editing code. But I didn’t find what is a key feature for programming and even a must have feature for learning: the REPL.
Has it recently changed?
How do you evaluate a selection/region/file?

This is the main point of people not using that kind of text editors. While I understand what you mean, I have personally not actively looked for a REPL feature in VSCode since I started using it. I still do not know whether that kind of tools exists. I actually started learning OCaml with only VSCode and a terminal in another window. I was launching rlwrap ocaml and loading the file with #use "file.ml". When I needed to evaluate the file again, I would just switch windows, press the up arrow and Enter. In more advanced projects, I even feel like autocomplete and static analysis are sufficient in order to be productive, as the tests are often macro tests of all the modules working together as a whole, on some input files for example. I do not want to generalise my case too much, but for my projects, as the language has really clear syntax and semantics, most of the errors are avoided with type checking.

I don’t think you can with merlin. If you use dune you can do dune utop at a terminal and you will get a REPL in which you can access all the modules in your project.

For what it’s worth, after some iterations I now use a setup where I have one editor window open (vim in my case) and a terminal window which runs tmux, and in the tmux session, utop is running. I have set up my editor to send highlighted text regions to tmux with a shortcut command – this is possible in vim via a plugin, and I’m sure other editors can do it too. This setup replicates the evaluate-region feature many IDEs have.