我有以下代码.我花了很多时间试图找出它为什么抱怨我的List.foldback函数的第二个参数.它抱怨它想要"acc"(char*bool*(Direction - > int*int)*int).这对我没有任何意义,因为文档显示它只是匹配'State.在这种情况下,我正试图制作"国家"运动名单".
有关我正在做的事情的完整描述,请访问Code golf voronoi问题
type Direction = Left=0 | Up=1 | Right=2 | Down=3
type Movement = (char * (int * int) * Direction)
let rec masterClaims (items:Movement list) =
items
// Execute these methods in the order in which they are passed.
|> List.map (fun (ch, coord, dir) ->
(
ch,
// This function does the claiming. This has to be
// done in the order of the input list.
claimCells ch coord,
getCoordinate coord,
int dir
))
// Create next items and fold them into the final results for the layer.
// Use foldback so that they are pre-pended to the final list in the correct
// order.
|> List.foldBack (fun (ch, wasClaimed, getCoordinate, directionInt) (acc:Movement list) ->
if (wasClaimed) then
// Create a list of next nodes to inspect
// [counter-clockwise; forward; clockwise]
[(directionInt+3)%4;directionInt;(directionInt+1)%4]
|> List.map enum<Direction>
|> List.iter (fun direction ->
(
ch,
getCoordinate direction,
direction
) :: acc |> ignore)
acc // should be Movement list
) List.empty<Movement>
// The theory here is that we will execute the fold
// for every item before we pass the entire collection
// to the recursive call.
|> masterClaims
Run Code Online (Sandbox Code Playgroud)
托马斯修复的代码
let inline foldBackNormal f s input = List.foldBack f input s
type Direction = Left=0 | Up=1 | Right=2 | Down=3
type Movement = (char * (int * int) * Direction)
let rec masterClaims (items:Movement list) =
items
// Execute these methods in the order in which they are passed.
|> List.map (fun (ch, coord, dir) ->
(
ch,
// This function does the claiming. This has to be
// done in the order of the input list.
claimCells ch coord,
getCoordinate coord,
int dir
))
// Create next items and fold them into the final results for the layer.
// Use foldback so that they are pre-pended to the final list in the correct
// order.
|> foldBackNormal (fun (ch, wasClaimed, getCoordinate, directionInt) (acc:Movement list) ->
if (wasClaimed) then
// Create a list of next nodes to inspect
// [counter-clockwise; forward; clockwise]
[(directionInt+3)%4;directionInt;(directionInt+1)%4]
|> List.map enum<Direction>
|> List.iter (fun direction ->
(
ch,
getCoordinate direction,
direction
) :: acc |> ignore)
acc // should be Movement list
) List.empty<Movement>
// The theory here is that we will execute the fold
// for every item before we pass the entire collection
// to the recursive call.
|> masterClaims
Run Code Online (Sandbox Code Playgroud)
该List.foldBack函数有点奇怪,因为它将输入列表作为最后一个参数之前的参数,将初始状态作为最后一个参数.因此,切换最后两个参数的顺序应该可以解决问题.或者,您可以使用切换的参数顺序定义帮助程序,并使用:
let foldBackNormal f s input = List.foldBack f input s
Run Code Online (Sandbox Code Playgroud)
这是出于历史原因(与OCaml的兼容性),但我确信你不是唯一一个发现这种令人困惑的人:-).
| 归档时间: |
|
| 查看次数: |
368 次 |
| 最近记录: |