Exercises on Elixir Maps, Keyword Lists, Sets, and Structs

Adolfo Neto
2 min readOct 29, 2019
Photo by Andrew Stutesman on Unsplash

My students from the “Introduction to Functional Programming” course are studying Chapter 8 of Dave Thomas’ Programming Elixir 1.6, which is about Maps, Keyword Lists, Sets, and Structs.

These are the exercises they will have to do today:

1) Let kl be a keyword list defined as following:

kl = [name: “Ada Lovelace”, born: 1815, died: 1852, known_for: “mathematics”, known_for: “computing” ]

Write a command that, using data from kl, prints:

Ada Lovelace was known for mathematics and computing.

You must use IO.puts/1 with #{}, Keyword.get_values/2 and Enum.join/2.

2) Create a map for representing the following information:

Joe Armstrong: Programming Erlang — Software for a Concurrent World (2nd edition), The Pragmatic Bookshelf, 2013.

3) Create a list of maps for representing the following information:

F. Cesarini and S. Thompson: Erlang Programming — A Concurrent Approach to Software Development, O’Reilly, 2009.

M. Logan, E. Merrit, R. Carlsson: Erlang and OTP in Action, Manning Eds, 2010.

F. Hébert: Learn you Some Erlang for Great Good!, No Starch Press, 2013.

Martin Odersky, Lex Spoon, Bill Venners: Programming in Scala, Artima Press 2010.

M. Fogus: Functional Javascript, O’Reilly, 2013.

Stuart Halloway, Aaron Bedra: Programming Clojure, The Pragmatic Bookshelf, 2012.

M. Bevilacqua-Linn: Functional programming Patterns in Scala and Clojure, The Pragmatic Bookshelf, 2013.

4) Iterate over the list of maps from question 3so that only the titles of books published in 2010 or earlier are printed.

5) Solve question 4 again now using Structs instead of Maps.

6) Sets

a) Create a set s1 with all numbers between 10 and 100.

b) Create a set s2 with all numbers between 50 and 150.

c) Create a set s3 which is the union of s1 and s2

d) Create a set s4 which is the intersection of s1 and s3

e) Create a set s5 which is the difference of s3 and s4

f) Calculate the sum of the sizes of all sets from the items above.

What do you think? Too easy?

--

--

Adolfo Neto

Associate Professor at UTFPR. Interested in programming (Elixir), logic and Deep Work.