f#自定义链接列表和引用 [英] f# custom linked list and references

查看:107
本文介绍了f#自定义链接列表和引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这门语言的新手.为了尝试理解引用,我尝试实现一个简单的有针对性的清单,这是新生大学计算机科学的方法.

i am new to this language. In order to try and understand referencing i have tried implementing a simple directed list the freshman college computer science way.

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?

请告诉我为什么我对此不好

Please tell me why im bad at this

推荐答案

首先,您尝试以某种命令性的方式来实现它-可以,但是并不是真正的功能. 无论如何,您遇到的第一件事是,您无法分配null-如果您确实想要将[<AllowNullLiteral>]添加到您的item类型中(当然,您必须使其成为一个类而不是一个类)记录):

First of all you try to implement it in some imperative way - this is ok, but not really functional. Anyway, the first thing you have problems with is, that you cannot assign null - if you really want to you have to add [<AllowNullLiteral>] to your item type (but of course you have to make it a class instead of a record):

[<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

但这绝不是一个好主意,所以我会这样开始:

But this is almost never a good idea, so I would start like this:

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

现在,如果您仔细观察的话,您会发现如果只是在前面附加内容并瞧瞧,就不需要真正的裁判了……您会得到通常的功能列表;)

now if you watch closely you will see that you don't really need the refs if you just append at the front and voila ... you got your usual functional list ;)

PS:您也忘记了其他一些东西:

PS: you forgot some other things too:

  • 您在其中使用浮点数,因此必须使用0.0而不是0
  • 您的sum函数必须是递归的(请注意,它不是尾递归的,因此您会遇到大列表问题!)
  • 您必须取消引用 ref-具有!的单元格
  • 您必须使用ref(例如ref null)构造ref-单元格
  • 您的type list() =对我来说毫无意义,所以我将其转换为模块
  • you are using floats in there so you have to use 0.0 instead of 0
  • your sum-function has to be recursive (mind you it's not tail-recursive so you will get problems with big lists!)
  • you have to dereference ref-cells with !
  • you have to construct ref-cells with ref (for example ref null)
  • your type list() = made no sense to me so I converted it into a module

PPS:请不要因为变异这样的东西不是 F#-Way -只是为了向您展示如何做到这一点...但是如果您没有,请不要这样做

PPS: please not that it's not the F#-Way to mutate things like this - it's just to show you how you can do it ... but don't if you don't have to

这篇关于f#自定义链接列表和引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆