0

It's usually said that in functional programming data structures are immutable. I know that immutable data structure is a data structure that cannot be changed.

However, does it also mean that variable references also can't be changed?

For example,

var arr = [1,2,3]

arr = [4,5,6]

In this code I didn't change the array [1,2,3], but created a new one. Still, I changed the reference of the variable 'arr'. Does this code follow the principle of immutability data structure?

2 Answers 2

2

Does it also mean that variable references also can't be changed?

Yes. They're called constants then. You'd better write

const arr1 = [1,2,3] const arr2 = [4,5,6] 

This approach allows you to treat the scope environment as an immutable datastructure as well, and this should be the default. It makes reasoning about your code a lot easier.

Sign up to request clarification or add additional context in comments.

Comments

1

You shouldn't reassign references. Ideally you'd create a new variable so you can see a "before and after" of any changes you've made.

The underlying structure can still be immutable however, even if the reference pointing to it changed. If someone was looking at an old version of the structure, reassigning a reference wouldn't do anything to harm the validity of the data, since the old version still exists, unchanged.

8 Comments

If a data structure is mutable then once one thread modifies it and another thread reads it, work has to be done to prevent the race condition. Immutable data structure saves the effort of handling the race condition. However, what is the benefit of immutable references? Why is it bad for functional programming to change the reference?
@CrazySynthax Because it makes it significantly easier to think about code and follow a "stream of data" when nothings changing. Say you have a large function where a certain variable is used all throughout it. If that variable is conditionally reassigned halfway down the function, now you have to take into consideration what the value was before, what it became, instead of just knowing the the value stayed constant.
@CrazySynthax In functional programming, unlike imperative programming, reassigning isn't necessary, and since there's no benefit other than not creating 1 more variable, it just shouldn't be done.
Yes, that's what I'm saying. And it's necessary in imperative programming because the paradigm relies heavily on looping constructs that require references to be reassigned. In a standard imperative for-loop, since the loop itself doesn't evaluate to a value, you need to reassign an existing reference for the effect of the loop to be seen (or carry out side effects in some other way). In functional programming, you'd use a reduction or map instead, which creates new variables instead of modifying existing ones.
@CrazySynthax I should clarify though, that whether or not an assignment is threads safe depends on the language, among other things. When using immutable datastructures, any given thread will be looking at an immutable copy of a value, not at a reference. When you reassign arr to the second array, this doesn't effect anything since anyone who was looking at the original array will be holding a copy of the original; they're not relying on a reference.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.