small medium large xlarge

Generic-user-small
28 Oct 2015, 12:24
Audun Bjarne Tønnesen (5 posts)

In the Dave Thomas book there’s an example of defining a map-function: - - - - def map([], _func), do: [] def map([ head | tail ], func), do: [ func.(head) | map(tail, func) ] - - - - Look at these runs: Why do the runs marked ‘NB’ produce so strange results? (Erlang 18.1, Elixir 1,1)

iex(9)> MyList.map [5,6,7,8], &(&1*&1) [25, 36, 49, 64]

iex(10)> MyList.map [5,6,7,8], &(&1*&1) [25, 36, 49, 64]

iex(11)> MyList.map [6,7,8], &(&1*&1) NB ‘$1@’

iex(12)> MyList.map [7,8,9], &(&1*&1) NB ‘1@Q’

iex(13)> MyList.map [17,18,19], &(&1*&1) [289, 324, 361]

iex(14)> MyList.map [8,9,10], &(&1*&1) NB ‘@Qd’

iex(15)> MyList.map [9,10,11], &(&1*&1) NB ‘Qdy’

iex(16)> MyList.map [9,10,12], &(&1*&1) [81, 100, 144]

iex(25)> MyList.map [6,7,8,12], &(&1*&1) [36, 49, 64, 144]

iex(25)> MyList.map [6,7,8,12], &(&1*&1) [36, 49, 64, 144]

Passing in another function:

iex(26)> MyList.map [1,2,3,4], &(&1+2) [3, 4, 5, 6]

iex(27)> MyList.map [6,7,8,9], &(&1+2) NB ‘\b\t\n\v’

iex(28)> MyList.map [6,7,8,11], &(&1+2) NB ‘\b\t\n\r’

iex(29)> MyList.map [11,12,13,14], &(&1+2) [13, 14, 15, 16]

Shoud I report this as an erratum, or is it perhaps an Elixir bug?

2015-09-18_headshot_pragsmall
05 Dec 2015, 16:45
Michael Herold (2 posts)

I believe this is because of how Elixir displays lists. See the inset “How iex Displays Lists” on page 63 and see if that matches what you’re seeing.