stringocaml

Difference between String.map and String.mapi in OCaml


Difference between String.map and String.mapi in OCaml. The guide says the the former "applies the function" while the latter "calls the function"? What does that mean? I also tried this:

String.map (fun x -> Char.uppercase_ascii x) "lala";;

and it works. But if I try it with

String.mapi (fun x -> Char.uppercase_ascii x) "lala";;

I get

Error: This expression has type int but an expression was expected of type char

Which doesn't make sense to me because I though the error should be the other way around. How should I think about String.mapi?


Solution

  • There is no difference between terms calls and applies in the context of OCaml documentation. The main difference between those two functions is that String.map passes to the user function only one argument, the character, as the input, where String.mapi passes to the user function two arguments -- the position of the character (starting from zero) and the character itself.