F# 模式鉴别器未定义
我是F#的新手,你能帮我解决这个错误吗:“模式鉴别器未定义”F# 模式鉴别器未定义,f#,F#,我是F#的新手,你能帮我解决这个错误吗:“模式鉴别器未定义” let findColorIndex colorArray, color = let mutable index=0 for i in 0..colorArray.Length-1 do if Color.FromName(colorArray.[i]).R.Equals(color.R) then if Color.FromName(colo
let findColorIndex colorArray, color =
let mutable index=0
for i in 0..colorArray.Length-1 do
if Color.FromName(colorArray.[i]).R.Equals(color.R) then
if Color.FromName(colorArray.[i]).G.Equals(color.G) then
if Color.FromName(colorArray.[i]).B.Equals(color.B)
then index<-i
index
让findColorIndex颜色数组,颜色=
设可变索引=0
对于0..colorArray.Length-1 do中的i
如果Color.FromName(colorArray.[i]).R等于(Color.R),则
如果Color.FromName(colorArray[i]).G等于(Color.G),则
如果Color.FromName(colorArray.[i]).B.Equals(Color.B)
然后索引错误消息很难读取。它是编译器不喜欢的初始逗号。你可能是说
let findColorIndex colorArray color =
或
在风格上,您的代码看起来像是C代码的直接翻译。它能用,但看起来不太好看。我将其改写如下:
// A little helper function to get all color values at once
let getRGB (color : Color) = (color.R, color.G, color.B)
let findColorIndex colorArray color =
let rec findIdx i =
// If no matching color is found, return -1
// Could use options instead...
if i >= colorArray.Length then -1
elif getRGB (Color.FromName colorArray.[i]) = getRGB color then i
else findIdx (i + 1)
findIdx 0
或者,您可以使用数组。tryFindIndex
:
let findColorIndex colorArray color =
// Returns None if no matching index is found,
// otherwise Some index
colorArray |> Array.tryFindIndex (fun c ->
getRGB (Color.FromName c) = getRGB color)
您通常应该使用第一个选项。请参阅:以获得对差异的良好解释。
let findColorIndex colorArray color =
// Returns None if no matching index is found,
// otherwise Some index
colorArray |> Array.tryFindIndex (fun c ->
getRGB (Color.FromName c) = getRGB color)