# Converting a tree

**URL:** <https://discuss.ocaml.org/t/converting-a-tree/1625>\
**Category:** Learning\
**Created:** [February 26, 2018, 5:47pm UTC](https://discuss.ocaml.org/t/converting-a-tree/1625 "2018-02-26T17:47:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hughng92](https://avatars.discourse-cdn.com/v4/letter/h/f0a364/32.png) [@hughng92](https://discuss.ocaml.org/u/hughng92)\
**Post date:** [February 26, 2018, 5:47pm UTC](https://discuss.ocaml.org/t/converting-a-tree/1625/1 "2018-02-26T17:47:54Z")

</div>

Hello,  
I am trying to convert my tree to a list using recursion:

> ```
> let rec bt_to_list mytree = match mytree with
> |Empty -> []
> |Node(lt,x,rt) -> [x]:: (bt_to_list lt) :: (bt_to_list rt)
> 
> ```

I have an error:

> Error: This expression has type 'a list list  
> but an expression was expected of type 'a list  
> The type variable 'a occurs inside 'a list

Why is that? My logic seems right to me.  
Thank you!

---

<div class="post-metadata">

**Author:** ![objmagic](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/objmagic/32/9_2.png) [@objmagic](https://discuss.ocaml.org/u/objmagic)\
**Post date:** [February 26, 2018, 5:56pm UTC](https://discuss.ocaml.org/t/converting-a-tree/1625/2 "2018-02-26T17:56:16Z")

</div>

In your code, `[x]`, `bt_to_list lt`, and `bt_to_list rt` all have type `'a list`. However, you are using `::` to append `'a list` with another `'a list`.

The fix is using list concatenation operator `@`: `(x :: (bt_to_list lt)) @ (bt_to_list rt)`
