Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading