"The input sequence has an insufficient number of elements" 在 Seq.length 通话中?
"The input sequence has an insufficient number of elements" on a Seq.length call?
我正在研究 Matasano 加密挑战,但我 运行 遇到了一个奇怪的错误。我有一个整数序列,在调用 Seq.length(或 Seq.sum)时,我得到一个 'System.InvalidOperationException' 并解释为 "The input sequence has an insufficient number of elements." 通常此错误与调用相关Seq.take 或类似的空 Seq,但此 Seq 不为空,通常会导致此类异常的调用也不为空。
let HammingDistance (b1:seq<byte>) (b2:seq<byte>) : int =
let bytePairs = Seq.zip b1 b2
let HammingWeight (b:byte) =
let bitPositions = {0 .. 7}
let isSet (byte1:byte) (offset:int) = if (byte1 &&& ((byte 1) <<< offset)) > (byte 0) then 1 else 0
Seq.map (isSet b) bitPositions |> Seq.sum
let HammingDistanceBytes (byte1, byte2) =
HammingWeight (byte1 ^^^ byte2)
let distances = Seq.map HammingDistanceBytes bytePairs
printfn "Distances: %A" distances
printfn "Distances length: %d" (Seq.length distances)
Seq.sum distances
异常前的最后几行输出如下:
Distances length: 1435
Distances: seq [3; 3; 1; 5; ...]
Distances length: 1436
Distances: seq [3; 6; 2; 1; ...]
Distances length: 1437
Distances: seq [0; 3; 2; 3; ...]
Distances length: 1438
Distances: seq [3; 3; 4; 2; ...]
任何人都可以解释为什么会发生此异常,或者提供我应该采取的任何步骤来进一步诊断问题吗?
这里的问题与懒惰求值有关 - b2 不可能首先生成,因为它是从一个更大的列表派生的:
// message:seq<byte>
let firstKeysizeBytes = Seq.take keysize message
let secondKeysizeBytes = Seq.skip keysize message |> Seq.take keysize
在最终序列被强制求值之前,异常不会弹出。错误消息的位置非常混乱,但这就是惰性评估的生活。我可能应该转向使用严格的顺序类型。
我正在研究 Matasano 加密挑战,但我 运行 遇到了一个奇怪的错误。我有一个整数序列,在调用 Seq.length(或 Seq.sum)时,我得到一个 'System.InvalidOperationException' 并解释为 "The input sequence has an insufficient number of elements." 通常此错误与调用相关Seq.take 或类似的空 Seq,但此 Seq 不为空,通常会导致此类异常的调用也不为空。
let HammingDistance (b1:seq<byte>) (b2:seq<byte>) : int =
let bytePairs = Seq.zip b1 b2
let HammingWeight (b:byte) =
let bitPositions = {0 .. 7}
let isSet (byte1:byte) (offset:int) = if (byte1 &&& ((byte 1) <<< offset)) > (byte 0) then 1 else 0
Seq.map (isSet b) bitPositions |> Seq.sum
let HammingDistanceBytes (byte1, byte2) =
HammingWeight (byte1 ^^^ byte2)
let distances = Seq.map HammingDistanceBytes bytePairs
printfn "Distances: %A" distances
printfn "Distances length: %d" (Seq.length distances)
Seq.sum distances
异常前的最后几行输出如下:
Distances length: 1435
Distances: seq [3; 3; 1; 5; ...]
Distances length: 1436
Distances: seq [3; 6; 2; 1; ...]
Distances length: 1437
Distances: seq [0; 3; 2; 3; ...]
Distances length: 1438
Distances: seq [3; 3; 4; 2; ...]
任何人都可以解释为什么会发生此异常,或者提供我应该采取的任何步骤来进一步诊断问题吗?
这里的问题与懒惰求值有关 - b2 不可能首先生成,因为它是从一个更大的列表派生的:
// message:seq<byte>
let firstKeysizeBytes = Seq.take keysize message
let secondKeysizeBytes = Seq.skip keysize message |> Seq.take keysize
在最终序列被强制求值之前,异常不会弹出。错误消息的位置非常混乱,但这就是惰性评估的生活。我可能应该转向使用严格的顺序类型。