Iain Munro

Iain Munro

Software engineer

Erlang Day 2 – High-order functions and advanced list concepts

I’m starting to get into functional programming. The best thing I like so far of it all is that all of this is extremely easy to test! The prolog-based syntax of Erlang is still quite annoying, I constantly forget to end lines with dots and forget when to make use of semi-colons. Probably that because I was most familiar with the C-style languages and its on top of the functional programming paradigm that has its own set of challenges. But I’ll slowly get used to it!

Another interesting read is probably: Don’t drink too much Kool-Aid It talks about how you should never go full retard and think that Erlang is the answer to all your problems 🙂 It did get me excited for functional programming though! (I think might try F# and Haskell next!)

Exercises

List key lookup

Consider a list of keyword-value tuples, such as:
[{erlang, "a functional language"}, {ruby, "an OO language"}]
Write a function that accepts the list and a keyword and returns the associated value for the keyword.

Looking back to it, this could also have been done with just a pipe and filter. Which I’ll use in the next exercise.

Total price

Consider a shopping list that looks like [{item quantity price}, …]. Write a list comprehension that builds a list of items of the form [{item total_price}, …], where total_price is quantity times price.

So we’ll order some beer and some snack to go with it!

Bonus: Tic-tac-toe

Due to a far too busy week, full of exams, deadlines I wasn’t able to get around making this exercise. However, I would probably make a solution using the switch mapping all the possible outcomes.

DROP A COMMENT

Your email address will not be published. Required fields are marked *