导入模块失败 - 模块未定义且值或构造函数未定义
Import Module fails - module is not defined and value or constructor not defined
我想使用文件 BasicFunctions.fs 中模块中的函数。
完成后 f# hello world tutorial one next step is to work through F# Tour.
问题what is the equivalent of import in F# and printfn an integer helped a bit. The question value or constructor not defined似乎与我的情况无关。
尝试 1:更改配置以包含第二个文件
如果我将项目配置 myFsharpApp.fsproj
更改为此
<ItemGroup>
<Compile Include="BasicFunctions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
我收到错误:FSC : error FS0225: Source file 'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs' could not be found [C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]
但是在路径'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs'下可以找到文件BasicFunctions.fs。所以我不明白为什么找不到。
尝试 2:将配置更改为:
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
使用 dotnet build
然后 returns
- error FS0039: 命名空间或模块 'BasicFunctions' 不是
定义
- 错误 FS0001:类型不匹配。期待 ''a -> 'b -> 'c'
但给定一个 ''a -> unit ' 类型 ''a -> 'b' 不匹配
类型 'unit'
- 错误 FS0039:未定义值或构造函数 'sampleFunction1'。[C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]
大部分代码是从F# Tour复制过来的。区别在于我想从文件 BasicFunctions.fs
导入函数并打印 sampleFunction1 x = x*x + 3
的结果
代码结构如下
// BasicFunctions.fs
module BasicFunctions =
let sampleFunction1 x = x*x + 3
// Program.fs
module main =
open BasicFunctions
// Define a new function to print a name.
let printGreeting name =
printfn "Hello %s from F#!" name
[<EntryPoint>]
let main argv =
printfn "%d" BasicFunctions.sampleFunction1 3 // 12 expected
0 // return an integer exit code
问题
- 我在导入另一个文件及其功能时做错了什么?
sampleFunction1 x = x*x + 3
有什么问题?
- 假设
sampleFunction1
returns 是一个值为 12 的整数,为什么 printfn "%d" 12
不起作用?
当前代码状态的屏幕
fsproj 的内容
这是myFSharpApp.fsproj
的内容
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
- 顶级模块的定义没有 = 和缩进
- 无
- Space 是函数应用程序,因此为了以正确的优先级调用您需要括号或管道
所以就变成了下面这样
// BasicFunctions.fs
module BasicFunctions
let sampleFunction1 x = x * x + 3
// Program.fs
open BasicFunctions
[<EntryPoint>]
let main argv =
sampleFunction1 3
|> printfn "%d"
0
请注意,您可以跳过打开并直接引用该模块。需要按顺序包含这两个文件。
<ItemGroup>
<Compile Include="BasicFunctions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
我想使用文件 BasicFunctions.fs 中模块中的函数。 完成后 f# hello world tutorial one next step is to work through F# Tour.
问题what is the equivalent of import in F# and printfn an integer helped a bit. The question value or constructor not defined似乎与我的情况无关。
尝试 1:更改配置以包含第二个文件
如果我将项目配置 myFsharpApp.fsproj
更改为此
<ItemGroup>
<Compile Include="BasicFunctions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
我收到错误:FSC : error FS0225: Source file 'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs' could not be found [C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]
但是在路径'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs'下可以找到文件BasicFunctions.fs。所以我不明白为什么找不到。
尝试 2:将配置更改为:
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
使用 dotnet build
然后 returns
- error FS0039: 命名空间或模块 'BasicFunctions' 不是 定义
- 错误 FS0001:类型不匹配。期待 ''a -> 'b -> 'c' 但给定一个 ''a -> unit ' 类型 ''a -> 'b' 不匹配 类型 'unit'
- 错误 FS0039:未定义值或构造函数 'sampleFunction1'。[C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]
大部分代码是从F# Tour复制过来的。区别在于我想从文件 BasicFunctions.fs
导入函数并打印 sampleFunction1 x = x*x + 3
代码结构如下
// BasicFunctions.fs
module BasicFunctions =
let sampleFunction1 x = x*x + 3
// Program.fs
module main =
open BasicFunctions
// Define a new function to print a name.
let printGreeting name =
printfn "Hello %s from F#!" name
[<EntryPoint>]
let main argv =
printfn "%d" BasicFunctions.sampleFunction1 3 // 12 expected
0 // return an integer exit code
问题
- 我在导入另一个文件及其功能时做错了什么?
sampleFunction1 x = x*x + 3
有什么问题?- 假设
sampleFunction1
returns 是一个值为 12 的整数,为什么printfn "%d" 12
不起作用?
当前代码状态的屏幕
fsproj 的内容
这是myFSharpApp.fsproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
- 顶级模块的定义没有 = 和缩进
- 无
- Space 是函数应用程序,因此为了以正确的优先级调用您需要括号或管道
所以就变成了下面这样
// BasicFunctions.fs
module BasicFunctions
let sampleFunction1 x = x * x + 3
// Program.fs
open BasicFunctions
[<EntryPoint>]
let main argv =
sampleFunction1 3
|> printfn "%d"
0
请注意,您可以跳过打开并直接引用该模块。需要按顺序包含这两个文件。
<ItemGroup>
<Compile Include="BasicFunctions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>