Elixir for Haskell programmers 2

Kevin Da Silva
2 min readMay 3, 2022

Well, I guess I started doing a series about teaching elixir to Haskell programmers, I'm still figuring out and developing ideas that will be well compared between both languages.

Photo by Daniele Levis Pelusi on Unsplash

An important feature in functional languages is pattern matching and in elixir, pattern matching is very similar to the PROLOG pattern matching, luckily it isn't very different from the Haskell way

{:ok, result} = make_request("google.com"){:ok, ^result} = make_request("google.com") // true{:ok, ^result} = make_request("facebook.com") 
// false because the ^result will not be a new variable declaration it will verify if the value of result is equal to the second parameter of the tuple

In Haskell, we would have something very similar

data Status = OK | NOTOK deriving(Show)
main = do
let (OK, result) = makeRequest("google.com")
let (OK, result) = makeRequest("google.com")
// error result is already defined, and we can't pattern match the value of a dynamic variable

Although not being possible to pattern match against a declared variable, pattern matching/destructuring values is very similar between both languages

{:ok, result} = make_request("google.com")let (OK, result) = makeRequest("google.com")

And also the pattern matching in functions is very similar between the languages, like in elixir it would be something like this:

def generic_function(:atom), do: "ATOM"
def generic_function(35), do: "35"
def generic_function(_), do: "I Dunno"

And the equivalence in Haskell:

genericFunction ATOM = "ATOM" 
genericFunction 35 = "35"
genericFunction _ = "I Dunno"

But there's a difference elixir functions have arities of multiple elements that means we can pattern match against different numbers of arguments which is not possible in Haskell

def generic_function(:atom, 35), do: "ATOM 35"
def generic_function(35), do: "35"
def generic_function(_), do: "I Dunno"

And now that we saw how to pattern-match, we can't leave without learning about guards, In Haskell guards have the advantage of declaring the function once.

guardsInHaskell num
| num > 0 = "Greater than zero"
| num < 0 = "Less than zero"
| otherwise = "It's zero"

While in elixir, we kinda have to combine pattern matching and guards like this:

guards_in_elixir(num) when num > 0 do 
"Greater than zero"
end
guards_in_elixir(num) when num <0 do
"Less than zero"
end
guards_in_elixir(_), do: "It's zero"

In elixir to be able to use guards, we have to declare the function as many when clauses we want to check, and we have to open the do end blocks to every function that uses a when clause.

I guess we reached the end of the second episode of a brand-new series, thank you for reading, and you can access the previous article right here.

Bye!

--

--

Kevin Da Silva

I'm a back-end developer, functional programming lover, fascinated by computer science and languages. From the south part of Brazil to the world