Amp231
Amp231

Reputation: 21

F# interactive window problem

i am getting this error in the interactive window on http://www.tryfsharp.org. It works fine in visual studio and im not sure how to tackle it.Any help would be appreciated

let randomNumberGenerator count =
    let rnd = System.Random()
    List.init count (fun numList -> rnd.Next(0, 100))

let rec sortFunction = function
| [] -> []
| l -> let minNum = List.min l in
       let rest = List.filter (fun i -> i <> minNum) l in
       let sortedList = sortFunction rest in
       minNum :: sortedList

let List = randomNumberGenerator 10
let sortList = sortFunction List
printfn "Randomly Generated numbers in a NON-SORTED LIST\n"
printfn "%A" List
printfn "\nSORTED LIST \n"
printfn "%A \n" sortList

error FS0039: The field, constructor or member 'init' is not defined

Aprreciate your help

Upvotes: 0

Views: 223

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243051

You should be getting the error only when you run the code for the second time and it shoul behave the same in the TryF# console as well as locally in Visual Studio.

The problem is that you're declaring a value named List:

let List = randomNumberGenerator 10

which hides the standard module List. After you declare the value List.init tries to access a member of this List value instead of accessing a function in the standard List module.

There is a good reason for naming conventions, such as using lowercase for local variable names :-)

Upvotes: 5

Related Questions