dev-resources.site
for different kinds of informations.
Scope progression
Published at
10/18/2024
Categories
functional
fsharp
go
Author
lamg
Main Article
Author
4 person written this
lamg
open
In imperative programming, we usually have code that looks the following way:
func addOneToSlice(xs []int) []int {
rs := make([]int, len(xs))
for i, value := range xs {
rs[i] = value + 1
}
return rs
}
However, notice the following about the for
loop:
- Each iteration has a specific purpose, which is to add one to the current element.
- However, each iteration has no constraint on which element it can operate.
- Operating with
xs[i+2]
andrs[i+3]
wouldn't fundamentally alter the structure of the code we have, while making the end result incorrect.
Compare how the same task would be done in F#:
let rec addOneToList =
function
| [] -> []
| x :: xs -> x + 1 :: addOneToList xs
Now consider the following:
- We have a list as a function argument.
- A list in functional languages is a linked list.
- The efficient and standard operations on linked lists are:
- Separating the head
x
from its tailxs
- Doing something to the head
x
- Comparing the list passed as a parameter with the empty list
[]
- Separating the head
Given these restrictions, adding 1
to any element y
not at the head of the list would significantly alter the structure of our function.
Now compare how the computation progresses in both styles:
- In the functional style, we create a new scope with new values, which involves making a recursive call in the example above.
- In the imperative style, we mutate an existing value without changing the scope.
In functional style, marrying both scope with computational progress has the following consequences:
- We avoid mutation.
- The execution flow is explicit.
- The structure we are dealing with becomes clear.
fsharp Article's
30 articles in total
Learning some Fantomas AST
read article
Ingesting Data in F# with Aether: A Practical Guide to Using Lenses, Prisms, and Morphisms
read article
Suicide Boys Merch quality designed shop
read article
F# 9: Nullable Reference Types and Advancing Null Safety
read article
Unlocking High-Performance AI Computing with F#: A Comprehensive Guide
read article
Scope progression
currently reading
How do I register a complaint with Delhivery?
read article
New wallpapers every day
read article
F# 🤝 GTK4
read article
Introducing F# with Semantic Kernel: Simplifying AI App Development with the Pipeline Pattern
read article
Describing musical domain with F#
read article
Who's Your .NET Ally? - F# vs C#
read article
F# For Dummys - Day 13 Collections Array
read article
F# For Dummys - Day 12 Collections List
read article
Primitive Type Differentiation in F#
read article
Request -> Handler -> SubPub Pattern with MediatR or Raw F# code
read article
F# For Dummys - Day 11 Collections Tuple
read article
F# For Dummys - Day 9 Branching
read article
F# For Dummys - Day 8 Function && Pipeline
read article
F# For Dummys - Day 7 Operators
read article
F# For Dummys - Day 16 Collections Sequence
read article
F# For Dummys - Day 15 Collections Set
read article
F# For Dummys - Day 14 Collections Map
read article
F# For Dummys - Day 10 Loop
read article
F# For Dummys - Day 6 Format
read article
F# For Dummys - Day 5 Mutable
read article
F# For Dummys - Day 4 Value
read article
F# For Dummys - Day 3 New Program
read article
F# For Dummys - Day 2 Environment
read article
F# For Dummys - Day 0 Foreword
read article
Featured ones: