Previous use in ML

ML before is described in http://sml-family.org/Basis/general.html as

a to b returns a. It gives a shorthand for evaluating a, then b, before returning the value of a.

When I tried to use this command awaiting x = 4 and (4 + 1)

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

      

I have an error message:

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)
- 

      

What could be wrong?

Edit

From Matt's answer, I had to use

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

      

I also found that it is before

used to close the stream after some FileIO functions have been processed.

(* 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)

      

+3


source to share


1 answer


From here , it says the type before

is equal before : ('a * unit) -> 'a

, and as your type error indicates, it expects the type of the second argument to be of type unit

, however you supplied something of the type int

. Try it val x = (3+1 before ())

and you should get the expected result. The goal is to include the second argument in some way that affects the computation, like manipulating a cell ref

or doing some I / O that you want to do before evaluating your first argument. Seems to be the same thing:

val x = e1 before e2

      

and



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

      

However, before

not what I really use, so if anyone else has anything to add, comments are definitely welcome.

+4


source







All Articles