Last active
August 29, 2015 14:00
-
-
Save romulojales/11203962 to your computer and use it in GitHub Desktop.
Exercises from "Programming Erlang: Software for a Concurrent world."
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%------------------------------------------------------------------- | |
%%% @author romulo.jales | |
%%% @copyright (C) 2014, <COMPANY> | |
%%% Exercise 2 chapter 4 "programming erlang: Software for a concurrent world" | |
%%% The BIF tuple_to_list( T) converts the elements of the tuple T to a list. | |
%%% Write a function called my_tuple_to_list( T) that does the same thing only not using the BIF that does this. | |
%%% @end | |
%%% Created : 23. Apr 2014 1:42 AM | |
%%%------------------------------------------------------------------- | |
-module(e2c4). | |
-author("Rômulo Jales"). | |
%% API | |
-export([my_tuple_to_list/1, test_my_tuple_to_list/0]). | |
my_tuple_to_list({}) -> []; | |
my_tuple_to_list(Tuple) -> | |
El = element(1, Tuple), | |
[El|my_tuple_to_list(erlang:delete_element(1, Tuple))]. | |
test_my_tuple_to_list()-> | |
[1,2,3,4,5] = my_tuple_to_list({1,2,3,4,5}), | |
[1,2,3,4,5, {6,7}] = my_tuple_to_list({1,2,3,4,5,{6,7}}), | |
[] = my_tuple_to_list({}), | |
test_passed. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%------------------------------------------------------------------- | |
%%% @author romulo.jales | |
%%% @copyright (C) 2014, <COMPANY> | |
%%% Exercise 2 chapter 5 "programming erlang: Software for a concurrent world" | |
%%% Write a function map_search_pred(Map, Pred) that returns the first element {Key, Value} in the map for which Pred(Key, Value) is true. | |
%%% @end | |
%%% Created : 13. May 2014 0:09 AM | |
%%%------------------------------------------------------------------- | |
-module(e2c5). | |
-author("Rômulo Jales"). | |
-export([map_search_pred/2, test_map_search_pred/0]). | |
find_pred([], Pred) -> {}; | |
find_pred([H|T], Pred) -> | |
{Key, Value} = H, | |
case Pred(key, Value) of | |
true -> {Key, Value}; | |
false -> find_pred(T, Pred) | |
end. | |
map_search_pred(Map, Pred) -> | |
find_pred(maps:to_list(Map), Pred). | |
key_odd_value_even(Key, Value)-> | |
if Key rem 2 == 0 -> | |
false; | |
Value rem 2 /= 0 -> | |
false; | |
true -> true | |
end. | |
test_map_search_pred()-> | |
{} = map_search_pred(#{}, fun key_odd_value_even/2), | |
{} = map_search_pred(#{2=>1}, fun key_odd_value_even/2), | |
{1, 2} = map_search_pred(#{1=>2}, fun key_odd_value_even/2), | |
{5, 4} = map_search_pred(#{1=>3, 2=>3, 3=>3, 5=>4 }, fun key_odd_value_even/2), | |
test_passed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%------------------------------------------------------------------- | |
%%% @author romulo.jales | |
%%% @copyright (C) 2014, <COMPANY> | |
%%% @doc | |
%%% Exercise 3 chapter 4 "programming erlang: Software for a concurrent world" | |
%%% Look up the definitions of erlang:now/0, erlang:date/0, and erlang:time/0. | |
%%% Write a function called my_time_func(F), which evaluates the fun F and times how long it takes. | |
%%% Write a function called my_date_string() that neatly formats the current date and time of day. | |
%%% @end | |
%%% Created : 23. Apr 2014 2:47 AM | |
%%%------------------------------------------------------------------- | |
-module(e3c4). | |
-author("Rômulo Jales"). | |
%% API | |
-export([my_date_string/0, my_time_func/1]). | |
print_diff_now(T1, T2) -> | |
{Mega1, Sec1, Micro1} = T1, | |
{Mega2, Sec2, Micro2} = T2, | |
string:join([integer_to_list(Mega2 - Mega1), integer_to_list(Sec2-Sec1), integer_to_list(Micro2-Micro1)], ":"). | |
my_time_func(F)-> | |
T1 = now(), | |
F(), | |
T2 = now(), | |
print_diff_now(T1, T2). | |
my_date_string()-> | |
{Year, Month, Day} = date(), | |
{Hour, Minute, Second} = time(), | |
% DD/MM/YYYY | |
Date = string:join([integer_to_list(Day), integer_to_list(Month), integer_to_list(Year)], "/" ), | |
Time = string:join([integer_to_list(Hour), integer_to_list(Minute), integer_to_list(Second)], ":"), | |
string:join([Date, Time], " "). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%%------------------------------------------------------------------- | |
%%% @author romulo.jales | |
%%% @copyright (C) 2014, <COMPANY> | |
%%% @doc | |
%%% Exercise 5 chapter 4 "programming erlang: Software for a concurrent world" | |
%%% Write a module called math_functions.erl, exporting the functions even/ 1 and odd/1. | |
%%% The function even( X) should return true if X is an even integer and otherwise false. | |
%%% odd( X) should return true if X is an odd integer. | |
%%% | |
%%% Exercise 6 | |
%%% Add a higher-order function to math_functions.erl called filter( F, L), which returns all the elements X in L for which F( X) is true. | |
%%% | |
%%% Exercise 7 | |
%%% Add a function split( L) to math_functions.erl, which returns {Even, Odd} | |
%%% where Even is a list of all the even numbers in L and Odd is a list of all the odd numbers in L. | |
%% Write this function in two different ways using accumulators and using the function filter you wrote in the previous exercise. | |
%%% @end | |
%%% Created : 23. Apr 2014 4:06 AM | |
%%%------------------------------------------------------------------- | |
-module(math_functions). | |
-author("romulo.jales"). | |
%% API | |
-export([even/1, odd/1, test_even/0, test_odd/0, filter/2, test_filter_even/0, split_filter/1, test_split_filter/0, split_acc/1, test_split_acc/0]). | |
even(X) -> (X rem 2) =:= 0. | |
odd(X) -> | |
not even(X). | |
filter(F, L)-> [X || X <- L, F(X)]. | |
split_filter(L)-> | |
Even = filter(fun even/1, L), | |
Odd = filter(fun odd/1, L), | |
{Even, Odd}. | |
split_acc(L)-> | |
acc_split(lists:reverse(L), [], []). | |
acc_split([], Even, Odd) -> | |
{Even, Odd}; | |
acc_split([H|T], Even, Odd)-> | |
case (H rem 2) of | |
1 -> acc_split(T, Even, [H|Odd]); | |
0 -> acc_split(T, [H|Even], Odd) | |
end. | |
test_split_acc()-> | |
{[2,4,6,8], [1,3,5,7,9]} = split_acc([1,2,3,4,5,6,7,8,9]), | |
test_split_acc_ok. | |
test_split_filter()-> | |
{[2,4,6,8], [1,3,5,7,9]} = split_filter([1,2,3,4,5,6,7,8,9]), | |
test_split_filter_ok. | |
test_even()-> | |
true = even(2), | |
true = even(256), | |
false = even(3), | |
false = even(121), | |
test_even_ok. | |
test_odd()-> | |
false = odd(2), | |
false = odd(256), | |
true = odd(3), | |
true = odd(121), | |
test_odd_ok. | |
test_filter_even()-> | |
[2,4,6,8] = filter(fun even/1, [1,2,3,4,5,6,7,8,9]), | |
test_filter_even_ok. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment