Type-mismatch in between List.reduce and defined type
-
29-09-2019 - |
문제
I'm trying to make a function array, which I'm using inside a List.reduce HOF. I used to have a
let minimax = [| (min : int->int->int); max |]
which was working great, but now I want to get the max value of a list, so I thought:
let minimax = [|List.min; List.max|]
This, however, throws the following error:
Minimax.fs(175,5): error FS0030: Value restriction. The value 'minimax' has been inferred to have generic type
val minimax : ('_a list -> '_a) [] when '_a : comparison
Either define 'minimax' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.
I added a type annotation like this:
let minimax = [|(List.min:TreeOfPosition list -> TreeOfPosition); List.max|]
and it compiled. The problem now is with the List.reduce,
| BranchP(position, children) -> List.reduce (minimax.[minOrmax]) (List.map (loop (1 - minOrmax)) children)
Type mismatch. Expecting a TreeOfPosition -> TreeOfPosition -> TreeOfPosition but given a TreeOfPosition list -> TreeOfPosition The type 'TreeOfPosition' does not match the type 'TreeOfPosition list'
Thanks for any tip,
Pedro Dusso
해결책
The problem is that min
operates on two values but List.min
operates on a list of values, so you can't just substitute one function for the other. Since List.min
is basically defined as List.reduce min
, you can probably just get rid of the List.reduce
and apply minimax[minormax]
directly. I can't see how that would be any better than your original solution with min
and max
, though... perhaps you could provide some additional context so it's clearer exactly what problem you're trying to solve.