开发者

is there an eval in ML?

is there a e开发者_开发知识库val function in ML?


ML is really just a dialect, but no ML implementation that I've used (OCaml, F#) has eval as far as I know. This makes sense as ML uses strict typing (the types are known at compile time). Eval would break that guarantee.

However I did find an implementation of eval in OCaml that apparently uses code from the toplevel:

http://thelackthereof.org/OCaml_Eval


It is possible to have some sort of eval in F#, but the code to be evaluated has to be quoted.

#r"FSharp.PowerPack.dll"
#r"FSharp.PowerPack.Linq.dll"
(* load the PowerPack *)
open Microsoft.FSharp.Quotations
open Microsoft.FSharp.QuotationEvaluation
let x = <@@ 8*11 @@>
x.EvalUntyped() // = 88

More about code quotations: http://msdn.microsoft.com/en-us/library/dd233212.aspx


Yes, at least SML/NJ and Poly/ML can do this: code is compiled at run-time and added to the environment.

For Poly/ML there is a worked example here: ML REPL

Poly/ML can do more things like that, e.g. managing the ML environment under program control. It also supports source-level debugging, with arbitrary evaluation at breakpoints.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜