# Calling function in ocaml with ref list

**URL:** https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853
**Category:** Learning
**Created:** [April 12, 2018, 9:01pm UTC](https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853 "2018-04-12T21:01:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![sc0102](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@sc0102](https://discuss.ocaml.org/u/sc0102)
#### Post date: [April 12, 2018, 9:01pm UTC](https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853/1 "2018-04-12T21:01:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ancolie](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ancolie/32/6189_2.png) [@ancolie](https://discuss.ocaml.org/u/ancolie)
#### Post date: [April 12, 2018, 9:10pm UTC](https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853/2 "2018-04-12T21:10:52Z")

</div>

> [@sangeeta\_chowdhary](#):
>
> findAttributeList (tl) x

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`.

---

<div class="post-metadata">

### Author: ![bluddy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bluddy/32/104_2.png) [@bluddy](https://discuss.ocaml.org/u/bluddy)
#### Post date: [April 12, 2018, 9:16pm UTC](https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853/3 "2018-04-12T21:16:01Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![sc0102](https://avatars.discourse-cdn.com/v4/letter/s/ecd19e/32.png) [@sc0102](https://discuss.ocaml.org/u/sc0102)
#### Post date: [April 12, 2018, 9:43pm UTC](https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853/4 "2018-04-12T21:43:18Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bluddy](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bluddy/32/104_2.png) [@bluddy](https://discuss.ocaml.org/u/bluddy)
#### Post date: [April 12, 2018, 10:04pm UTC](https://discuss.ocaml.org/t/calling-function-in-ocaml-with-ref-list/1853/5 "2018-04-12T22:04:07Z")

</div>

```ocaml
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

```
