【问题标题】:The usage of before in ML机器学习中 before 的使用
【发布时间】:2015-08-07 21:52:23
【问题描述】:

ML 之前在 http://sml-family.org/Basis/general.html 中被描述为

a 在 b 之前 返回一个。它提供了一种符号简写,用于评估 a, 然后 b, before 返回 a 的值。

当我尝试使用此命令时,期望 x = 4 和 (4+1) 被评估

val x = (3+1 before 4+1)

我收到错误消息:

Standard ML of New Jersey v110.78 [built: Sun Apr 26 01:06:11 2015]
- stdIn:1.11-1.25 Error: operator and operand don't agree [overload conflict]
  operator domain: [+ ty] * unit
  operand:         [+ ty] * [+ ty]
  in expression:
    (3 + 1 before 4 + 1)
- 

可能出了什么问题?

编辑

根据马特的回答,我应该使用

val x = (3+1 before print "<end>")

我还发现before是用来在处理完一些FileIO函数后关闭流的。

(* http://stackoverflow.com/questions/2168029/open-file-in-mlsmlnj *)
val infile = "input.txt" ;

(*  reading from file follow this to list of string per line *)
fun readlist (infile : string) = let 
    val ins = TextIO.openIn infile 
    fun loop ins = 
     case TextIO.inputLine ins of 
            SOME line => line :: loop ins 
          | NONE      => [] 
in 
    loop ins before TextIO.closeIn ins 
end ;

val pureGraph =  readlist(infile);

size (hd pureGraph)

【问题讨论】:

  • "and (4+1) isevaluate" 你预计4+1的评估会有什么效果?您是想要打印结果还是只想计算结果并丢弃?

标签: sml ml


【解决方案1】:

来自here,它说before 的类型是before : ('a * unit) -&gt; 'a,并且正如您的类型错误所指定的,它期望第二个参数的类型是unit 类型,但是,您有提供了int 类型的东西。尝试做val x = (3+1 before ()),你应该得到预期的结果。预期目的是让第二个参数成为某种影响计算的方面,例如操作ref 单元或执行一些 IO,您希望在评估第一个参数之前运行这些操作。好像下面是一样的:

val x = e1 before e2

val x = let val a = e1
            val _ = e2
        in a end

也就是说,before 不是我真正使用的东西,所以如果其他人有什么要补充的,当然欢迎 cmets。

【讨论】:

  • 我曾在我想在递增计数器之前获取旧值的情况下使用它。 !a 在 a := !a+1 之前。 Poly/ML 编译器中有很多这样的。
猜你喜欢
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-04
相关资源
最近更新 更多