I have been interested in functional languages for some time now. It started with Lisp which i can do crash course to deal with WAL
. Scala was on my list to play more with chisel
.
Now, I know about Hardcaml
, I need to to do some Ocaml to get started. This is quick and small primer about Ocaml
documenting bits while i learn stuff
Install Ocaml Link to heading
Obviously starting with Ocaml
compiler and shell
sudo apt install ocaml
Then we can install opam
which is the package manager for ocaml
sudo apt install opam
opam init
eval $(opam env)
opam install -y utop odoc ounit2 qcheck bisect_ppx menhir ocaml-lsp-server ocamlformat
We can add eval $(opam env)
rc files to be loaded with each shell.
Running Hello world Link to heading
The first hello world with some basic examples to simple stuff such as expression, array index, conditionals.
print_endline "Hello, OCaml!"
let kk = print_endline "Hello, OCaml!dd";;
let inc x = x + 1;;
let y = inc 3;;
print_endline (string_of_int y);;
let x = 11==1 && 1==1;;
print_endline (string_of_bool x);;
let x1 = "ABC";;
print_endline x1;;
let x2 = x1.[0];;
print_endline (String.make 1 x2);;
if 1==1 then
begin
print_endline "1==1";
print_endline "1==1"
end
else
print_endline "1!=1";;
We can run with ocaml compiler to create elf or run it in ocaml
ocamlc main.ml; ./a.out
ocaml main.ml
Dune hello world Link to heading
Finally, Dune is the build system for ocaml and it seems very opinionated about project structure. I guess i will circle back.
The first way to use dune is creating files manually (the 3 files)
::::::::::::::
dune
::::::::::::::
(executable
(name hello))
::::::::::::::
dune-project
::::::::::::::
(lang dune 3.17)
::::::::::::::
hello.ml
::::::::::::::
print_endline "Hello, world!"```
dune build hello.exe
dune exex ./hello.exe
or we can just use dune init
to create a directory
dune init project my_project
cd my_project
echo '(executable (name main))' > dune
echo 'print_endline "Hello, Dune!"' > main.ml
dune exec ./main.exe