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