tags:

views:

132

answers:

2

This compiles and works:

let rec HelloEternalWorld _ = 
  Console.ReadLine() |> printf "Input: %s\n"
  HelloEternalWorld 0

HelloEternalWorld 0

This does not compile:

let rec HelloEternalWorld = 
  Console.ReadLine() |> printf "%s\n"
  HelloEternalWorld

HelloEternalWorld

I try to understand why not?

+1  A: 

All you're missing are parentheses, as it would compile if it were:


let rec HelloEternalWorld() = 
  Console.ReadLine() |> printf "%s\n"
  HelloEternalWorld()


To define a function with no arguments you need the parentheses to distinguish the function from a simple value.

emaster70
+3  A: 

Please post the error messages you get, they say everything you need!

The value ... will be evaluated as part of its own definition.

Your code doesn't compile because you're declaring a recursive value (which doesn't exist) instead of a recursive function.

In order to make this a function, you'll have to write something like

let rec HelloEternalWorld() = 
  Console.ReadLine() |> printfn "%s"
  HelloEternalWorld()

which is now a function of type unit -> unit.

Dario