3

I am trying to convert a function from using the pipe forward operator to use the forward composition.

let four_digit_number_has_all_different_digits digits = 
    digits
    |> Seq.distinct 
    |> Seq.length 
    |> (=) 4

Here is my attempt to convert it -

let four_digit_number_has_all_different_digits = 
    Seq.distinct 
    >> Seq.length 
    >> (=) 4

FSI gives me this error -

error FS0030: Value restriction. The value 'four_digit_number_has_all_different_digits'     has been inferred to have generic type
    val four_digit_number_has_all_different_digits : ('_a -> bool) when '_a :> seq<'_b> and '_b : equality    
Either make the arguments to 'four_digit_number_has_all_different_digits' explicit or, if you do not intend for it to be generic, add a type annotation.

In a simple case where the types remain the same removing the initial value and replacing |> with >> works. Can or should I be trying to use forward composition here? And if I should, how do I correct the error?

Chris Wilding
  • 551
  • 3
  • 10
  • See also this blog post: http://blogs.msdn.com/b/mulambda/archive/2010/05/01/value-restriction-in-f.aspx – ildjarn Apr 21 '14 at 16:26
  • Thanks, that makes sense so it should be `let four_digit_number_has_all_different_digits : string -> bool = Seq.distinct >> Seq.length >> (=) 4` – Chris Wilding Apr 21 '14 at 18:18

0 Answers0