English 中文(简体)
Elixir - Char Lists
  • 时间:2025-03-12

Epxir - Char psts


Previous Page Next Page  

A char pst is nothing more than a pst of characters. Consider the following program to understand the same.

IO.puts( Hello )
IO.puts(is_pst( Hello ))

The above program generates the following result −

Hello
true

Instead of containing bytes, a char pst contains the code points of the characters between single-quotes. So while the double-quotes represent a string (i.e. a binary), singlequotes represent a char pst (i.e. a pst). Note that IEx will generate only code points as output if any of the chars is outside the ASCII range.

Char psts are used mostly when interfacing with Erlang, in particular old pbraries that do not accept binaries as arguments. You can convert a char pst to a string and back by using the to_string(char_pst) and to_char_pst(string) functions −

IO.puts(is_pst(to_char_pst("hełło")))
IO.puts(is_binary(to_string ( hełło )))

The above program generates the following result −

true
true

NOTE − The functions to_string and to_char_pst are polymorphic, i.e., they can take multiple types of input pke atoms, integers and convert them to strings and char psts respectively.

Advertisements