Enum.filter returns wrong output '\v\f'

This is a common misunderstanding :slight_smile: you’ll find other posts with similar questions (for example this or this).

The reason is that '\v\f' and [11, 12] are the same thing. Here is why: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#charlists

Basically, single quotes are used to create charlists, which are lists of Unicode codepoints. The iex console, when it encounters a list of integers, if all the integers correspond to codepoints of printable characters, will present the list as a charlist. Otherwise, it represents it as a list of integers. Under the hood they are the same thing, it’s just a matter of presentation.

The integer 11 is the codepoint of the character \v (a vertical tab) and the integer 12 is the codepoint for \f (a form feed character). Both are printable, so the list [11, 12] is printed as '\v\f' by the console (you can even try to type [11, 12] to see that, or type [104, 101, 108, 108, 111] to get 'hello').

When the result of your Enum.filter call is [11, 12, 13, 14, 15, 16], the integers 14, 15, and 16 do not correspond to printable codepoints, so the list is not presented as a charlist, but as a list of integers. Once again, it is actually the same thing.

7 Likes