Calling function in ocaml with ref list


#1

Hi,

I have ref list and I am writing this function -
let atrr = ref [(1,2);(3,4)]

let rec findAttributeList attrList x =
match !attrList with
[] -> false
|(a,b)::tl-> if a = x then false else true; findAttributeList (tl) x;;

Getting error while calling findAttributeList-

Error: This expression has type ('a * 'b) list
but an expression was expected of type ('a * 'b) list ref


#2

Here, tl has type ('a * 'b) list, your function expects a ('a * 'b) list ref because you wrote !attrList, so you should write findAttributeList (ref tl) x.


#3

You should probably dereference the list before feeding it to the function. Also, you have two bugs on your if expression: the function will always iterate and never return true.


#4

How should I do that? I am very new to Ocaml. I will appreciate your help.


#5
let atrr = ref [(1,2);(3,4)]

let rec find l x =
    match l with
    | [] -> false
    |(a,_)::tl -> if a = x then true else find tl x
in
find !attr