I'm trying to use the Num module in OCaml (bignums and big fractions). Some things seem to be working, while others seem not to, and I'm not able to generate a single, complete example. For instance:
# Num.Int(234);;
- : Num.num = Num.Int 234
# Num.mult_num;;
Characters -1--1:
Num.mult_num;;
Error: Reference to undefined global `Num'
May I ask for a simple example of multiplying two bignums?
The reference for Num is here.
If the toplevel is already launched, you can dynamically load the library:
Another possibility (which will work for all third party libraries and will manage paths and dependencies for you) is to use
ocamlfind. For this, issue(or better put it in your
~/.ocamlinitfile). To load a library, just do(If
ocamlfind— hencetopfind— is not available, install it using opam.)Here is an example of multiplication:
The construction
Num.(e)is a shorthand forlet open Num in eand makes possible to use theNumfunctions without prefix ine. Here is a definition of the factorial:You can try it with
If you used
#require, it installs a pretty printer forNumvalues so the previous interaction looks like:which is much easier to read!