let Keyword (let mutable)
This page was last reviewed on Mar 1, 2023.
Dot Net Perls
Let. We declare values and functions in F# with let. Y equals 100. We introduce a binding to the value 100 to the identifier Y. We cannot change the meaning of Y later.
With the mutable keyword, we create mutable values (variables). These can be changed as a program executes. With let (part of the lightweight syntax) we also declare functions.
Mutable example. This first example uses the "mutable" keyword on a variable. We bind the identifier "number" to the value 100 and then reassign "number" to 200.
Important Mutable indicates assignment. In a declaration, we use the equals sign, but we cannot declare the same thing twice.
// Create a mutable variable. let mutable number = 100 printfn "%d" number // Reassign the variable to another value. number <- 200 printfn "%d" number
100 200
Not mutable error. This problem is encountered by many new F# developers. We try to assign a value that is not mutable. We can fix this by adding a mutable keyword to "color."
However Using two values is a better option (at least on simple things) than a variable that has a change in meaning.
let color = "green" printfn "%s" color // This causes an error. color <- "blue" printfn "%s" color
error FS0027: This value is not mutable
Separate values. Consider this program. We do not reassign any variable. Instead we just use two separate let-values. The meaning of this program is easy to understand.
And Time does not need to be considered to know the values color1 and color2: they never change. The program is easy to reason about.
// Use two identifiers for the values. let color1 = "magenta" printfn "%s" color1 let color2 = "crimson" printfn "%s" color2
magenta crimson
Lightweight function. The F# language uses a lightweight syntax. This considers indentation. It requires the "let" keyword for function declarations.
Here The function "greeting" is declared. It receives one string parameter, and returns a string (a type inferred by the compiler).
Detail To specify a lambda expression, we must use the "fun" keyword instead of the let-keyword.
// Use let keyword to create a function that receives a string. // ... It returns another string. let greeting (name : string) = "Hi " + name.ToUpper() + "," // Call the function and print its result. printfn "%s" (greeting "visitor") printfn "How are you?"
Hi VISITOR, How are you?
Temporary values. Sometimes a function is long and complex. Having temporary (intermediate) values makes it easier to reason about. We can use let-values inside a "let" function.
Here This function concatenates some strings. We use value1 and value2 inside of it to make it easier to read.
let combineStrings left right = // Create a temporary string based on the first argument. let value1 = "[" + left + "]" // Based on the second argument. let value2 = "{" + right + "}" // Return another string. // ... This is not optimally fast. value1 + "..." + value2 // Call the function with two string arguments. let result = combineStrings "cat" "dog" printfn "%s" result
Block comments. Sometimes a let statement spans multiple lines. This is not easy to comment out with single-line comments. But with a block comment we can temporarily erase it.
(* let modify x = List.map (fun y -> y * 2) x |> List.sum *)
Lightweight syntax. Indentation is important in F#. We must line up statements in the correct position when using lightweight syntax.
A review. Special features like inferred types make F# fast to write and easy to understand. With "let" we solve many problems: we specify values, variables and functions.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Mar 1, 2023 (edit).
© 2007-2024 Sam Allen.