简体   繁体   English

F# 交互式 window 问题

[英]F# interactive window problem

i am getting this error in the interactive window on http://www.tryfsharp.org .我在http://www.tryfsharp.org上的交互式 window 中遇到此错误。 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错误 FS0039:未定义字段、构造函数或成员'init'

Aprreciate your help感谢您的帮助

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.只有在第二次运行代码时才会出现错误,并且它在 TryF# 控制台和本地 Visual Studio 中的行为应该相同。

The problem is that you're declaring a value named List :问题是您要声明一个名为List的值:

let List = randomNumberGenerator 10

which hides the standard module List .它隐藏了标准模块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.声明值后, List.init尝试访问此List值的成员,而不是访问标准List模块中的 function。

There is a good reason for naming conventions, such as using lowercase for local variable names:-)命名约定有一个很好的理由,例如对局部变量名使用小写:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM