在 F# 元组中使用 CustomComparison 和 CustomEquality 实现自定义比较

发布于 2024-09-06 13:56:08 字数 4062 浏览 4 评论 0原文

我来这里是为了询问一个特定的主题 - 我确实在网上找到了一些有关此问题的信息。 我正在实现 Minimax 算法的 F# 版本。我现在遇到的问题是我想比较我的树的叶子(下面的数据结构)。搜索 VS 给我的错误,我得到了这样的结果:

我曾经拥有的树类型:

type TreeOfPosition =
    | LeafP   of Position
    | BranchP of Position * TreeOfPosition list

以及实现 IComparable 的诱惑

type staticValue = int
[<CustomEquality;CustomComparison>]
type TreeOfPosition =
    | LeafP   of Position * staticValue
    | BranchP of Position * TreeOfPosition list

    override x.Equals(yobj) = 
        match yobj with
        | :? TreeOfPosition as y -> (x = y)
        | _ -> false

    override x.GetHashCode() = hash (x)
    interface System.IComparable with
        member x.CompareTo yobj =
            match yobj with
            | :? TreeOfPosition as y -> compare (x) (y)
            | _ -> invalidArg "yobj" "cannot compare value of different types"

最后,我只想获得 LeafP 列表的最大值(和最小值)通过其静态值(在其他函数中计算)。

上面的代码可以编译。但是用这个进行测试:

let p = new Position()
p.Add(1,BLACK)
let a = LeafP(p,1)
let b = LeafP(p,2)

let biger = compare a b
printf "%d" biger

我在 GetHashCode 的覆盖中的“| :? TreeOfPosition as y -> Compare (x) (y)”行中得到了 System.StackOverflowException 。

我在 hubfs.net 中有一个线程(http://cs.hubfs.net/forums /thread/15891.aspx),我正在讨论我的 Minimax。在这里您可以找到我的最新代码(http://www.inf.ufrgs。 br/~pmdusso/works/Functional_Implementation_Minimax_FSharp.htm)

提前致谢,

Pedro Dusso

嗯,我非常清楚这个想法,但我无法让它发挥作用。请记住,我想从叶子列表中获取具有最大静态值的叶子(“List.max”:P),我认为实现 CompareToEquals 将让 List.max 对它们起作用,对吗? 我这样写:

let mycompare x y = 
  match x, y with
  // Compare values stored as part of your type
  | LeafP(_, n1), LeafP(_, n2) -> compare n1 n2
  //| BranchP(_, l1), BranchP(_, l2) -> compare l1 l2 //I do not need Branch lists comparison
  | _ -> 0 // or 1 depending on which is list...

[< CustomEquality;CustomComparison >]
type TreeOfPosition =
    | LeafP   of Position * int
    | BranchP of Position * TreeOfPosition list

    override x.Equals(yobj) = 
       match yobj with
       | :? TreeOfPosition as y -> (x = y)
       | _ -> false

    override x.GetHashCode() = hash (x)
    interface System.IComparable with
       member x.CompareTo yobj = 
          match yobj with 
          | :? TreeOfPosition as y -> mycompare x y
          | _ -> invalidArg "yobj" "cannot compare value of different types" 

我以这种方式排列函数时遇到的问题是:

1)未定义模式鉴别器“LeafP”(LeafP 红色下划线)

2)(77,39):错误 FS0039:值或构造函数“mycompare”未定义,当我尝试按 ALT ENTER 时,此消息出现在我的 F# Interactive 中。位置 {77,39} 对应于 mycompare 调用的开头(在 GetHashCode 中)。

我做错了什么?我能做什么更好?

非常感谢,

Pedro Dusso

编辑 3 - 已解决

是的!我终于成功解决你的答案了!

最终代码在这里:

[<CustomEquality;CustomComparison>]
type TreeOfPosition =
    | LeafP   of Position * int
    | BranchP of Position * TreeOfPosition list

    //Func: compare
    //Retu: -1: first parameter is less than the second
    //       0: first parameter is equal to the second
    //       1: first parameter is greater than the second
    static member mycompare (x, y) = 
        match x, y with
        // Compare values stored as part of your type
        | LeafP(_, n1), LeafP(_, n2) -> compare n1 n2
        | _ -> 0 // or 1 depending on which is list...

    override x.Equals(yobj) = 
        match yobj with
        | :? TreeOfPosition as y -> (x = y)
        | _ -> false

    override x.GetHashCode() = hash (x)
    interface System.IComparable with
       member x.CompareTo yobj = 
          match yobj with 
          | :? TreeOfPosition as y -> TreeOfPosition.mycompare(x, y)
          | _ -> invalidArg "yobj" "cannot compare value of different types" 

感谢您的反馈!

佩德罗·杜索

I'm here to ask a specific topic - I really found few info about this on the web.
I'm implementing a F# version of Minimax algorithm. The problem I'm having now is that I want to compare Leaf of my tree (data structure below). Searching the erros the VS gave to me I arrived to something like this:

The tree type I used to have:

type TreeOfPosition =
    | LeafP   of Position
    | BranchP of Position * TreeOfPosition list

and the temptative for implementing the IComparable

type staticValue = int
[<CustomEquality;CustomComparison>]
type TreeOfPosition =
    | LeafP   of Position * staticValue
    | BranchP of Position * TreeOfPosition list

    override x.Equals(yobj) = 
        match yobj with
        | :? TreeOfPosition as y -> (x = y)
        | _ -> false

    override x.GetHashCode() = hash (x)
    interface System.IComparable with
        member x.CompareTo yobj =
            match yobj with
            | :? TreeOfPosition as y -> compare (x) (y)
            | _ -> invalidArg "yobj" "cannot compare value of different types"

In the end, I just wanna get the max (and the min) of a list of LeafP by its static value (calculate in other function).

The code above compiles. However testing with this:

let p = new Position()
p.Add(1,BLACK)
let a = LeafP(p,1)
let b = LeafP(p,2)

let biger = compare a b
printf "%d" biger

I got a System.StackOverflowException in the "| :? TreeOfPosition as y -> compare (x) (y)" line in the override of the GetHashCode.

I have a thread in the hubfs.net (http://cs.hubfs.net/forums/thread/15891.aspx) with I'm discussing my Minimax. Here you can find my lastest code (http://www.inf.ufrgs.br/~pmdusso/works/Functional_Implementation_Minimax_FSharp.htm)

Thanks in advance,

Pedro Dusso

Well, I understood very clearly the idea but I can’t make it work. Remembering that I want to get the leaf with the max static value from a list of leafs (“List.max” :P), I think implementing the CompareTo or Equals will let the List.max works on them, correct?
I compose the things like this:

let mycompare x y = 
  match x, y with
  // Compare values stored as part of your type
  | LeafP(_, n1), LeafP(_, n2) -> compare n1 n2
  //| BranchP(_, l1), BranchP(_, l2) -> compare l1 l2 //I do not need Branch lists comparison
  | _ -> 0 // or 1 depending on which is list...

[< CustomEquality;CustomComparison >]
type TreeOfPosition =
    | LeafP   of Position * int
    | BranchP of Position * TreeOfPosition list

    override x.Equals(yobj) = 
       match yobj with
       | :? TreeOfPosition as y -> (x = y)
       | _ -> false

    override x.GetHashCode() = hash (x)
    interface System.IComparable with
       member x.CompareTo yobj = 
          match yobj with 
          | :? TreeOfPosition as y -> mycompare x y
          | _ -> invalidArg "yobj" "cannot compare value of different types" 

The problems I’m having arranging the functions this way is:

1) The pattern discriminator 'LeafP' is not defined (with LeafP red-underlined)

2) (77,39): error FS0039: The value or constructor 'mycompare' is not defined, when I try a ALT ENTER this message appear in my F# Interactive. The position {77,39} corresponds to the beginning of mycompare call (in GetHashCode).

What I’m doing wrong? What can I do better?

Thanks very much,

Pedro Dusso

EDIT 3 - Solved

Yes! I manage your answer to work finaly!

The final code is here:

[<CustomEquality;CustomComparison>]
type TreeOfPosition =
    | LeafP   of Position * int
    | BranchP of Position * TreeOfPosition list

    //Func: compare
    //Retu: -1: first parameter is less than the second
    //       0: first parameter is equal to the second
    //       1: first parameter is greater than the second
    static member mycompare (x, y) = 
        match x, y with
        // Compare values stored as part of your type
        | LeafP(_, n1), LeafP(_, n2) -> compare n1 n2
        | _ -> 0 // or 1 depending on which is list...

    override x.Equals(yobj) = 
        match yobj with
        | :? TreeOfPosition as y -> (x = y)
        | _ -> false

    override x.GetHashCode() = hash (x)
    interface System.IComparable with
       member x.CompareTo yobj = 
          match yobj with 
          | :? TreeOfPosition as y -> TreeOfPosition.mycompare(x, y)
          | _ -> invalidArg "yobj" "cannot compare value of different types" 

Thanks for the feedback!

Pedro Dusso

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-09-13 13:56:08

首先,您收到异常是因为 compare 函数调用您正在比较的值的 CompareTo 方法(即 x.ComaperTo(y ))。您在 CompareTo 的自定义实现中使用 compare 进行比较的值是要求您比较的值(运行时),因此这会导致堆栈溢出。

实现CompareTo或Equals的常用方法是仅比较您存储在类型中的某些值。例如,您可以编写如下内容:

编辑:您可以编写一个辅助函数mycopare来进行比较(或者您可以简单地更改CompareTo代码> 实现)。但是,如果您想使用函数,则需要将其移动到类型声明中(以便它了解类型 - 请注意,在 F# 中,声明的顺序很重要!)

一种编写方法是

[<CustomEquality; CustomComparison >] 
type TreeOfPosition = 
  | LeafP   of Position * int 
  | BranchP of Position * TreeOfPosition list 

  override x.Equals(yobj) =  
     match yobj with 
     | :? TreeOfPosition as y -> 
        // TODO: Check whether both y and x are leafs/branches
        // and compare their content (not them directly)
     | _ -> false 
  override x.GetHashCode() = // TODO: hash values stored in leaf/branch

  interface System.IComparable with 
     member x.CompareTo yobj =  

       // Declare helper function inside the 'CompareTo' member
       let mycompare x y = 
         match x, y with
         // Compare values stored as part of your type
         | LeafP(_, n1), LeafP(_, n2) -> compare n1 n2
         | BranchP(_, l1), BranchP(_, l2) -> compare l1 l2
         | _ -> -1 // or 1 depending on which is list...

       // Actual implementation of the member
       match yobj with 
       | :? TreeOfPosition as y -> mycompare x y
       | _ -> invalidArg "yobj" "cannot compare value of different types" 

:有效,因为每次调用 compare 只需要部分数据,因此您正在取得一些进展。

First of all, you're getting the exception because the compare function calls the CompareTo method of the values you're comparing (that is x.ComaperTo(y)). The values you're comparing using compare in the custom implementation of CompareTo are the values that the you are asked to compare (by the runtime), so this causes the stack overflow.

The usual way to implement CompareTo or Equals is to compare only some values that you store in your type. For example, you could write something like this:

EDIT: You can write a helper function mycopare to do the comparison (or you could simply change the CompareTo implementation). However, if you want to use a function, you need to move it inside the type declaration (so that it knows about the type - note that in F#, the order of declaration matters!)

One way of writing it is this:

[<CustomEquality; CustomComparison >] 
type TreeOfPosition = 
  | LeafP   of Position * int 
  | BranchP of Position * TreeOfPosition list 

  override x.Equals(yobj) =  
     match yobj with 
     | :? TreeOfPosition as y -> 
        // TODO: Check whether both y and x are leafs/branches
        // and compare their content (not them directly)
     | _ -> false 
  override x.GetHashCode() = // TODO: hash values stored in leaf/branch

  interface System.IComparable with 
     member x.CompareTo yobj =  

       // Declare helper function inside the 'CompareTo' member
       let mycompare x y = 
         match x, y with
         // Compare values stored as part of your type
         | LeafP(_, n1), LeafP(_, n2) -> compare n1 n2
         | BranchP(_, l1), BranchP(_, l2) -> compare l1 l2
         | _ -> -1 // or 1 depending on which is list...

       // Actual implementation of the member
       match yobj with 
       | :? TreeOfPosition as y -> mycompare x y
       | _ -> invalidArg "yobj" "cannot compare value of different types" 

This would work, because every call to compare takes only some part of the data, so you're making some progress.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文