Instantly share code, notes, and snippets.

biomunky/Goofing with Elixir Last active Dec 21, 2015

Goofing with Elixir Lang
 ## ## Define an .ex file that is compiled with elixirc ## defmodule Hack do def reverse_list([h|t], acc // []) do Hack.reverse_list(t, List.concat([h], acc)) end def reverse_list([], acc) do acc end def sum([h|t], acc) do sum(t, h + acc) end def sum([], acc) do acc end end ## ## compile: elixirc reverse-list.ex ## start iex in same dir as the .beam ## ## iex(1)> Hack.reverse_list [1,2,3] ## [3, 2, 1] ## ## ## Now define a .exs (script file) ## defmodule Hack do def reverse_list([h|t], acc) do Hack.reverse_list(t, List.concat([h], acc)) end def reverse_list([], acc) do acc end def sum([h|t], acc) do sum(t, h + acc) end def sum([], acc) do acc end end x = Hack.reverse_list [1,2,3], [] IO.puts "#{is_list x}" IO.puts "The length of x is #{length x}" IO.puts "#{x}" theSum = Enum.reduce(x, 0, fn(x, acc) -> x + acc end) IO.puts "The sum of the invisible list is #{theSum}" y = Hack.sum [1,2,3], 0 IO.puts "The result is #{y}" ## ## Save this and run it using: ## elixir reverse-list.exs ## ## the output of script is: ## >> true ## >> The length of x is 3 ## >> ## >> The sum of the invisible list is 6 ## >> The result is 6 ## The question is... why am i not seeing the list in stdout yet i am seeing that it's a list, it has length 3 and sum of 6 ## confused!