这是Haskell 中Monad定律的解释.
如何解释F#中的Monad法则?
bind(M,return)相当于M.
bind((返回x),f)等价于f x.
bind(bind(m,f),g)相当于bind(m,(fun x - > bind(fx,g))).
我知道递归函数是F#中一种强大的技术.我的问题是:是否有退出语句,它可以跳出递归函数,就像命令式语言一样.例如,将节点插入二叉树.
type Tree<'a> when 'a :> IComparable<'a> =
| Nil
| Leaf of 'a
| Node of Tree<'a> * 'a * Tree<'a>
let tt2 = Node(
Node(Leaf "D", "B",Node(Leaf "G", "E", Leaf "H" )),
"A",
Node(Nil, "C", Node(Nil, "F", Leaf "I")))
let rec contains (x : #IComparable<'a>) = function
| Nil -> false
| Leaf y -> if x.CompareTo(y) = 0 then true else false
| Node(l, y, r) ->
match l, y, r with
| l, y, Nil -> …Run Code Online (Sandbox Code Playgroud) 它是使用递归的强大技术,因为它具有强大的可描述功能.尾递归提供比正常递归更强大的计算,因为它将递归更改为迭代.Continuation-Passing Style(CPS)可以将大量循环代码转换为尾递归.Continuation Monad提供递归语法,但实质上它是尾递归,即迭代.它应该合理地使用Continuation Monad为100000阶乘.这是代码.
type ContinuationBuilder() =
member b.Bind(x, f) = fun k -> x (fun x -> f x k)
member b.Return x = fun k -> k x
member b.ReturnFrom x = x
(*
type ContinuationBuilder =
class
new : unit -> ContinuationBuilder
member Bind : x:(('d -> 'e) -> 'f) * f:('d -> 'g -> 'e) -> ('g -> 'f)
member Return : x:'b -> (('b -> 'c) -> 'c)
member ReturnFrom : x:'a -> 'a
end …Run Code Online (Sandbox Code Playgroud) 当我调用它们的函数时,我已经知道了F#库和.NET库之间的区别.但是当我在VS 2010对象浏览器中看到语句"printfn"时,我只看到了"public static T PrintFormatLine(... format)"而不是"printfn".
为什么在VS2010对象浏览器中F#库中的语句"printfn"名称不同?
如果没有F#库的文档,如何调用F#库中的函数,因为VS 2010对象浏览器中的名称完全不同?
以下代码有效.
Seq.iter (printfn "%d") [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
Run Code Online (Sandbox Code Playgroud)
这是另一个例子.http://msdn.microsoft.com/en-us/library/ee340408.aspx
数组类型可以直接替换序列类型吗?