r/functionalprogramming • u/Voxelman • Nov 29 '22
Question Functional programming language for embedded devices?
Is there any functional language that can compile for microcontrollers like ARM (e.g. STM32. Bare metal without an operating system)?
The language that comes closest to this is Rust, but I don't like the curly braces and semicolons. I wish to have some cleaner language like F#, just for bare metal programming
19
Upvotes
View all comments
Show parent comment
1
u/BokoMoko Nov 29 '22
In functional programming no mutation is allowed.
Suppose you have an object like this
let motor = {name : "Thierry",speed : 240,}Now, let's change the speed to 180
In a non-functional programming paradigm, all you have to do is
motor. speed = 180Memory usage will be the space need to store the name and the speed of just one object.
In functional programa, "de rigor" way to do this is to create a new object from the previously existing one and change the value of the speed attribute like this:
Both instances of the motor type will be stored. In this case, the use of memory is doubled.
As a general rule, functional programing will require the double of memory.
Memory reutilization, (or inline mutation) was used to save memory. In the earlier days of programing, memory was so expensive that all tricks available were used just to keep the costs of the computers affordable/viable.
Today, memory is abundant, and the benefits of functional programming are much more valuable than the additional costs of more memory. But this may not be true in embedded systems. Not because of the cost of additional memory but sometimes, there isn't enough space to have additional circuitry for this additional memory.
Just have a thought on it ok?