« Fsharp » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 25 : Ligne 25 :
|}
|}


= Array =
= List and arrays =
<kode lang='fs'>
<kode lang='fs'>
let myList = [1; 2; 3]
let myList = [1..5]      // [1; 2; 3; 4; 5]
let myList = [0..2..10]  // [0; 2; 4; 6; 8; 10]
// elements in lists are not mutable
let myArray = [|1; 2; 3|]
let myArray = [|1; 2; 3|]
// elements in arrays are mutable
// elements in arrays are mutable
myArray[1] <- 4  // [|1; 4; 3|]
myArray[1] <- 4  // [|1; 4; 3|]
</kode>
</kode>
= List =
<kode lang='fs'>
let myList = [1; 2; 3]
</kode>
Elements in lists are not mutable


= Console input =
= Console input =

Version du 24 septembre 2022 à 12:26

Command line

Ps.svg
dotnet fsi  # start F# Interactive
dotnet fsi script.fsx  # run script.fsx

dotnet new console -o fsharp1 -lang f#

printf

Fs.svg
let s = "value"
printfn "text %A text" s
Format Type
%A any
%s string
%i integer
%b boolean

List and arrays

Fs.svg
let myList = [1; 2; 3]
let myList = [1..5]      // [1; 2; 3; 4; 5]
let myList = [0..2..10]  // [0; 2; 4; 6; 8; 10]
// elements in lists are not mutable

let myArray = [|1; 2; 3|]
// elements in arrays are mutable
myArray[1] <- 4  // [|1; 4; 3|]

Console input

Fs.svg
printf "Enter a value: "
let input = Console.ReadLine()
printfn "Entered value: %s" input
.vscode/launch.json
{
    "configurations": [
        {
            "console": "integratedTerminal" // // switch from internalConsole to integratedTerminal to allow console input in VS code
        }
    ]
}

Dev env

  • VScode
    • Ionide for F# extension → intellisense
    • fantomas-fmt → format F#

Create launch and tasks

  1. Disable the Ionide for F# extension
  2. Run → Run Without Debugging
  3. Select environment: .NET 5+ and .NET Core (this create the launch.json file)
  4. Open launch.json → Add Configuration → .NET: Launch .NET Core Console App
  5. Fill target-framework = net6.0 and project-name = myproject
  6. Run → Run Without Debugging
  7. Configure Task → Create tasks.json file from template → Select a Task Template: .NET Core