CodeX
Published in

CodeX

Elixir for Haskell programmers

Well, you are probably thinking “Why would a Haskell developer be interested in Elixir?” or “There aren't many Haskell developers in the world so this guide wouldn't be so useful”, and the main reason is that I just feel like talking more about elixir and the differences and similarities with Haskell independent of future use for this material, also Elixir is a pretty nice language and has a vibrant community that incentivizes the community to contribute in spreading the word of Elixir. And “Why not a Haskell for Elixir programmers?”, because Haskell is an already very mature language with incredible courses from even more incredible professors that I don't see where I would be able to insert something new while Elixir is still a very young language with lots of stuff to do to spread the language and make the community stronger.

Now that I properly introduced the reason, let's start from the basics:

Photo by Arjun MJ on Unsplash

What is Elixir?

Elixir is a functional language that runs in the Erlang virtual machine, created by José Valim at the former plataformatech(acquired by nubank), also Elixir is hugely inspired by Ruby(José Valim used to be a member of the core team of rails), Clojure(Valim decided to apply macros to turn the language extensible) and Erlang(The style of concurrency actor model for being much more comprehensible for humans and all the capabilities of the Erlang VM to be fault-tolerant and self-heal).

The very basics

It wouldn't be a proper functional language introduction without the emphasis on functions, lo let's take a sip into elixir functions syntax:

defmodule Example do 
def hi() do
IO.puts "Hello World"
end
end
Example.hi()

To be able to use module functions in Elixir, I need to first create a module(defmodule), then define my function(with def keyword), and to execute I just have to run Example.hi()(type this code on the repl and hit run, it should display hello world).

If you thought that the syntax has some c like code you guessed right, it's the inspiration in the Ruby language where we have to open and close blocks, but later we'll see some ways to not type these blocks all the tie.

The equivalent in Haskell

module Examplehi = print "Hello World"

Anonymous functions

In contradiction to Haskell, Elixir has a distinction between module functions and anonymous functions, whereas in Haskell both are equal under the hood.

Example in Haskell

domath x = x + 3-- is equal to(\x -> x+3)example = do
print $ map (domath) [4] -- outputs [7]
print $ map (\x -> x+3) [4] -- outputs [7]

While the equivalence in Elixir wouldn't be possible:

defmodule MathMod do 
def do_math(x) do
x+3
end
def example() do
IO.inspect Enum.map([4], do_math)
end
end
MathMod.hi(4) # outputs 7
MathMod.example() # outputs an error because passing module functons as a parameter generates an error

To be able to pass a function as a parameter, I need to specify an anonymous function like this:

defmodule MathMod do 
def do_math(x) do
x+3
end
def example() do
IO.inspect Enum.map([4], fn x -> x+3 end)
end
end
MathMod.example() # outputs [7]

Also in parts for this distinction between functions, Elixir doesn't have auto curry, so we have to manually curry our functions and only anonymous functions can be curried:

anFunc = fn x -> fn y -> x+y end endIO.puts anFunc.(3).(4) # outputs 7

Shortening our functions

As I mentioned before, we can save some time by not writing some of the open-close parents or code blocks:

defmodule MathMod do 
def do_math(x) do
x+3
end
end
# is equal todefmodule MathMod do
def do_math(x), do: x+3
end

And also:

defmodule MathMod do 
def example() do
IO.inspect(Enum.map([4], fn x -> x+3 end))
end
end
# is equal todefmodule MathMod do
def example, do: IO.inspect Enum.map([4], &(&1+3))
#notice that &1
# is an alias for the first parameter(the x) if our function had
# more parameters the alias would be &2, &3...&n
end

Whew, I think that for the first sip of Elixir we covered a bunch of things, probably it isn't the most beginner-friendly way to start but if you are familiar with Haskell I tried to let it as most familiar as possible, I don't know if I will continue and turn this into a series, but I still hope to have contributed in your learning journey in some way.

Thanks for reading! Bye :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store