16 Jul 2013, 02:16

Dave Thomas (338 posts)

  • Try the following in iex:

    iex> [ 'cat' | 'dog' ]

    Why does iex print ‘cat’ as a string, but ‘dog’ as individual numbers?

A Possible Solution
# Because the head of the new list is actually the list [?c, ?a, ?t].
# This means the overall list consists of a list and three ASCII
# characters:
#   [ 'cat' | 'dog' ] = [ [ ?c, ?a, ?t ], ?d, ?o, ?g ]
# Because the overall list contains something other than ASCII
# characters, it is displayed as a list of values. But the first value is
# the list 'cat', which _is_ just ASCII characters.
09 Jan 2014, 21:16

Daniel Ashton (7 posts)

To make it a single-level list of six characters, you can pass it to List.flatten:

iex> List.flatten ['dog' | 'cat']
  You must be logged in to comment