将OCaml转换为F#:是否有一种简单的方法来模拟OC#的顶级#trace在F# [英] Converting OCaml to F#: Is there a simple way to simulate OCaml top-level #trace in F#

查看:200
本文介绍了将OCaml转换为F#:是否有一种简单的方法来模拟OC#的顶级#trace在F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将基于OCaml的多个模块转换为F#。我在F#中转换并运行代码,但F#中最后一个函数的结果与OCaml中最后一个函数的结果不同。所以显然我必须跟随函数调用来弄清楚哪个函数返回错误的结果。



OCaml有一个很好的顶级指令,用于跟踪函数的输入和输出,即#trace



我已经搜索到F#的 debug trace 方法,最接近的是使用Trace.Write方法对代码进行测试,但每个方法需要几行。



例如



原始

  let fun001 parm001 = 
parm001 * 10

乐器化

  let fun001 parm001 = 
//对于VS 2010,此跟踪输出将发送到输出窗口。
System.Diagnostics.Trace.WriteLine(function001< - );
System.Diagnostics.Trace.WriteLine(sprintf%Aparm001);
let result = parm001 * 10
System.Diagnostics.Trace.WriteLine(function001 - >);
System.Diagnostics.Trace.WriteLine(sprintf%A结果);
result

F#与搜索时错过的OCaml #trace有相同的功能吗?



如果你确定答案是否定的,那就是我需要的。我知道人们对简短的答案感到皱眉,但是如果答案是否定的话,那就是我需要的。



编辑



对于更复杂的方法,捕获结果将会对代码进行广泛修改



原始

  let func001 parm001 parm002 = 
match parm001 with
| pattern001 - > func002 parm002
| head :: tail - >
func003 head
func001 tail
| [] - >失败失败

乐器化

  let func001org parm001 parm002 = 
match parm001 with
| pattern001 - > func002 parm002
| head :: tail - >
func003 head
func001 tail
| [] - > failwithfailed
和fun001 parm001 parm002 =
//对于VS 2010,此跟踪输出将发送到输出窗口。
System.Diagnostics.Trace.WriteLine(function001< - );
System.Diagnostics.Trace.WriteLine(sprintf%A,%Aparm001 parm002);
let result = func001org parm001 parm002
System.Diagnostics.Trace.WriteLine(function001 - >);
System.Diagnostics.Trace.WriteLine(sprintf%A结果);
结果

编辑



PostSharp不支持F#。请参阅:使用PostSharp与F# - 需要使用实例的文档

解决方案

不(虽然我很想在F#中有这样的设施)。


I am converting several modules based on OCaml to F#. I have the code converted and running in F#, however the result of the final function in F# is not the same as the result of the final function in OCaml. So obviously I have to follow the function calls to figure out which function is returning the wrong result.

OCaml has a nice top-level directive for tracing the input and output of a function, i.e. #trace .

I have searched F#'s debug and trace methods and the closest I get is to instrument the code using Trace.Write methods but it takes several lines for each method.

e.g.

Original

let fun001 parm001 =
  parm001 * 10

Instrumented

let fun001 parm001 =
  // For VS 2010, this trace output will be sent to Output window.
  System.Diagnostics.Trace.WriteLine("function001 <--");      
  System.Diagnostics.Trace.WriteLine(sprintf "%A" parm001);      
  let result = parm001 * 10
  System.Diagnostics.Trace.WriteLine("function001 -->");
  System.Diagnostics.Trace.WriteLine(sprintf "%A" result);
  result

Does F# have the same functionality as OCaml #trace that I missed when searching?

If you are sure the answer is no, that's all I need. I know people frown on short answers, but that is all I need if the answer is no.

EDIT

For more complex methods where capturing the result would evolve extensive modification to the code

Original

let func001 parm001 parm002 =
    match parm001 with
    | pattern001 -> func002 parm002
    | head :: tail -> 
        func003 head
        func001 tail
    | [] -> failwith "failed"

Instrumented

let func001org parm001 parm002 =
    match parm001 with
    | pattern001 -> func002 parm002
    | head :: tail -> 
        func003 head
        func001 tail
    | [] -> failwith "failed"
and fun001 parm001 parm002 =
  // For VS 2010, this trace output will be sent to Output window.
  System.Diagnostics.Trace.WriteLine("function001 <--");      
  System.Diagnostics.Trace.WriteLine(sprintf "%A, %A" parm001 parm002 );      
  let result = func001org parm001 parm002 
  System.Diagnostics.Trace.WriteLine("function001 -->");
  System.Diagnostics.Trace.WriteLine(sprintf "%A" result);
  result

EDIT

PostSharp does not support F#. See: Using PostSharp with F# - Need documentation with working example

解决方案

No (although I would love to have such a facility in F#).

这篇关于将OCaml转换为F#:是否有一种简单的方法来模拟OC#的顶级#trace在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆