F# strange printfn problem -


I was playing with F # (Visual Studio 2010 Beta 1) around, and I wrote a small console script In which the user asked the input number 2 and one operator and then executed it. It works fine except for a small, but annoying thing: Sometimes my printfn instructions are ignored. I really have a breakpoint in the code to see the case.

Code snippet:

  Convert (source: string) = Try System.Int32.Parse (Source) with:? System.FormatException - & gt; Printfn "'% s' is not a number!" Source; WaitForExitKey (); Give exit 1 read = printfn "Please enter a number."; Give System.Console.ReadLine num1: int = read () | & Gt; Printfn is running in the Convert / Print function ... Give num2: int = read () | & Gt; // Convert ... but this has not been taken care of  

This is not the full source of the course, but I think it will be enough if you need the full source then Just tell.

So my question is very simple: what is the reason for this issue with printfn? Am I doing something wrong?

Thank you in advance, ShdNx

A partial explanation of what is happening , But the short and sweet version is that F # will execute any value on the declaration if it does not take the criteria.

  read = printfn "Please enter a number." System.Console.ReadLine   

Since read does not take any parameters, its instant announcement is executed and the return value of the function to the identifier read < / Code>.

By coincidence, your return value is with a type of work (unit -> string) This result is because F # automatically if they do not pass all their parameters ReadLine one unit expects the parameter, but since it is not passed, you can actually read to the readLine function Force it.

The solution is as follows:

  read () = // read a unit parameter printfn "Please enter the number." Since  read  takes a parameter, it is re-evaluated every time it is called. In addition, we are passing a parameter for  readLine  otherwise we will return the  ReadLine  function as a value. 


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -