gpt4 book ai didi

f# 折返 : Does accumulator need to be the same as list type?

转载 作者:行者123 更新时间:2023-12-02 21:36:51 24 4
gpt4 key购买 nike

我有以下代码。我花了很多时间试图弄清楚为什么它提示我的 List.foldback 函数的第二个参数。它提示它希望“acc”为(char * bool * (Direction -> int * int) * int)。这对我来说没有任何意义,因为 documentation显示它只是匹配“State”。在这种情况下,我试图使“状态”成为“运动列表”。

有关我正在做的事情的完整描述,请访问 the Code golf voronoi problem

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

经过 Tomas 修复的代码

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

最佳答案

List.foldBack 函数有点奇怪,因为它将输入列表作为最后一个列表之前的参数,并将初始状态作为最后一个列表 论点。因此,交换最后两个参数的顺序应该可以解决问题。或者,您可以定义一个具有交换参数顺序的助手并使用它:

let foldBackNormal f s input = List.foldBack f input s

这是出于历史原因(与 OCaml 的兼容性),但我确信您不是唯一一个觉得这令人困惑的人:-)。

关于f# 折返 : Does accumulator need to be the same as list type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167512/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com