# Genetic Algorithms in Elixir: Ch4. errata in evolve function (page 59)

**URL:** <https://forum.devtalk.com/t/genetic-algorithms-in-elixir-ch4-errata-in-evolve-function-page-59/3427>\
**Category:** PragProg Customers\
**Tags:** book-genetic-algorithms-in-elixir, errata\
**Created:** [25 October 2020 10:05 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-ch4-errata-in-evolve-function-page-59/3427 "2020-10-25T10:05:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickdm](https://forum.devtalk.com/user_avatar/forum.devtalk.com/patrickdm/32/629_2.png) [@patrickdm](https://forum.devtalk.com/u/patrickdm)\
**Post date:** [25 October 2020 10:05 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-ch4-errata-in-evolve-function-page-59/3427/1 "2020-10-25T10:05:23Z")

</div>

At the beginning of page 59, in chapter 4

> alter the evolve function to track a generation parameter, like this:

```auto
def evolve(problem, generation, opts \\ []) do
  # ...
  if terminate?(population, generation) do
    # ...
  else
    generation = generation + 1
    ...
    |> evolve(population, generation opts)
  end
end

```

The `evolve` definition is missing the first param `population`;  
a comma is also missing between the last two params of the evolve call, at the end of the function’s body,

Edit: I guess `problem` should be in the params of the recursive call to `evolve` in place of `population` , and as prefix in the `terminate?` call.

We have also to include an initial value for `generation` in `run/2`’s evolve call, to avoid an\>

> (ArithmeticError) bad argument in arithmetic expression

due to an uninitialized generation in the + 1 operation. (I’m starting from generation 0)

Corrige:

```auto

def run(problem, opts \\ []) do
  population = initialize(&problem.genotype/0, opts)
  first_generation = 0

  population
  |> evolve(problem, first_generation, opts)
end

def evolve(population, problem, generation, opts \\ []) do
  # ...
  if problem.terminate?(population, generation) do
    # ...
  else
    generation = generation + 1
    ...
    |> evolve(problem, generation, opts)
  end
end

```

(Hope I’m not bothering anyone with this small corrections, I’m really enjoying the book)

---

<div class="post-metadata">

**Author:** ![seanmor5](https://forum.devtalk.com/user_avatar/forum.devtalk.com/seanmor5/32/1169_2.png) [@seanmor5](https://forum.devtalk.com/u/seanmor5)\
**Post date:** [25 October 2020 16:00 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-ch4-errata-in-evolve-function-page-59/3427/2 "2020-10-25T16:00:08Z")

</div>

Thank you for finding all these things 🙂. I’m glad you’re enjoying the book! I’ll go back and fix this one as well.

---

<div class="post-metadata">

**Author:** ![patrickdm](https://forum.devtalk.com/user_avatar/forum.devtalk.com/patrickdm/32/629_2.png) [@patrickdm](https://forum.devtalk.com/u/patrickdm)\
**Post date:** [25 October 2020 16:14 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-ch4-errata-in-evolve-function-page-59/3427/3 "2020-10-25T16:14:14Z")

</div>

It is a pleasure to be helpful, I’m just reading the book, studying the concepts and putting the code at work, so the small glitches in the redaction wake up the little debugger-daemon in me… 🐛😈 They actually help me to focus and understand the overall logic and goals! Also I know how hard is to spot them when you know every and each word of your book by memory. I’m glad to be of some help, while learning so much!
