Character syntax

Super noob question that I came across while looking at some exercism examples. In charlist like ‘ABCD’ you can’t match on ['A' | tail] like I would have expected. Instead you have to use this question-mark syntax [?A | tail]. Can someone point me to the documentation for that syntax?

1 Like

Check https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#unicode-and-code-points

The ? returns the codepoint for the character.

A charlist is a list of integers, so ABCD is the same as [65, 66, 67, 68].
If you try to match it with ['A' | tail], you’re basically saying the list should look like [[65], 66, 67, 68]. As it’s a list of integers, you need to match the first integer, ie the first codepoint with ?A instead of 'A'.

6 Likes