1

例如,如果我定义F x y为柯里化函数并且在环境(F x)中使用了很多部分函数,let binding​​那么就性能而言,我是否应该始终将其用于命名函数?(例如,与缓存的 a 相比,property通常object不需要缓存 like )propertyArray.Length

 let Gofrom (board: int[][]) (color: int) (reelID, reelPosition) (direction: Direction) =
    let rec walkfrom x =
        match direction.Target x with
        | (a, _ | _, a) when a = -1 || a = board.Length -> x
        | a, b when board.[a].[b]= color -> walkfrom (a, b)
        | _ -> x
    walkfrom (reelID, reelPosition)

对比

 let rec Gofrom (board: int[][]) (color: int) (reelID, reelPosition) (direction: Direction) =
        match direction.Target (reelID, reelPosition) with
        | (a, _ | _, a) when a = -1 || a = board.Length -> (reelID, reelPosition)
        | a, b when board.[a].[b]= color -> Gofrom board color (a, b) direction
        | _ -> (reelID, reelPosition)
4

1 回答 1

3

性能上应该没有区别,但我肯定会使用第一种形式,因为它明确了哪些参数在调用之间不会改变。

顺便说一句,这并不是真正的柯里化,因为您将代码重构为一个单独的函数(闭包)以避免传递 args。

于 2012-09-13T17:10:35.440 回答