簡體   English   中英

f# 自定義鏈表和引用

[英]f# custom linked list and references

我是這種語言的新手。 為了嘗試和理解引用,我嘗試以大一大學計算機科學的方式實現一個簡單的定向列表。

type item = { 
    value:float 
    next:ref<item>
}

type list() = 
    let head:ref<item> = null // tried instantiation so many different ways and it likes none of em

    let sum i = 
        if i == null then
            0
        else 
            i.value + sum i.next // constructor not defined?

請告訴我為什么我不擅長這個

首先,您嘗試以某種命令式方式實現它 - 這沒問題,但不是真正的功能。 無論如何,您遇到的第一件事是,您不能分配null - 如果您真的想要,您必須將[<AllowNullLiteral>]添加到您的item類型(但當然您必須將其設為類而不是記錄):

[<AllowNullLiteral>]
type Item(value, next) = 
    member this.value = value
    member this.next : Item ref = next

let head : ref<Item> = ref null

let rec sum (i : Item) = 
    if i = null then
        0.0
    else 
        i.value + sum !i.next

但這幾乎從來都不是一個好主意,所以我會這樣開始:

module List =

   type Item = { value : float; next : List }
   and  List = Item option ref

   let empty : List = ref None
   let single x = { value = x; next = ref None }

   // this is how you can change the list
   let rec append x l =
      let item = single x
      match !l with
      | None -> 
         l := Some item
      | Some node ->
         append x node.next

   let rec sum (l : List) =
      match !l with
      | None      -> 0.0
      | Some item -> item.value + sum item.next

現在,如果您仔細觀察,您會發現如果您只是在前面附加,那么您實際上並不需要 refs,瞧……您得到了通常的功能列表 ;)

PS:你也忘記了一些其他的東西:

  • 你在那里使用浮點數,所以你必須使用0.0而不是0
  • 你的sum函數必須是遞歸的(注意它不是尾遞歸的,所以你會遇到大列表的問題!)
  • 你必須取消引用ref -細胞有!
  • 你必須用ref構造ref -cells (例如ref null
  • 你的type list() =對我來說毫無意義所以我把它轉換成一個模塊

PPS:請不要因為F#-Way來改變這樣的事情 - 這只是向您展示如何做到這一點......但如果您不必,請不要這樣做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM