# Genetic Algorithms in Elixir: Code throws error (page 111)

**URL:** <https://forum.devtalk.com/t/genetic-algorithms-in-elixir-code-throws-error-page-111/7841>\
**Category:** PragProg Customers\
**Tags:** question, book-genetic-algorithms-in-elixir\
**Created:** [13 March 2021 10:00 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-code-throws-error-page-111/7841 "2021-03-13T10:00:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joustava](https://forum.devtalk.com/user_avatar/forum.devtalk.com/joustava/32/2549_2.png) [@joustava](https://forum.devtalk.com/u/joustava)\
**Post date:** [13 March 2021 10:00 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-code-throws-error-page-111/7841/1 "2021-03-13T10:00:48Z")

</div>

Not sure if I’m doing something wrong here, but it seems that this piece of code will throw errors:

```elixir
def fitness_function(chromosome) do
    target = 'ILoveGeneticAlgorithms'
    encrypted = 'LIjs`B`k`qlfDibjwlqmhv'
    cipher = fn word, key -> Enum.map(word, & rem(&1 ^^^ key, 32768)) end
    key =
      chromosome.genes
      |> Enum.map(&Integer.to_string/1)
      |> Enum.join("")
      |> String.to_integer(2)

      guess = List.to_string(cipher.(encrypted, key))
      String.jaro_distance(target, guess)
end

```

As

```auto
String.jaro_distance(target, guess)

```

Doesn’t expect ‘charlists’ but binaries

```auto
** (FunctionClauseError) no function clause matching in String.Unicode.graphemes/1

    The following arguments were given to String.Unicode.graphemes/1:

        # 1
        'ILoveGeneticAlgorithms'

    Attempted function clauses (showing 1 out of 1):

        def graphemes(binary) when is_binary(binary)

    (elixir 1.10.3) lib/elixir/unicode/unicode.ex:252: String.Unicode.graphemes/1
    (elixir 1.10.3) lib/string.ex:2488: String.jaro_distance/2
    (genetic 0.1.0) lib/genetic.ex:24: anonymous fn/2 in Genetic.evaluate/3
    (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
    (genetic 0.1.0) lib/genetic.ex:23: Genetic.evaluate/3
    (genetic 0.1.0) lib/genetic.ex:116: Genetic.evolve/6
    scripts/code_breaker.exs:39: (file)

```

I’m running this on:

```auto
⟩ elixir --version
Erlang/OTP 23 [erts-11.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [hipe]

Elixir 1.10.3 (compiled with Erlang/OTP 23)

```

Not sure if something has changed recently regarding this issue.

Also, it seems the capture operator ‘&’ is in the wrong place in the book and should be moved before the call to ‘rem’ otherwise a

```auto
** (ArithmeticError) bad argument in arithmetic expression: rem(#Function<1.106933999/1 in CodeBreaker.fitness_function/1>, 32768)
    :erlang.rem(#Function<1.106933999/1 in CodeBreaker.fitness_function/1>, 32768)
    scripts/code_breaker.exs:16: anonymous fn/2 in CodeBreaker.fitness_function/1
    (genetic 0.1.0) lib/genetic.ex:24: anonymous fn/2 in Genetic.evaluate/3
    (elixir 1.10.3) lib/enum.ex:1396: Enum."-map/2-lists^map/1-0-"/2
    (genetic 0.1.0) lib/genetic.ex:23: Genetic.evaluate/3
    (genetic 0.1.0) lib/genetic.ex:116: Genetic.evolve/6
    scripts/code_breaker.exs:33: (file)

```

will occur.  
I think on the next page in the iex example it is the correct way.

---

<div class="post-metadata">

**Author:** ![adkelley](https://forum.devtalk.com/user_avatar/forum.devtalk.com/adkelley/32/2873_2.png) [@adkelley](https://forum.devtalk.com/u/adkelley)\
**Post date:** [4 June 2021 04:00 UTC](https://forum.devtalk.com/t/genetic-algorithms-in-elixir-code-throws-error-page-111/7841/2 "2021-06-04T04:00:56Z")

</div>

> [@joustava](#):
>
> ` target = 'ILoveGeneticAlgorithms'`

Strings in Elixir are inserted between double quotes. Therefore change: `target = 'ILoveGeneticAlgorithms'` to: `target = "ILoveGeneticAlgorithms"`
