Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using bind and Kliesli composition operators with Result

I’m trying to use the bind (>>=) and Kleisli composition (>=>) operators with the basic Result type, but either they are not defined or are not in scope:

let f x =
  if x%2 = 0 then Ok (x/2)
  else            Error ()

let ff x = Ok x >>= f >>= f
let ff' = f >=> f

[<EntryPoint>]
let main _ =
  printfn "%A" (ff 12)
  printfn "%A" (ff' 28)
  0

Error FS0043 Expecting a type supporting the operator ‘>>=’ but given a function type. You may be missing an argument to a function.

I’ve tried to open a few different namespaces to bring the definition into scope, but no luck.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It seems from this like the operators can’t be defined in general without extensions, but are there definitions for the standard Result anywhere?

>Solution :

Haskell-like operators are not defined in the F# core library, nor are they likely to ever be. You’ll need to either write them in your own prelude, or use a library like FSharpPlus for these and other more Haskell-like (Well, typelevel) programming approaches.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading