f# linked list implementing with records

Viewed 1111

I'm trying to implement a linked list in f# using records. I know I can use the build in list type but this is for learning purposes. My type is:

type Cell = { data : int; next : RList}
and RList = Cell option ref

And I'd like to make a simple insert function, but I'm told that f# is expecting a boolean but was given an expression of type unit. I'm wondering if this means I've formatted my if/else statement incorrectly

let rec insert comp (item: int) (list: RList) =
    let c = {data = item; next = ref None}
    match !list with
     | None -> list = cellToRList c
     | Some {data = d; next = remaining} -> 
        if (comp(item, d)) then
            c.next := !remaining
            remaining := ref c (* compiler indicates error here *)
        else insert comp item remaining

Note: comp is any comparison function taking (item, d) as input and outputting true or false ex:

let compare (x, y) = x > y

My goal is simply to insert a new cell with data = item if compare outputs true. In the example above it could be used to insert into a sorted list and maintain sorted-ness. The entire function should return type unit. Any hint as to why my interpreter is looking for a boolean would be appreciated!

Note: I'm very new to F#

====

Fixed with improvements Courtesy of Foggy, Mikhail, and Fyodor

type Cell = { data : int; next : (Cell option) ref}

let rec insert compare (item: int) (list: (Cell option) ref) : unit =
    let c = {data = item; next = ref None}
    match !list with
    | None -> list := Some c
    | Some {data = d; next = remaining} -> 
        if (compare(d, item)) then
            c.next := !remaining
            remaining := Some c
        else insert compare item remaining
1 Answers
Related