Abundance
Abundance

Reputation: 2183

OCaml function type with arrows ->

I'm using OCaml StringMap.add function as follows:

let rec makecount l = function
    [] -> mymap
    | s::tl -> 
    if (not (StringMap.mem s mymap)) then
        StringMap.add s 1 makecount(tl)
    else
        StringMap.add s ((StringMap.find s mymap) + 1) makecount(tl)

I get the error, referring to the StringMap.add s 1 makecount(List.tl l):

Error: This function has type StringMap.key -> 'a -> 'a StringMap.t -> 'a StringMap.t   
It is applied to too many arguments; maybe you forgot a `;'.   

Firstly, can someone explain the formatting for the function type. What do those arrows mean? If someone can find the error too, that would be nice as well.

Upvotes: 0

Views: 735

Answers (2)

Pierre G.
Pierre G.

Reputation: 4441

what if you write : StringMap.add s 1 (makecount (List.tl l)) ? What happens is that makecount is considered as a argument in your code, whereas you want the result of makecount (List.tl l).

The arrows are saying that String.Map expects 3 arguments (respectively of types StringMap.key , 'a , 'a StringMap.t) and returns a result of type 'a StringMap.t.

Upvotes: 1

Amadan
Amadan

Reputation: 198566

function
    []    -> ...
  | s::tl -> ...

is an anonymous function that matches its argument against [] and s::tl, and executes the first branch that "fits". [] will only match an empty list; s::tl matches any list with at least one element (s), where tl is the tail - the list you get when you chop off the head.

You assign this anonymous function taking an argument to makecount l; meaning makecount itself now wants two arguments (where you never use l again). The solution is simple: just delete l, and have makecount be a function that takes one argument (per above).

Upvotes: 1

Related Questions