I'm building a merge sort function and my split method is giving me a value restriction error. I'm using 2 accumulating parameters, the 2 lists resulting from the split, that I package into a tuple in the end for the return. However I'm getting a value restriction error and I can't figure out what the problem is. Does anyone have any ideas?
let split lst =
let a = []
let b = []
let ctr = 0
let rec helper (lst,l1,l2,ctr) =
match lst with
| [] -> []
| x::xs -> if ctr%2 = 0 then helper(xs, x::l1, l2, ctr+1)
else
helper(xs, l1, x::l2, ctr+1)
helper (lst, a, b, ctr)
(a,b)
Any input is appreciated.
The code, as you have written it, doesn't really make sense. F# uses immutable values by default, therefore your function, as it's currently written, can be simplified to this:
This is probably not what you want. In fact, due to immutable bindings, there is no value in predeclaring
a, b
andctr
.Here is a recursive function that will do the trick:
Instead of using a recursive function, you could also solve this problem using
List.fold
,fold
is a higher order function which generalises the accumulation process that we described explicitly in the recursive function above.This approach is a bit more concise but very likely less familiar to someone new to functional programming, so I've tried to describe this process in more detail.