NewAbnormal
NewAbnormal

Reputation: 3

This expression has type 'a list but an expression was expected of type 'b * 'c

I don't understand why I'm getting this error message, and I'd be glad to be enlightened about it

let write c v d =
  let rec new_dict=function 
    | []->(k, v)
    | (key,value)::q-> if key=k
                        then new_dict (q) 
                        else (key,value)::new_dict (q)
  in new_dict (d) ;;

Here i aimed to create a new dictionary in which the key 'c' would get a new value, or adding that key if it wasn't in the dictionary in the first place.

Sorry if my error is kind of obvious, I'm new to OcamL.

Thanks.

Upvotes: 0

Views: 67

Answers (1)

Chris
Chris

Reputation: 36496

Let's consider new_dict only, with some formatting cleanup.

let rec new_dict = 
  function 
  | [] -> (k, v)
  | (key, value)::q -> 
    if key = k then 
      new_dict q
    else 
      (key, value) :: new_dict q

In the event the list is empty, it returns a tuple. Otherwise it returns a list. Note the :: list constructor.

Perhaps you meant:

let rec new_dict = 
  function 
  | [] -> [(k, v)]
  | (key, value)::q -> 
    if key = k then 
      new_dict q
    else 
      (key, value) :: new_dict q

Upvotes: 2

Related Questions