f#超出范围异常,因为列表理解产生的结果超出了我的预期

时间:2016-03-30 02:58:40

标签: f#

我正在创建一个带有Map的函数,并生成所有可能的值交换。

let mutations setup =
  let setupList = setup |> Map.toList
  [for x in 0..(setupList.Length-1) do
    for y in x..(setupList.Length-1) do
      yield (x, y)]
  |> Seq.map (fun (x,y) ->
    setupList
    |> swapSndAt x y
    |> Map.ofList)
  |> Seq.distinct

以下是swapSndAt的代码:

let swapSndAt i1 i2 xs =
  let f1,s1 = xs |> List.item i1
  let f2,s2 = xs |> List.item i2
  xs |> List.mapi (fun i x ->
    if i = i1 then f1,s2
    else if i = i2 then f2,s1
    else x)

当我运行此功能时,我会在System.ArgumentException: The index was outside the range of elements in the list.中呼叫List.item时收到swapSndAt

怎么可能,因为列表理解确保观察边界?

0 个答案:

没有答案