user9993
user9993

Reputation: 6170

Hello world won't compile with "The value or constructor is not defined"

let prefix prefixString baseString = 
    prefixString + " " + baseString

prefix "Hello" "World"

With the code above I'm getting the error Stuff.fs(34,1): error FS0039: The value or constructor 'prefix' is not defined.

I'm not entirely sure why this is happening, as I'm watching a video series on F# in which literally the same code is being compiled and ran successfully. Is there something wrong with my environment?

Upvotes: 3

Views: 1538

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

In the comment, you mentioned that you are running the snippet using "run selection" command. This command runs the selected piece of code in F# Interactive which initially contains no definitions. So, if you select and run just the last line, you will get:

> prefix "Hello" "World";;
stdin(1,1): error FS0039: The value or constructor 'prefix' is not defined

This is because F# Interactive does not know what the definition of prefix is - it does not look for it automatically in your file. You can fix this by selecting everything and running all code in a single interaction, or you can first run the definition and then the last line, i.e.:

> let prefix prefixString baseString = 
    prefixString + " " + baseString;;
val prefix : prefixString:string -> baseString:string -> string

> prefix "Hello" "World";;
val it : string = "Hello World"

Note that when you run the first command, F# Interactive will print the type of the defined functions, so you can see what has just been defined.

The fact that F# Interactive has its own separate "state of the world" is quite important, as it also means that you need to re-run functions after you change them so that subsequent commands use the new definition.

Upvotes: 7

Related Questions