« Fsharp » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(→String) |
(→String) |
||
Ligne 36 : | Ligne 36 : | ||
// concat strings | // concat strings | ||
let sb = | let sb = StringBuilder() | ||
sb.Append("abc") | sb.Append("abc") | ||
sb.Append("def") | sb.Append("def") | ||
</kode> | </kode> | ||
Version du 24 septembre 2022 à 13:04
Command line
dotnet fsi # start F# Interactive dotnet fsi script.fsx # run script.fsx dotnet new console -o fsharp1 -lang f# |
printf
let s = "value" printfn "text %A text" s |
Format | Type |
---|---|
%A | any |
%s | string |
%i | integer |
%b | boolean |
String
let s = "abcde" // slice s[1..3] // "bcd" // triple-quote string allows to use double-quote within the string let s = """<tag attribute="value" />""" // concat strings let sb = StringBuilder() sb.Append("abc") sb.Append("def") |
Collection
List
Ordered, immutable, series of elements of the same type.
let list1 = [1; 2; 3] let list2 = [1..5] // [1; 2; 3; 4; 5] let list3 = [0..2..10] // [0; 2; 4; 6; 8; 10] // elements in lists are not mutable // cons operator 0 :: list1 // create a new list with 0 at the head [0; 1; 2; 3] // extract head and tail let head :: tail = list1 // head: 1, tail: [2; 3] // concatenate operator list1 @ [4; 5] // create a new list [1; 2; 3; 4; 5] |
Array
Fixed-sized, zero-based, mutable collectionof consecutive elements of the same type.
let myArray = [|1; 2; 3|] // elements in arrays are mutable myArray[1] <- 4 // [|1; 4; 3|] |
Map
Set
Immutable Dictionary
Composite data type
Tuple
let tuple1 = (1, "one") let (a, b) = tuple1 // a: 1, b: "one" |
Record
type Item = { Id: int; Name: string; } let item1 = { Id = 1; Name = "one" } |
Function
let add x y = x + y let result = add 1 2 // 3 |
Console input
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
- Disable the Ionide for F# extension
- Run → Run Without Debugging
- Select environment: .NET 5+ and .NET Core (this create the launch.json file)
- Open launch.json → Add Configuration → .NET: Launch .NET Core Console App
- Fill target-framework = net6.0 and project-name = myproject
- Run → Run Without Debugging
- Configure Task → Create tasks.json file from template → Select a Task Template: .NET Core