Skip to content

Latest commit

 

History

History

lists

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Lists

Reading Material

Recursion and high order functions can be tricky if you don't have a functional programming background, for that reason take your time to read and really understand the concepts.

Exercises

Reverse

This exercise consists in creating the function lists_exercises:reverse/1 which should be Tail Recursive, and take a list as argument and return another list with every element in the opposed position.

Example:

1> lists_exercises:reverse([1, 2, 3, 4]).
%% [4, 3, 2, 1]

solution

Remove consecutive

Create a function lists_exercises:rmconsecutive/1 that takes a list and returns another one but without any consecutive repetitions.

Example:

1> lists_exercises:rmconsecutive([1,1,1,2,3,4,1,1,1,3]).
%% [1, 2, 3, 4, 1, 3]

solution

Even Fibonacci Numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

solution

Reduce

Implement lists_exercises:foldl/3 and lists_exercises:foldl/2 using recursion (see Erlang foldl reference).

Example:

1> lists_exercises:foldl(fun(X, Y) -> X + Y end, 0, [1, 2, 3, 4]).
%% 10

2> lists_exercises:foldl(fun(X, Y) -> X * Y end, [1, 2, 3]).
%% 6

3> lists_exercises:foldl(func(X, Y) -> X andalso Y, [true, false, true]).
%% false

solution

Rotate Lists

Create a function lists_exercises:rotate/2 that rotates the contents of a list n positions. It should take 2 arguments:

  • A list
  • A tuple of {left, N} or {right, N} that indicates the direction and the size of the displacement expected.

Example:

1> lists_exercises:rotate([1, 2, 3, 4, 5], {right, 2}).
%% [4,5,1,2,3]

solution

Run-length encoding of a list

Implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as terms {N,E} where N is the number of duplicates of the element E.

Example:

1> lists_exercises:run_length_encode([a,a,a,a,b,c,c,a,a,d,e,e,e,e]).
%%[{4,a},{1,b},{2,c},{2,a},{1,d},{4,e}]

solution

Any

Write a function lists_exercises:list_any/2 that takes a predicate (a function that returns a boolean value) and a list and returns true if any element of the list satisfies the predicate and false otherwise. Note: Implement it without using lists:list_any.

Example:

1> lists_exercises:list_any(fun(X) -> X > 3 end, [4, 2, 0]).
%% true

2> lists_exercises:list_any(fun(X) -> X == 0 end, [1, 2, 3]).
%% false

solution

Anagram

Write a function list_exercises:anagram/2 that takes a list of strings, a string ("MyWord"), and returns all elements of the list that are an anagram of "MyWord"

Example:

1>lists_exercises:anagram(["god","cat","dog"], "dog").
%%["god"]

solution

First letter last letter game

There is a game called first letter, last letter. The object of this game is for one player to say a word apple, and for the other player to say a word that begins with the last letter of the previous word, i.e. elephant.

Write a function lists:exercises:last_letter/1 that takes a list of strings and return a list where the subsequent name starts with the final letter of the previous name. Take the first element of the list as the first word of the sequence, that names cannot be repeated.

Example:

1> lists_exercises:last_letter(["turnIp","Potato","peas","OniOn","yam","letuCe","broccoli",
 "asparaguS","Artichoke"]).
 %%["turnIp","Potato","OniOn"]

2> lists_exercises:last_letter([])
%% []

solution