List insert_at with empty list returns '\a'

70 views
Skip to first unread message

Bibek Pandey

unread,
Aug 8, 2016, 8:15:39 PM8/8/16
to elixir-lang-talk, Voicemail
Hi all --

I'm getting a '\a' when trying to insert into an empty list.  Doesn't appear to be expected behavior.

$ iex
Erlang/OTP 19 [erts-8.0.2] [source-9503fff] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> List.insert_at([], 0, 7)
'\a'
iex(2)> List.insert_at([], 1, 7)
'\a'
iex(3)> List.insert_at([], 2, 7)
'\a'
iex(4)> List.insert_at([3], 2, 7)
[3, 7]
iex(5)> List.insert_at([3], 0, 7)
[7, 3]
iex(6)> List.insert_at([3], 1, 7)
[3, 7]
iex(7)>

 
Thanks B

Theron Boerner

unread,
Aug 8, 2016, 8:20:38 PM8/8/16
to elixir-lang-talk, Voicemail
Lists are (sometimes) strings. \a is 7. Ergo, [7] == '\a'

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAF5zmfxOVPpDqVCQCTjHy5gJwEW74JbHH2m6BK3ADRubD5GoPA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Bibek Pandey

unread,
Aug 8, 2016, 8:23:45 PM8/8/16
to Theron Boerner, elixir-lang-talk
That's right -- but feels like something changed as this was working okay for me a few days ago.  Anyhow thanks

On Mon, Aug 8, 2016 at 5:20 PM, Theron Boerner <hunter...@gmail.com> wrote:
Lists are (sometimes) strings. \a is 7. Ergo, [7] == '\a'

On Mon, Aug 8, 2016 at 7:15 PM Bibek Pandey <bib...@gmail.com> wrote:
Hi all --

I'm getting a '\a' when trying to insert into an empty list.  Doesn't appear to be expected behavior.

$ iex
Erlang/OTP 19 [erts-8.0.2] [source-9503fff] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> List.insert_at([], 0, 7)
'\a'
iex(2)> List.insert_at([], 1, 7)
'\a'
iex(3)> List.insert_at([], 2, 7)
'\a'
iex(4)> List.insert_at([3], 2, 7)
[3, 7]
iex(5)> List.insert_at([3], 0, 7)
[7, 3]
iex(6)> List.insert_at([3], 1, 7)
[3, 7]
iex(7)>

 
Thanks B

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.

Louis Pilfold

unread,
Aug 8, 2016, 8:27:23 PM8/8/16
to elixir-lang-talk, Voicemail

Hey

[7] == '\a'

Single quote strings are actually lists of characters, so when you have a list of numbers that can be treated as characters Elixir will print them as like so.

Cheers,
Louis


--

Ben Wilson

unread,
Aug 8, 2016, 9:41:07 PM8/8/16
to elixir-lang-talk, bib...@gmail.com, lo...@lpil.uk
You can read more about it in the getting started guides: http://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.

Bibek Pandey

unread,
Aug 8, 2016, 10:47:53 PM8/8/16
to Ben Wilson, elixir-lang-talk, lo...@lpil.uk, Voicemail
Thanks folks.  I appreciate it

I guess logically I understand it and have before but I feel like I have some intuitive gap/resistance to really accept this (because I feel like I have run into this before).

In the spirit of Haskell, it would be interesting to have a Maybe type to wrap the List type.  So Maybe List could return a list, a single char or a list with a single num, or an absence of both.

http://learnyouahaskell.com/a-fistful-of-monads

The return type of insert_at is a list and so technically this works fine, but folks just have a lot of programmer word gravity and baggage -- LOL.  So even though '\a' is in fact [7] which is a list, `\a` in itself printed out doesn't look like a list.

Just my 2 --


Enjoy the Olympics!
B

Onorio Catenacci

unread,
Aug 9, 2016, 4:05:28 PM8/9/16
to elixir-lang-talk, benwil...@gmail.com, lo...@lpil.uk, bib...@gmail.com
For what it's worth @B, there's a good simple reason that this behaves the way it does.  


TL;DR Erlang error codes are returned as char lists.  Hence defaulting to displaying char lists as text helps significantly with interfacing with Erlang. (Apologies if I'm summarizing incorrectly but that's my understanding). 

--
Onorio

Ben Wilson

unread,
Aug 9, 2016, 4:08:27 PM8/9/16
to elixir-lang-talk, benwil...@gmail.com, lo...@lpil.uk, bib...@gmail.com
Maybe type with list doesn't make any sense. For one thing the maybe type is Nothing | Some(a) but that isn't what we have here at all. Thus a Maybe list would be Nothing | Some(list), which doesn't cover the various single char and so on you talked about.

I think you're over thinking it. '\a' and [7] are just two different ways of writing exactly the same list.


On Monday, August 8, 2016 at 10:47:53 PM UTC-4, brpandey wrote:

Bibek Pandey

unread,
Aug 9, 2016, 9:19:10 PM8/9/16
to Ben Wilson, elixir-lang-talk, lo...@lpil.uk
@BW  Yes, I'm probably overthinking it​. The below type defs are pretty straightforward
​ -- this is not a type issue at all -- ​


list() [any()]
charlist() [char()]
char() 0..0x10FFFF

Since the culprit for me here is the IO display -- I think the easiest solution is what @O is referencing (Thanks!) -->
iex(1)> IEx.configure inspect: [char_lists: false]
:ok
iex(2)> [7]
[7]

iex(3)> List.insert_at([], 2, 7)
[7]
iex(4)> :)





Reply all
Reply to author
Forward
0 new messages