I am learning F# syntax for my interview in a few days. I am not getting any syntax error highlights where I should be. Also, some lines of code works, other do not.
I am on:
- VSCode Version 1.63.2 (Universal)
- MacOs 12.2
- Ionide-FSharp is installed
- dotnet version 5.0.404
This works:
let mutable a = 10
a <- 20 //chaning a to 20
a //a is now 20
let items = [1..5] //creates list from 1 to 5
List.append items [6] //adds 6 to list of 6 --does not change 'items'
This does not work:
let prefix prefixStr baseStr =
prefixStr ", " baseStr
prefix "Hello" "World"
returns:
Microsoft (R) F# Interactive version 11.4.2.0 for F# 5.0
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> let prefix prefixStr baseStr = ;;
let prefix prefixStr baseStr = ;;
-------------------------------^^
/Users/MyName/Documents/learnf#/stdin(1,32): error FS0010: Incomplete structured construct at or before this point in binding
> # silentCd @"/Users/MyName/Documents/learnf#";;
- # 1 @"/Users/MyName/Documents/learnf#/001_fsharp_1.fs"
- ;;
CodePudding user response:
You're only sending the first line of your code to fsi.
Use your cursor to highlight all of this:
let prefix prefixStr baseStr =
prefixStr ", " baseStr
prefix "Hello" "World"
Then hit alt enter. It will send all 4 lines at once and give you what you want.
CodePudding user response:
If you're interested in finding bugs quickly and easily, use the official online compiler from fsharp.org.
Lists in F# are immutable and any modifying operations generate new lists instead of modifying existing lists (FSharp.Core).
let items = [1..5] //creates list from 1 to 5 let items2 = List.append items [6] printfn "%A" items printfn "%A" items2 -> [1; 2; 3; 4; 5] -> [1; 2; 3; 4; 5; 6]prefix "Hello" "World"The result of this expression is implicitly ignored. To discard this value explicitly use'expr |> ignore', or bind the result to a name.let prefix prefixStr baseStr = prefixStr ", " baseStr let text = prefix "Hello" "World" printfn "%s" text -> Hello, World
