└── readme.md /readme.md: -------------------------------------------------------------------------------- 1 | # ocaml cheatsheet 2 | 3 | Notes from when I went on [Dillon Mulroy's Twitch stream](https://www.twitch.tv/dmmulroy) to talk about [getting started with OCaml](https://www.youtube.com/watch?v=FtI5hxDcVKU). 4 | 5 | ## learning 6 | 7 | The official ["Learn OCaml"](https://ocaml.org/docs) docs are always improving, but here are some other resources I found invaluable when starting out. 8 | 9 | #### Tools & techniques 10 | 11 | 👉 [Real World OCaml](https://dev.realworldocaml.org/) 12 | 13 | - [Command-line parsing](https://dev.realworldocaml.org/command-line-parsing.html) 14 | - [Error handling](https://dev.realworldocaml.org/error-handling.html) 15 | - [Maps and hash tables](https://dev.realworldocaml.org/maps-and-hashtables.html) 16 | - [Serialization](https://dev.realworldocaml.org/data-serialization.html) 17 | - [JSON](https://dev.realworldocaml.org/json.html) 18 | - [Testing](https://dev.realworldocaml.org/testing.html) 19 | 20 | #### Programming language design & theory 21 | 22 | 👉 [OCaml Programming: Correct + Efficient + Beautiful](https://cs3110.github.io/textbook/cover.html#) 23 | 24 | - [Implementing an interpreter in OCaml](https://cs3110.github.io/textbook/chapters/interp/intro.html) 25 | - [The Curry-Howard Correspondence](https://cs3110.github.io/textbook/chapters/adv/curry-howard.html) 26 | 27 | ## project setup 28 | 29 | ```sh 30 | dune init proj 31 | cd 32 | opam switch create . --deps-only 33 | ``` 34 | 35 | Minimal `dune-project` for CLIs: 36 | 37 | ```lisp 38 | (lang dune 3.11) 39 | 40 | (generate_opam_files true) 41 | 42 | (package 43 | (name ) 44 | (depends ocaml dune)) 45 | ``` 46 | 47 | #### git 48 | 49 | Initialize a git repo: 50 | 51 | ```sh 52 | git init 53 | ``` 54 | 55 | Then add a `.gitignore`: 56 | 57 | ```txt 58 | _build/ 59 | _opam/ 60 | ``` 61 | 62 | #### opam switch 63 | 64 | Setup `opam` to auto-enable the switch when entering the directory: 65 | 66 | ```sh 67 | opam init --enable-shell-hook 68 | ``` 69 | 70 | Or do this every time: 71 | 72 | ```sh 73 | eval $(opam env) 74 | ``` 75 | 76 | #### dev dependencies 77 | 78 | ```sh 79 | opam install ocaml-lsp-server odoc ocamlformat utop 80 | ``` 81 | 82 | ## neovim 83 | 84 | Do not do the `rtp` thing that `opam` suggests. 85 | Instead, do this if using `lazy.nvim` as your package manager: 86 | 87 | ```lua 88 | -- ~/.config/nvim/lua/plugins/ocaml.lua 89 | 90 | return { 91 | { 92 | "nvim-treesitter/nvim-treesitter", 93 | opts = { 94 | ensure_installed = { "ocaml", "ocaml_interface" }, 95 | }, 96 | }, 97 | { 98 | "neovim/nvim-lspconfig", 99 | opts = { 100 | servers = { 101 | -- use local lsp installed within project's opam switch 102 | ocamllsp = { mason = false }, 103 | }, 104 | }, 105 | }, 106 | } 107 | ``` 108 | 109 | [dmmulroy]: https://www.twitch.tv/dmmulroy 110 | [getting-started-with-ocaml]: https://www.youtube.com/watch?v=FtI5hxDcVKU 111 | [learn-ocaml]: https://ocaml.org/docs 112 | --------------------------------------------------------------------------------