【发布时间】:2022-01-07 03:13:06
【问题描述】:
谁能澄清我收到此消息的原因(请参阅下面注释的两行),并可能指出用户定义的二元运算符的正确规范。 为什么我必须在类型定义之外定义运算符 (.-->.) 而不是 (.>>=.)?
type ListA<'T> = NIL | Cons of 'T * ListA<'T> with
static member cat (a:ListA<'T>) (b:ListA<'T>) :ListA<'T>=
match a with
| NIL -> b
| Cons (a, tail) -> Cons (a, (ListA<'T>.cat tail b))
static member join (a:ListA<ListA<'T>>) =
match a with
| NIL -> NIL
| Cons (a, b) -> ListA<'T>.cat a (ListA<'T>.join b)
static member fmap (fn: 'T -> 'U) (a:ListA<'T>) =
match a with
| NIL -> NIL
| Cons (a, tail) -> Cons ((fn a), (ListA<'T>.fmap fn tail))
static member inline (.>>=.)(ma:ListA<'T> , k:'T -> ListA<'U>) = ListA<'T>.fmap k ma |> ListA<_>.join
static member inline (.<<<.)(mbc:'B -> ListA<'C> , mab:'T -> ListA<'B>) = (fun x -> (mab x) .>>=. mbc)
static member inline (.>>>.)(mab:'T -> ListA<'B> , mbc:'B -> ListA<'C>) = (fun x -> (mab x) .>>=. mbc)
static member join2 (a:ListA<_>) = a .>>=. id
let (.-->.)(mab:'T -> ListA<'B>) (mbc:'B -> ListA<'C>) = (fun x -> (mab x) .>>=. mbc)
let (.<--.)(mab:'B -> ListA<'C>) (mbc:'T -> ListA<'B>) = (fun x -> (mbc x) .>>=. mab)
let f = fun x -> Cons (x, Cons ((x + 1), NIL))
let g = fun x -> Cons (x * 2, NIL)
// NOT Compiling: let fgA = fun x -> (f .<<<. g)
// NOT Compiling: let fgB = fun x -> (f .>>>. g)
let fg = fun x -> (f x) .>>=. g
let fg1 = (f .-->. g .-->. g)
let fg2 = (f .<--. g .-->. f)
printfn "%A" (fg1 3)
【问题讨论】:
标签: f#