开发者

Trying to get F# Closest pair problem working from F# example on www.rosettacode.org/wiki

开发者 https://www.devze.com 2023-01-23 04:50 出处:网络
// F# Brute force: let closest_pairs(xys: Point []) = let n = xys.Length seq { for i in 0..n-2 do for j in i+1..n-1 do
// F# Brute force: 
let closest_pairs  (xys: Point []) =
  let n = xys.Length  
  seq { for i in 0..n-2 do
          for j in i+1..n-1 do            
          yield xys.[i], xys.[j] }
   |>  Seq.minBy (fun 开发者_开发知识库(p0, p1) -> (p1 - p0).LengthSquared)

closest_pairs  [|Point(0.0, 0.0); Point(1.0, 0.0); Point (2.0, 2.0)|]

Type constraint mismatch. The type Point

is not compatible with type Size

The type 'Point' is not compatible with the type 'Size'

Also Type issues with int v float

F# 2.0, .NET 4, VS 2010


You just need to have defined an appropriate Point type first. Perhaps something like this:

type Point(x:float, y) =
  member p.x = x
  member p.y = y
  member p.LengthSquared = x * x + y * y
  static member (-)(p1:Point, p2:Point) = Point(p1.x-p2.x, p1.y-p2.y)
0

精彩评论

暂无评论...
验证码 换一张
取 消