r/functionalprogramming • u/Ok_Wishbone_5018 • Nov 20 '23
Question Is the code still functional programming?
If i declare a variable inside a function and mutate it, is it still considered functional?
For example, if i write a function to find the max of an array with a for loop and a max value and return that max value the function is still pure. It doesn't have any side effects, since the max variable is not global but it does contraddict the principle of immutability of data. So my question is, can you use for loops and variables inside of functions or not?
(ik of another way of getting the result using functions like reduce in javascript, thats not the point of the question)
33
u/XDracam Nov 20 '23
By "never using variables ever", you only silence the academic purists. You usually don't gain much.
Functional programming lives and dies at the function level. It's about controlling the lifetime of and access to mutable state. In the "onion pattern", you have one outer layer of the software with mutable state, and every function below that is referentially transparent and pure.
However, the implementation of a function can be seen as its own little program. It's perfectly fine to have local mutable variables (and for short functions it often makes the code more readable and a lot faster!) - just make sure that you don't mutate any state that lives longer than the function itself.
17
u/azium Nov 20 '23
There's no functional programming police. The main idea is that you want predictability.. if a function is totally contained you're free to create intermediate values to get the result you want.
Determining FP-ness is less about the small things like this and more about the overall structure of the program.
8
u/[deleted] Nov 20 '23 edited Nov 20 '23
Yes, it's still functional, because mutation doesn't go outside of scope of the function.
If it does (changes global variable or doing other side effect, then yes, it's violation).
4
u/DMenace83 Nov 20 '23
I think the bigger problem would be maintainability of your entire application.
It might not be apparent with a simple findMax function, but if it's a more complicated function, it could be a bit more difficult to troubleshoot if the rest of your application is written in pure functions. The person troubleshooting would be constantly flipping back and forth between reading pure FP code and code with mutating variables.
Also, IMO, a findMax function with a mutating variable and a for loop is a lot more complicated than just a simple array fold function.
3
u/Iceland_jack Nov 20 '23
Haskell has an
ST namemonad which allows mutability as long as name is polymorphicIf the name is universally quantified it means any destructive update is not observable from the outside, meaning it's safe to treat it as a pure computation. The higher-rank name type prevents us from writing this as
sum = runST . sumSTbut maybe it's possible with the new impredicative types.Now you can pass it all sorts of foldable structures and it uses mutation under the hood.
3
u/[deleted] Nov 20 '23
Then in the inside you are imperative, but the whole function is still pure.
2
u/delfV Nov 20 '23
It's very opinion-based question. IMO - is it pure? Yes. Is it functional? Code that uses this functions is functional, code inside the function is not. But should you care?
If you look at implementations of persistent data structures like Immutable.js you'll see a lot of mutations going there, but from user's perspective it is pure and functional. At the end of the day your program need to mutate memory anyway.
2
u/jherrlin Nov 20 '23
If your team has decided to apply functional programming ideas I think your mates would like to see a recursive function instead of an imperative loop here.
2
u/pthierry Nov 20 '23
Immutability is not a principle, it's a tool.
If you pass an immutable data structure to a function, you know it won't get changed without you knowing. That makes it easier to reason about correctness (especially in the presence of untrusted code and concurrency).
If a function is referentially transparent, you know that if you call it several times with the same arguments, you'll get the same results. It means you can call it in parallel, you can cache the results (memoization), etc…
The function you describe is referentially transparent, so from the point of view of its caller, it has the benefits of FP.
But the internals are imperative and it uses one mutable cell. Which means that the author and maintainer of the code don't benefit from FP to reason about it.
Sometimes there's a trade-off between static guarantees and some other quality. Without linear types, in Haskell, to efficiently initialize an array, you create a mutable array, change each cell, then freeze it into a immutable array. The cell mutations can be done purely from the outside perspective, with the
STmonad. But freezing is an operation that's either costly or dangerous because either you make a copy and it takes time or you don't and you run the risk that someone changes the array while someone is using it as immutable. They chose the latter (unsafeFreezeArray), which means the compiler cannot guarantee that it's safe and that forced them to be extra careful with this code. Fortunately, with linear types, it's now possible to implement a safe freeze where the compiler guarantees that the mutable reference is used only once, by the freeze function.1
u/[deleted] Dec 27 '23
strictly speaking, functional programming is programming that adheres to the substitution model. once you add assignment and mutation this no longer holds. so no its not functional programming