Ocaml isn't recognized as internal or external command in Windows terminal

Hi everyone.
I’m beginner in ocaml. I need help. I have an error like Error (warning 27 [unused-var-strict]): unused variable h
Here below my program:

let y : int list = [1;2;3];;

let is_list_empty (l: int list) : int = 
  begin match l with
  | [] -> 1
  | h::t -> 0
  end;;

print_int (is_list_empty y);;

So, I generated my project with that command :
opam exec – dune init proj My_project
I tried to build with:
opam exec – dune build
I noticed that the command ocaml isn’t recognized as external or internal command in my terminal. I use a Windows. It’s been two days and I can’t solve the problem. I also tried to run ocaml image with docker without succeeded. Docker image link docker ocaml/opam

You can replace h by _h or _. This should fix the unused variable error.

You cal also type in your dune file:

(executable
  (name your_executable)
  (flags (:standard -w -27)))

Note that a function named is_… is expected to return a boolean (true or false). But you would have to use string_of_bool when printing such a value. It will be more practical (you just have to write if is_empty l then …)

About your installation, which one have you chosen (Opam 2.2, Dkml…) ? Does opam switch indicates the installation of a compiler ?

1 Like

I use opam 2.2.1 with compiler arch-x86_64.1,ocaml-base-compiler.5.2.0,ocaml-options-vanilla.1,system-mingw.1
As you suggested I replaced h by _h, t by _t and I ran opam exec - - dune build and the build worked so I ran opam exec - - dune exec My_project and it’s work. You’re amazing, that solved my problem to run with opam, thank you.

I noticed that in my VSCode when I ran opam switch list , I got that message : The environment is not in sync with the current switch
So I ran : for /f “tokens=*” %i in (‘opam env’) do @%i
The ocaml problem was not recognized as command in my visual studio windows terminal. I don’t know what’s happening but after following these steps:

  1. Install WSL command
    Installing ubuntu by using wsl2 on windows.

  2. Install opam on ubuntu
    I configured on ubuntu the package manager opam

  3. Install ocaml on windows
    I installed opam, the ocaml package manager.

  4. Ocaml extension visual studio
    I added OCaml language extension for VSCode.

At last, I restarted my computer and now ocaml is recognized in Visual Studio terminal as command. Even if it’s still not working on windows terminal.
Now my problem is solved.
Thank you so much. :smiling_face_with_tear: