Working through PE.

This commit is contained in:
Kyle Isom 2019-04-22 23:49:46 +00:00
parent 8450ce8753
commit 6220e18137
3 changed files with 115 additions and 4 deletions

47
pe/chapter4.erl Normal file
View File

@ -0,0 +1,47 @@
-module(chapter4).
-export([t2l/1, timeit/1]).
% 2. 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.
t2l(T) ->
[ element(I, T) || I <- lists:seq(1, tuple_size(T)) ].
% 3. 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.
% erlang:time -> Returns the current time as {Hour, Minute, Second}.
% erlang:date -> Returns the current date as {Year, Month, Day}.
% erlang:now -> deprecated, should use erlang:timestamp
% erlang:timestamp -> Returns current Erlang system time on the format
% {MegaSecs, Secs, MicroSecs}.
% erlang:system_time(Unit) -> Returns current Erlang system time
% converted into the Unit passed as argument.
timeit(F) ->
Started = erlang:system_time(microsecond),
F(),
{erlang:system_time(microsecond) - Started, microsecond}.
% 4. Advanced: Look up the manual pages for the Python datetime
% module. Find out how many of methods in the Python datetime class
% can be implemented using the time-related BIFs in the erlang
% module. Search the erlang manual pages for equivalent
% routines. Implement any glaring omissions.
% 5. 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.
% 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.
% 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.

63
pe/lib_misc.erl Executable file
View File

@ -0,0 +1,63 @@
-module(lib_misc).
-export([
for/3,
qsort/1,
pythag/1,
perms/1,
kabs/1,
kfilter/2,
kclassify/1
]).
for(Max, Max, F) ->
[F(Max)];
for(I, Max, F) ->
[F(I)|for(I+1, Max, F)].
qsort([]) ->
[];
qsort([Pivot|T]) ->
qsort([X || X <- T,
X < Pivot])
++
[Pivot]
++
qsort([X || X <- T,
X >= Pivot]).
pythag(N) ->
[{A,B,C} ||
A <- lists:seq(1,N),
B <- lists:seq(1,N),
C <- lists:seq(1,N),
A+B+C =< N,
A*A+B*B =:= C*C
].
perms([]) ->
[[]];
perms(L) ->
[[H|T] || H <- L,
T <- perms(L--[H])].
kabs(V) when is_integer(V), V >= 0 ->
V;
kabs(V) when is_integer(V) -> -V.
kfilter(P, [H|T]) ->
case P(H) of
true -> [H|kfilter(P, T)];
false -> kfilter(P, T)
end;
kfilter(_, []) -> [].
kclassify(N) ->
if
N >= 10 ->
high;
N >= 3 ->
medium;
true ->
low
end.

View File

@ -8,7 +8,8 @@ cost (apples) -> 2;
cost (pears) -> 9;
cost (milk) -> 7.
total([]) ->
0;
total([{Item, Count}|Rest]) ->
cost(Item) * Count + total(Rest).
total(ShoppingList) ->
lists:sum(
lists:map(fun ({Item, Count}) ->
Count * cost(Item) end,
ShoppingList)).