我想在f#中使用记录实现链表。我知道我可以使用列表类型的构建,但这是用于学习的目的。我的类型是:f#链表执行记录
type Cell = { data : int; next : RList}
and RList = Cell option ref
而且我想作一个简单的插入功能,但我听说F#是期待一个布尔值但被赋予单位类型的表达式。我想知道如果这意味着我已经格式化我的if/else语句错误
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
注:比较是任何比较函数取(项目,d)作为输入和输出真或假例如:
let compare (x, y) = x > y
我的目标是简单地插入一个新的单元格与数据=项目,如果比较输出为真。在上面的例子中,它可以用来插入一个排序列表并维护排序。整个函数应该返回类型单位。任何暗示为什么我的解释器正在寻找一个布尔值将不胜感激!
注:我很新的F#
====
有雾,米哈伊尔和费奥多尔改进礼貌
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
这并不回答你的问题,但你不需要RList:'type Cell = {data:int;下一个:单元格选项}'https://dotnetfiddle.net/VzZwnb –
它更像是一个方便的包装,除非你认为它不太方便? – Sunny
当然,'RList'多余。 –