THK*_*THK 9 algorithm mono f# haskell functional-programming
我正在尝试使用F#在Richard Bird的功能算法设计珍珠中重写一些Haskell算法,并且遇到了我不理解的NullReferenceException.
Haskell算法:
unmerges :: [a] -> [([a], [a])]
unmerges [x,y] = [([x], [y]), ([y], [x])]
unmerges (x:xs) = [([x], xs), (xs, [x])] ++
concatMap (add x) (unmerges xs)
where add x (ys, zs) = [(x:ys, zs), (ys, x:zs)]
Run Code Online (Sandbox Code Playgroud)
...按预期工作:
*Main> unmerges [1,2]
[([1],[2]),([2],[1])]
*Main> unmerges [1,2,3]
[([1],[2,3]),([2,3],[1]),([1,2],[3]),([2],[1,3]),([1,3],[2]),([3],[1,2])]
Run Code Online (Sandbox Code Playgroud)
我的F#版本:
let concatMap f m = List.map (fun x -> f x) m |> List.concat
let rec unmerges (ints: 'a list) : ('a list * 'a list) list =
match ints with
| [] -> []
| [x; y] -> [([x], [y]); ([y], [x])]
| x :: xs -> [([x], xs); (xs, [x])] @
(let add x (ys, zs) = [(x::ys, zs); (ys, x::zs)] in
concatMap (add x) (unmerges xs))
Run Code Online (Sandbox Code Playgroud)
...适用于匹配两个元素列表,但在匹配较长列表模式时会引发错误:
> unmerges [1;2];;
val it : (int list * int list) list = [([1], [2]); ([2], [1])]
> unmerges [1;2;3];;
System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.FSharp.Core.Operators.op_Append[Tuple`2] (Microsoft.FSharp.Collections.FSharpList`1 list1, Microsoft.FSharp.Collections.FSharpList`1 list2) [0x00000] in <filename unknown>:0
at Microsoft.FSharp.Primitives.Basics.List.concat[Tuple`2] (IEnumerable`1 l) [0x00000] in <filename unknown>:0
at Microsoft.FSharp.Collections.ListModule.Concat[Tuple`2] (IEnumerable`1 lists) [0x00000] in <filename unknown>:0
at FSI_0055.concatMap[Tuple`2,Tuple`2] (Microsoft.FSharp.Core.FSharpFunc`2 f, Microsoft.FSharp.Collections.FSharpList`1 m) [0x00000] in <filename unknown>:0
at FSI_0055.unmerges[Int32] (Microsoft.FSharp.Collections.FSharpList`1 ints) [0x00000] in <filename unknown>:0
at <StartupCode$FSI_0057>.$FSI_0057.main@ () [0x00000] in <filename unknown>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,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] in <filename unknown>:0
Stopped due to error
Run Code Online (Sandbox Code Playgroud)
我尝试add单独定义,但没有强调任何问题.我很感激有关异常的任何见解和/或有关如何调试的建议.
根据记录,这确实是一个 Xamarin bug(详细信息尚不清楚,但已在当前 alpha 版本 5.10 和 Mono 4.2.1 中解决)。\xe2\x80\x93 THK九月 22 日 2:23
\n