# A question about computer programming: What's wrong with mutable variables?

**URL:** <https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371>\
**Category:** In My Humble Opinion\
**Created:** [June 22, 2014, 5:19pm UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371 "2014-06-22T17:19:09Z")\
**Posts on this page:** 17\
**Page:** 3

<div class="post-metadata">

**Author:** ![wolfman](https://avatars.discourse-cdn.com/v4/letter/w/a8b319/32.png) [@wolfman](https://boards.straightdope.com/u/wolfman)\
**Post date:** [June 23, 2014, 2:09am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/41 "2014-06-23T02:09:02Z")

</div>

> [@Frylock](#):
>
> I believe you. My conundrum is right now I don’t see how to not return a complete new state every time.
> 
> That’s not quite true, let me clarify. I can see ways to do it using deltas (just as someone else mentioned) but that doesn’t seem to satisfy the “no mutabilty” rule that some people claim should apply. With deltas, _something_ is changing. (Right?)

I not disagreeing with you at all. My point is that on  
Chessboard() you have 64 mostly independent variables. It’s much more efficient to represent the game move and mutating 2 of those variables, rather than creating a new state that is a copy of 62 and a new version of 2.

((And Yeah for the Chess pedants out there I’m ignoring castling where more than two change)  
(And for the programming pedants out there I’m ignoring the possible compressions where you don’t create a state for empty squares at all, so 64 isn’t necessary)  
The point still stands that mutating drops overhead significantly over a stubborn insistence on immutability)

---

<div class="post-metadata">

**Author:** ![Frylock](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@Frylock](https://boards.straightdope.com/u/Frylock)\
**Post date:** [June 23, 2014, 2:09am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/42 "2014-06-23T02:09:10Z")

</div>

nvm wrong context

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 23, 2014, 2:16am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/43 "2014-06-23T02:16:09Z")

</div>

> [@Frylock](#):
>
> Every time I see an example, I can work through it and understand it, but I’m not to a point where I can immediately grasp most examples at a glance.
> 
> Are you able to show me an example like this “count” thing I’ve been after in the last few posts? It doesn’t have to be about finding prime numbers specifically.
> 
> I totally understand it pays off to understand the higher-order functions. I just have a personal interest in understanding the underlying logic of all this.

Actually… well, Haskell’s definition of filter just uses list comprehension.

filter :: (a -\> Bool) -\> [a] -\> [a]  
filter p xs = [x | x \<- xs, p x]

This literally reads “filter takes a function p, and a list xs. It returns a list that contains all elements, x, such that x is in xs and p(x) is true”.

That’s as far down the rabbit hole the code goes. I assume Scala is similar.

---

<div class="post-metadata">

**Author:** ![Frylock](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@Frylock](https://boards.straightdope.com/u/Frylock)\
**Post date:** [June 23, 2014, 2:21am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/44 "2014-06-23T02:21:15Z")

</div>

And then how is length defined?

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 23, 2014, 2:33am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/45 "2014-06-23T02:33:44Z")

</div>

> [@Frylock](#):
>
> And then how is length defined?

I can’t find explicit, source, but the docs mention it’s O(n), so it’s probably not magic. You could trivially implement it like this:

```auto

length :: [a] -> Int
length (_:xs) = length(xs)+1
length [] = 0

```

This means “length takes a list of stuff, and returns an integer.” "If the list is not empty, split the list into two parts: the first element (which we ignore) and a list containing everything after the first element, we’ll call this “xs”. Recursively call this function with this list called “xs”. Add one to this result.

If the list is empty, return 0."

---

<div class="post-metadata">

**Author:** ![leahcim](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@leahcim](https://boards.straightdope.com/u/leahcim)\
**Post date:** [June 23, 2014, 2:37am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/46 "2014-06-23T02:37:39Z")

</div>

> [@Frylock](#):
>
> Every time I see an example, I can work through it and understand it, but I’m not to a point where I can immediately grasp most examples at a glance.
> 
> Are you able to show me an example like this “count” thing I’ve been after in the last few posts? It doesn’t have to be about finding prime numbers specifically.

How about I walk you though a method to count all of the prime numbers in a list, but without using any of the higher order functions, but using recursion. That might be instructive.

(First, note that the bit of code “1 to n” in Scala is just shorthand for “1.to(n)” where “to” is a method on the Int class that constructs a list of all the ints from 1 to n, so for now, take it for granted that we can construct a list from 1 to n easily).

We can write a function that counts all of the primes in a list as

```auto

def countPrimes(xs:List[Int]) = {
  if (xs.isEmpty) 0
  else if (isPrime(xs.head)) 1 + countPrimes(xs.tail)
  else countPrimes(xs.tail)
}

```

To break this down, the function looks at the list and determines three cases:

[ul]  
[li]If the list is empty, clearly there are no primes in it.[/li][li]If the list is non-empty and first element of the list is prime, the number of primes in the list is one greater than the number or primes in the tail of the list.[/li][li]If the list is non-empty and first element of the list is \*not \*prime, the number of primes in the list is just the number or primes in the tail of the list.[/li][/ul]

which are all manifestly true statements.

Note that this function does not have any vars and is entirely referentially transparent. If we want a function that counts primes up to a certain n, we can just put:

```auto

def countPrimes(n:Int) = countPrimes((1 to n).toList)

```

You could also write the function in another way that makes it a bit clearer where the accumulator is going, by using a second parameter in the function to indicate “number of primes we’ve seen already” to get:

```auto

def countPrimes(xs:List[Int], numWeveSeen:Int) = {
  if (xs.isEmpty) numWeveSeen
  else if (isPrime(xs.head)) countPrimes(xs.tail, numWeveSeen + 1)
  else countPrimes(xs.tail, numWeveSeen)
}

def countPrimes(n:Int) = countPrimes((1 to n).toList, 0)

```

In this the cases change slightly:

[ul]  
[li]If the list is empty, clearly there are no primes in it, so the number of primes is the number we have seen already.[/li][li]If the list is non-empty and first element of the list is prime, the number of primes in the list is the number or primes in the tail of the list, plus the number we have seen already, plus one.[/li][li]If the list is non-empty and first element of the list is \*not \*prime, the number of primes in the list is just the number or primes in the tail of the list, plus the number we have seen already.[/li][/ul]

I include this way of writing it because it makes it obvious that the accumulator has been replaced with a parameter that gets passed through the call stack, but still no vars.

---

<div class="post-metadata">

**Author:** ![Frylock](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@Frylock](https://boards.straightdope.com/u/Frylock)\
**Post date:** [June 23, 2014, 2:39am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/47 "2014-06-23T02:39:18Z")

</div>

@ **Jragon’s** post:

I’m going to read it a few more times to make sure, but I think this explains everything. Thank you for that!

I’m a philosophy teacher. Someone once said about philosophy types that they “worry that what works in practice won’t work in theory.” I think you may have just assuaged my theoretical worries. 😉

---

<div class="post-metadata">

**Author:** ![Frylock](https://avatars.discourse-cdn.com/v4/letter/f/ce7236/32.png) [@Frylock](https://boards.straightdope.com/u/Frylock)\
**Post date:** [June 23, 2014, 2:41am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/48 "2014-06-23T02:41:02Z")

</div>

@leahim’s post:

Ditto what I just said.

Thank you guys for sticking with me on this.

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [June 23, 2014, 2:51am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/49 "2014-06-23T02:51:25Z")

</div>

> [@Frylock](#):
>
> I’m a philosophy teacher. Someone once said about philosophy types that they “worry that what works in practice won’t work in theory.” I think you may have just assuaged my theoretical worries. 😉

Don’t worry. Functional programmers just worry that what works in practice may not be hard enough to work in theory.

---

<div class="post-metadata">

**Author:** ![leahcim](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@leahcim](https://boards.straightdope.com/u/leahcim)\
**Post date:** [June 23, 2014, 2:55am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/50 "2014-06-23T02:55:40Z")

</div>

> [@Jragon](#):
>
> That’s as far down the rabbit hole the code goes. I assume Scala is similar.

Just so Frylock has some Scala, something equivalent would be:

```auto

def filter(xs:List[A], f:A=>Boolean):List[A] = {
  if (xs == Nil) Nil
  else if (f(xs.head)) xs.head :: filter(xs.tail, f)
  else filter(xs.tail, f)
}

```

and length would be

```auto

def length(xs:List[A]):Int = {
  if (xs == Nil) 0
  else 1 + length(xs.tail)
}

```

(Although the real Scala implementations are probably a lot more complicated and higher up in GenTraversableOnceLike or something like that 🙂 .)

---

<div class="post-metadata">

**Author:** ![leahcim](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@leahcim](https://boards.straightdope.com/u/leahcim)\
**Post date:** [June 23, 2014, 2:56am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/51 "2014-06-23T02:56:44Z")

</div>

> [@friedo](#):
>
> Don’t worry. Functional programmers just worry that what works in practice may not be hard enough to work in theory.

In theory, theory and practice are identical, but in practice they are not.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 23, 2014, 2:58am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/52 "2014-06-23T02:58:16Z")

</div>

leachim –

Out of wonder, does Scala have the same conceit as Haskell – that all functions only take a single argument (or no arguments)?

Because in Haskell, while I didn’t want to say it earlier,

filter prime [1…n]

Is technically two steps:

1. evaluate “filter prime” which returns an anonymous function of the type “[a] -\> [a]”
2. Call this anonymous function on the list [1…n]

It looks like Scala just flat out uses multiple arguments instead of having the “all functions are uniform, multiple arguments are just tuples or currying”, is that the case or is the syntax sneaky?

> [@leahcim](#):
>
> (Although the real Scala implementations are probably a lot more complicated and higher up in GenTraversableOnceLike or something like that 🙂 .)

Well, and the Haskell list comprehension implementation is obviously more complex and implemented in the compiler ;).

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [June 23, 2014, 3:03am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/53 "2014-06-23T03:03:29Z")

</div>

> [@Jragon](#):
>
> leachim –
> 
> Out of wonder, does Scala have the same conceit as Haskell – that all functions only take a single argument (or no arguments)?

No, Scala is a _real_ programming language ( 😃 ) and can have functions of more than one variable.

---

<div class="post-metadata">

**Author:** ![leahcim](https://avatars.discourse-cdn.com/v4/letter/l/b4bc9f/32.png) [@leahcim](https://boards.straightdope.com/u/leahcim)\
**Post date:** [June 23, 2014, 3:13am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/54 "2014-06-23T03:13:17Z")

</div>

> [@Jragon](#):
>
> It looks like Scala just flat out uses multiple arguments instead of having the “all functions are uniform, multiple arguments are just tuples or currying”, is that the case or is the syntax sneaky?

Yeah, Scala is not as in love with Currying as the language named after Curry is. 🙂

It does have a “multiple parameter list” syntax for defining functions like:

```auto

def f(a:Type1, b:Type2)(c:Type3, d:Type4):ReturnType = ...

```

which is syntactic sugar for something like:

```auto

def f(a:Type1, b:Type2):(c:Type3, d:Type4) => ReturnType = ...

```

Which kind of helps with a bit of Currying for some pre-specified arrangements.

Most of this is due to the fact that this runs on the JVM and the JVM specifies how calls are made.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 23, 2014, 3:32am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/55 "2014-06-23T03:32:34Z")

</div>

Join us next week when we explain Prolog followed by a deep sense of general malaise and ennui!

---

<div class="post-metadata">

**Author:** ![JohnGalt](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/johngalt/32/184_2.png) [@JohnGalt](https://boards.straightdope.com/u/JohnGalt)\
**Post date:** [June 23, 2014, 4:02am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/56 "2014-06-23T04:02:17Z")

</div>

When you’re done with Prolog, how about Perl or regular expressions?

Frankly this was useful to me. I used to teach a course in F# (don’t laugh), but the “count one more” quote explains a lot.

---

<div class="post-metadata">

**Author:** ![Jragon](https://avatars.discourse-cdn.com/v4/letter/j/e19b73/32.png) [@Jragon](https://boards.straightdope.com/u/Jragon)\
**Post date:** [June 23, 2014, 4:10am UTC](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371/57 "2014-06-23T04:10:56Z")

</div>

I only know regular expressions in the sense that I’m fairly good at proving things about finite state automata. These Ruby/Perl style regular expressions may as well be magic to me.

The only reason Prolog makes me cry is because it pretends it’s first order logic like a jerk and then makes you actually care about the order of your predicates. Literally 90% of all Prolog bugs I’ve ever had have been infinite recursion because I put one predicate before another.

[Previous page](https://boards.straightdope.com/t/a-question-about-computer-programming-whats-wrong-with-mutable-variables/691371.md?page=2)
