Reputation: 5954
I want to use functions from a module inside the file BasicFunctions.fs. After completing f# hello world tutorial one next step is to work through F# Tour.
The question what is the equivalent of import in F# and printfn an integer helped a bit. The question value or constructor not defined seem to be not relevant for my case.
If i change my project configuration myFsharpApp.fsproj
to this
<ItemGroup>
<Compile Include="BasicFunctions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
I get the error: FSC : error FS0225: Source file 'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs' could not be found [C:\dev\fsharp\myFSharpApp\myFSharpApp.fsproj]
But the file BasicFunctions.fs can be found under the path 'C:\dev\fsharp\myFSharpApp\BasicFunctions.fs'. So i do not understand why it is not found.
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
Using dotnet build
then returns
Most of the code is copied from the F# Tour. The difference is that i want to import functions from the file BasicFunctions.fs
and want to print the result of sampleFunction1 x = x*x + 3
The code structure is the following
// 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
?printfn "%d" 12
not working assuming that sampleFunction1
returns an int with the value 12?This is the content of myFSharpApp.fsproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.fs" />
</ItemGroup>
</Project>
Upvotes: 1
Views: 912
Reputation: 586
So it becomes like below.
// BasicFunctions.fs
module BasicFunctions
let sampleFunction1 x = x * x + 3
// Program.fs
open BasicFunctions
[<EntryPoint>]
let main argv =
sampleFunction1 3
|> printfn "%d"
0
Note that you can skip the open and reference the module directly. Both files needs to be included, in order.
<ItemGroup>
<Compile Include="BasicFunctions.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
Upvotes: 2