我是F#的新手,我正在尝试编写一个计算powerset的函数。
我从Mono(在Mac上运行它)收到错误。
例如,我将通过calcPowerSet([1; 2; 3],[])来启动该函数。关于如何解决这个问题的任何想法?
System.InvalidProgramException: Invalid IL code in FSI_0010:calcPowerSet (Microsoft.FSharp.Collections.List`1,Microsoft.FSharp.Collections.List`1): IL_005d: stind.r4 at FSI_0010.calcPowerSet[Int32] (Microsoft.FSharp.Collections.List`1 _arg1_0, Microsoft.FSharp.Collections.List`1 _arg1_1) [0x00000] at .$FSI_0011._main () [0x00000] at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] stopped due to error
代码:
let rec calcPowerSet = function | ([], []) -> [[]] | ((head::tail), (cHead::cTail)) -> calcPowerSet (tail, (cHead::cTail)) @ calcPowerSet (tail, (head::cHead::cTail)) | ((head::tail), []) -> calcPowerSet (tail, []) @ calcPowerSet (tail, [head]) | ([], collect) -> [collect];;
答案 0 :(得分:2)
我不知道它为什么不起作用(我得到了相同的结果),但如果你把它重写为:
let calcPowerSet =
let rec innerCalc =
function
| ([], []) -> [[]]
| ((head::tail), (cHead::cTail)) ->
innerCalc (tail, (cHead::cTail)) @ innerCalc (tail, (head::cHead::cTail))
| ((head::tail), []) ->
innerCalc (tail, []) @ innerCalc (tail, [head])
| ([], collect) -> [collect]
innerCalc
它似乎在Mac(英特尔)下使用Mono 2.4和F#1.9.6.2 工作正常
答案 1 :(得分:2)
仅供参考,事实证明F#团队已经意识到这个错误,这是Mono团队所知道的Mono 2.4中的一个错误(dunno,如果已经修复)。