Search code examples
iof#f#-interactive

Why does this boolean expression return false when each term is true?


> (Console.Read() = (int 'd'));;
d
val it: bool = true

> (Console.Read() = (int 'd')) && (Console.Read() = (int 'o'));;
val it: bool = false

It seems to terminate without calling Read, how does the compiler know to make the expression false?


Solution

  • You have to be careful with Read(), it also picks up the line termination character(s). You can see this if you start a clean F# interactive session and do the following:

    > Console.Read();;
    d
    val it : int = 100
    
    > Console.Read();;
    val it : int = 13
    
    > Console.Read();;
    val it : int = 10
    

    Obviously I did this on Windows, since the line termination was \r\n.

    You should probably use ReadKey() (which you can't use in an FSI session) or ReadLine(). For example,

    > Console.ReadLine() = "do";;
    do
    val it : bool = true
    
    >