16 Jul 2013, 02:16
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 posts)

  • Try the following in iex:

    iex> [ 'cat' | 'dog' ]
    ['cat',100,111,103]
    

    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
Generic-user-small

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']
'dogcat'
  You must be logged in to comment