レコードを使用してf#でリンクリストを実装しようとしています。私はリストタイプでビルドを使用できることを知っているが、これは学習目的のためである。f#linked listレコードで実装する
type Cell = { data : int; next : RList}
and RList = Cell option ref
そして私は、単純な挿入機能をしたいのですが、私はF#はブール値を期待していると言われていますが、型ユニットの発現を与えた:私のタイプがあります。
let compare (x, y) = x > y
:compは真または偽の元を(アイテム、D)を入力として取り、出力する任意の比較関数である:これは私が私の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
注意をフォーマットしてきた意味場合、私は思ったんだけど
私の目標は、単にcompare = trueの場合、data = itemの新しいセルを挿入することです。上記の例では、ソートされたリストに挿入し、ソート済みを維持するために使用できます。関数全体が型単位を返さなければなりません。私の通訳がブール値を探している理由についてのヒントがあれば幸いです!
注:私はあなたのNone
からブール値を返す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'は冗長です。 –