Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
11
Task/Gapful-numbers/Erlang/gapful-numbers-1.erl
Normal file
11
Task/Gapful-numbers/Erlang/gapful-numbers-1.erl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
-module(gapful).
|
||||
-export([first_digit/1, last_digit/1, bookend_number/1, is_gapful/1]).
|
||||
|
||||
first_digit(N) ->
|
||||
list_to_integer(string:slice(integer_to_list(N),0,1)).
|
||||
|
||||
last_digit(N) -> N rem 10.
|
||||
|
||||
bookend_number(N) -> 10 * first_digit(N) + last_digit(N).
|
||||
|
||||
is_gapful(N) -> (N >= 100) and (0 == N rem bookend_number(N)).
|
||||
39
Task/Gapful-numbers/Erlang/gapful-numbers-2.erl
Normal file
39
Task/Gapful-numbers/Erlang/gapful-numbers-2.erl
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
-module(stream).
|
||||
-export([yield/1, naturals/0, naturals/1, filter/2, take/2, to_list/1]).
|
||||
|
||||
yield(F) when is_function(F) -> F().
|
||||
|
||||
naturals() -> naturals(1).
|
||||
naturals(N) -> fun() -> {N, naturals(N+1)} end.
|
||||
|
||||
filter(Pred, Stream) ->
|
||||
fun() -> do_filter(Pred, Stream) end.
|
||||
|
||||
do_filter(Pred, Stream) ->
|
||||
case yield(Stream) of
|
||||
{X, Xs} ->
|
||||
case Pred(X) of
|
||||
true -> {X, filter(Pred, Xs)};
|
||||
false -> do_filter(Pred, Xs)
|
||||
end;
|
||||
halt -> halt
|
||||
end.
|
||||
|
||||
take(N, Stream) when N >= 0 ->
|
||||
fun() ->
|
||||
case yield(Stream) of
|
||||
{X, Xs} ->
|
||||
case N of
|
||||
0 -> halt;
|
||||
_ -> {X, take(N - 1, Xs)}
|
||||
end;
|
||||
halt -> halt
|
||||
end
|
||||
end.
|
||||
|
||||
to_list(Stream) -> to_list(Stream, []).
|
||||
to_list(Stream, Acc) ->
|
||||
case yield(Stream) of
|
||||
{X, Xs} -> to_list(Xs, [X|Acc]);
|
||||
halt -> lists:reverse(Acc)
|
||||
end.
|
||||
9
Task/Gapful-numbers/Erlang/gapful-numbers-3.erl
Normal file
9
Task/Gapful-numbers/Erlang/gapful-numbers-3.erl
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
-module(gapful_demo).
|
||||
-mode(compile).
|
||||
|
||||
report_range([Start, Size]) ->
|
||||
io:fwrite("The first ~w gapful numbers >= ~w:~n~w~n~n", [Size, Start,
|
||||
stream:to_list(stream:take(Size, stream:filter(fun gapful:is_gapful/1,
|
||||
stream:naturals(Start))))]).
|
||||
|
||||
main(_) -> lists:map(fun report_range/1, [[1,30],[1000000,15],[1000000000,10]]).
|
||||
Loading…
Add table
Add a link
Reference in a new issue