Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected NullReferenceException in F# Algorithm

I'm trying to rewrite some Haskell algorithms in Richard Bird's Pearls of Functional Algorithm Design using F# and have run into a NullReferenceException that I don't understand.

The Haskell algorithm:

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)]

... which works as expected:

*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])]

My F# version:

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))

... which works fine for matching the two element list but throws an error when matching the longer list pattern:

> 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

I tried defining add separately and that did not highlight any issues. I'd appreciate any insights into the exception and/or suggestions on how to debug.

like image 861
THK Avatar asked Sep 17 '15 02:09

THK


People also ask

Why am I getting a NullReferenceException?

The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized). This means the reference is null , and you cannot access members (such as methods) through a null reference.

What does system NullReferenceException mean?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.

How do I fix NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.


1 Answers

For the record—it was indeed a Xamarin bug (details unclear, but it's resolved in the current alpha release, 5.10 and Mono 4.2.1). – THK Sep 22 at 2:23

like image 176
2 revs Avatar answered Oct 03 '22 12:10

2 revs