Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function with multiple arguments in OCaml

Tags:

ocaml

I am trying to write a function to increment a mutable int by a specified amount.

let increase var:int ref amount = (var := !var+amount;var);;

This is what I came up with, but it produces errors. What is the correct way to do it?

like image 543
osolmaz Avatar asked Feb 17 '26 08:02

osolmaz


1 Answers

Your only problem is in the specification of the type of var. You need to use parentheses:

# let increase (var: int ref) amount = var := !var + amount; var;;
val increase : int ref -> int -> int ref = <fun>

For what it's worth, the type specification is optional. OCaml will infer the type.

(I would personally consider having the function return unit, which would make it analogous to the built-in function incr.)

like image 98
Jeffrey Scofield Avatar answered Feb 20 '26 16:02

Jeffrey Scofield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!