我正在创建一个带有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
。
怎么可能,因为列表理解确保观察边界?