How to initialize a list?

1,232 views
Skip to first unread message

xavier nayrac

unread,
Jul 20, 2013, 11:41:09 AM7/20/13
to elixir-l...@googlegroups.com
New to Elixir, I'm trying to initialize a List of 100 elements, all set to false.
Here is what I have:

a = []
Enum.each 1..10, fn _ ->
  a = a ++ [false]    
end


I try this too:

Enum.each 1..10, fn _ ->
  List.insert_at a, 0, false
end


But 'a' doesn't change. I think I don't understand how immutable things works...
Does someone could give me a clue?

atkaaz

unread,
Jul 20, 2013, 11:47:40 AM7/20/13
to elixir group
maybe this?

Enum.each 1..10, fn _ ->
  a = List.insert_at a, 0, false
end



--
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.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

José Valim

unread,
Jul 20, 2013, 11:47:54 AM7/20/13
to elixir-l...@googlegroups.com
Enum.map 1..100, fn _ -> false end

In this example, we are mapping over each value in the range, transforming them into false.

Doing iteration in Elixir is different than in imperative languages. You rarely want to call each for example, because as you noticed, the value of a doesn't change.

The Introducing Erlang and the equivalent Introducing Elixir book are great resources into pattern matching, recursion and other functionality found in functional programming languages.

José Valim
Skype: jv.ptec
Founder and Lead Developer



--

xavier nayrac

unread,
Jul 20, 2013, 12:17:06 PM7/20/13
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Thank you, guys.

atkaaz

unread,
Jul 20, 2013, 12:33:21 PM7/20/13
to elixir group
I kind of expected this to work too:

iex(8)> lc n inlist 1..10, do: false
** (ErlangError) erlang error: {:bad_generator, 1..10}
    :erl_eval.expr/3


but only something like this would work:
iex(8)> lc n inlist [1,2,3], do: false
[false, false, false]
iex(9)> lc n inlist [1..10], do: false
[false]

atkaaz

unread,
Jul 20, 2013, 12:43:26 PM7/20/13
to elixir group
I am sort of explaining this to myself by this:


a = []
Enum.each 1..10, fn _ ->
  IO.puts inspect a
  a = a ++ [false]   
  IO.puts inspect a
end


outputs:
[]
[false]
[]
[false]
[]
[false]
[]
[false]
[]
[false]
[]
[false]
[]
[false]
[]
[false]
[]
[false]
[]
[false]
:ok


What is this called? similar to macro hygiene but it's something else, closure? or something with (local)scope?

Eric Meadows-Jönsson

unread,
Jul 20, 2013, 12:44:26 PM7/20/13
to elixir-l...@googlegroups.com
inlist only works on lists. 1..10 is a record that implements Enumerable. An inenum operator will probably come and then your example will work. lc n inlist [1..10], do: false doesnt work because you create a list the looks like this [Range[first: 1, last: 10]] with only one element.
Eric Meadows-Jönsson

atkaaz

unread,
Jul 20, 2013, 12:50:38 PM7/20/13
to elixir group
Thank you. I figured the one element part but had no idea about everything else. Much appreciated.
Reply all
Reply to author
Forward
0 new messages