erlang; outsmarting compiler with memoization? - erlang

The following is my solution to Project Euler 14, which works (in 18 s):
%Which starting number, under one million, produces the longest Collartz chain?
-module(soln14).
-export([solve/0]).
collatz(L) ->
[H|T] = L,
F = erlang:get({'collatz', H}),
case is_list(F) of
true ->
R = lists:append(F, T);
false ->
if H == 1 ->
R = L;
true ->
if H rem 2 == 0 ->
R = collatz([H div 2 | L]);
true ->
R = collatz([3*H+1 | L])
end
end,
erlang:put({'collatz', lists:last(L)}, R),
R
end.
dosolve(N, Max, MaxN, TheList) ->
if N == 1000000 -> MaxN;
true ->
L = collatz([N]),
M = length(L),
if M > Max -> dosolve(N+1, M, N, L);
true ->
dosolve(N+1, Max, MaxN, TheList)
end
end.
solve() ->
{Megass, Ss, Micros} = erlang:timestamp(),
S = dosolve(1, -1, 1, []),
{Megase, Se, Microe} = erlang:timestamp(),
{Megase-Megass, Se-Ss, Microe-Micros, S}.
However, the compiler complains:
8> c(soln14).
soln14.erl:20: Warning: variable 'R' is unused
{ok,soln14}
9> soln14:solve().
{0,18,-386776,837799}
Is this a compiler scoping error, or do I have a legit bug?

It's not a compiler error, just a warning that in the true case of "case is_list(F) of", the bindning of R to the result of lists:append() is pointless, since this value of R will not be used after that point, just returned immediately. I'll leave it to you to figure out if that's a bug or not. It may be that you are fooled by your indentation. The lines "erlang:put(...)," and "R" are both still within the "false" case of "case is_list(F) of", and should be deeper indented to reflect this.

The error message and the code are not "synchronized". with the version you give, the warning is on line 10: R = lists:append(F, T);.
What it means is that you bind the result of the lists:append/2 call to R and that you don't use it later in the true statement.
this is not the case in the false statement since you use R in the function erlang:put/2.
You could write the code this way:
%Which starting number, under one million, produces the longest Collartz chain?
-module(soln14).
-export([solve/0,dosolve/4]).
collatz(L) ->
[H|T] = L,
F = erlang:get({'collatz', H}),
case is_list(F) of
true ->
lists:append(F, T);
false ->
R = if H == 1 ->
L;
true ->
if H rem 2 == 0 ->
collatz([H div 2 | L]);
true ->
collatz([3*H+1 | L])
end
end,
erlang:put({'collatz', lists:last(L)}, R),
R
end.
dosolve(N, Max, MaxN, TheList) ->
if N == 1000000 -> MaxN;
true ->
L = collatz([N]),
M = length(L),
if M > Max -> dosolve(N+1, M, N, L);
true ->
dosolve(N+1, Max, MaxN, TheList)
end
end.
solve() ->
timer:tc(?MODULE,dosolve,[1, -1, 1, []]).
Warning the code uses a huge amount of memory, collatz is not tail recursive, and it seems that there is some garbage collecting witch is not done.

Related

How to convert this piece of elixir code to Erlang?

There is a piece of code in elixir that checks if node is alive or not and makes some operations on the basis of that. How to implement the same functionality in erlang?
def make_distributed([head | tail],l) do
unless Node.alive?() do
try do
{ip_tuple,_,_} = head
current_ip = to_string(:inet_parse.ntoa(ip_tuple))
if current_ip === "127.0.0.1" do
if l > 1 do
make_distributed(tail,l-1)
else
IO.puts "Could not make current node distributed."
end
else
server_node_name = String.to_atom("client#" <> current_ip)
Node.start(server_node_name)
Node.set_cookie(server_node_name,:monster)
Node.connect(String.to_atom("server#" <> current_ip))
end
rescue
_ -> if l > 1, do: make_distributed(tail,l-1), else: IO.puts "Could not make current node distributed."
end
end
end
if...else works differently in erlang, and I tried changing it to that format but some of the functonalities that are there in elixir code are hard to show in erlang.
First, a direct translation…
make_distributed([Head | Tail], L) ->
case node:is_alive() of
false ->
false; % Not sure what unless returns if the condition is false
true ->
try
{IPTuple, _, _} = Head,
CurrentIP = iolist_to_binary(inet_parse:ntoa(IPTuple)),
case CurrentIP of
<<"127.0.0.1">> ->
case L of
L when L > 1 ->
make_distributed(Tail, L - 1);
_ ->
io:format("Could not make current node distributed.")
end;
_ ->
ServerNodeName = binary_to_atom(<<"client#", CurrentIP/binary>>),
net_kernel:start([ServerNodeName, longnames, 15000]),
erlang:set_cookie(ServerNodeName, monster),
net_kernel:connect_node(binary_to_atom(<<"server#", CurrentIP/binary>>))
end
catch
_:_ ->
case L of
L when L > 1 ->
make_distributed(Tail, L - 1);
_ ->
io:format("Could not make current node distributed.")
end
end
end.
But that's some very ugly code. Let's use pattern-matching more effectively…
make_distributed([{IPTuple, _, _} | Tail], L) ->
case node:is_alive() of
false ->
false; % Not sure what unless returns if the condition is false
true ->
try
case iolist_to_binary(inet_parse:ntoa(IPTuple)) of
<<"127.0.0.1">> when L > 1 ->
make_distributed(Tail, L - 1);
<<"127.0.0.1">> ->
io:format("Could not make current node distributed.");
CurrentIP ->
ServerNodeName = binary_to_atom(<<"client#", CurrentIP/binary>>),
net_kernel:start([ServerNodeName, longnames, 15000]),
erlang:set_cookie(ServerNodeName, monster),
net_kernel:connect_node(binary_to_atom(<<"server#", CurrentIP/binary>>))
end
catch
_:_ when L > 1 ->
make_distributed(Tail, L - 1);
_:_ ->
io:format("Could not make current node distributed.")
end
end.
Still… we don't need to check for node liveness on every recursive step, right?
maybe_make_distributed(IPTuples, L) ->
case node:is_alive() of
false ->
false; % Not sure what unless returns if the condition is false
true ->
make_distributed(IPTuples, L)
end.
make_distributed([{IPTuple, _, _} | Tail], L) ->
try
case iolist_to_binary(inet_parse:ntoa(IPTuple)) of
<<"127.0.0.1">> when L > 1 ->
make_distributed(Tail, L - 1);
<<"127.0.0.1">> ->
io:format("Could not make current node distributed.");
CurrentIP ->
ServerNodeName = binary_to_atom(<<"client#", CurrentIP/binary>>),
net_kernel:start([ServerNodeName, longnames, 15000]),
erlang:set_cookie(ServerNodeName, monster),
net_kernel:connect_node(binary_to_atom(<<"server#", CurrentIP/binary>>))
end
catch
_:_ when L > 1 ->
make_distributed(Tail, L - 1);
_:_ ->
io:format("Could not make current node distributed.")
end.
Finally, let's move the L > 1 check to its own clause like a regular recursive function…
maybe_make_distributed(IPTuples, L) ->
case node:is_alive() of
false ->
false; % Not sure what unless returns if the condition is false
true ->
make_distributed(IPTuples, L)
end.
make_distributed(_, 0) ->
io:format("Could not make current node distributed.");
make_distributed([{IPTuple, _, _} | Tail], L) ->
try
case iolist_to_binary(inet_parse:ntoa(IPTuple)) of
<<"127.0.0.1">> ->
make_distributed(Tail, L - 1);
CurrentIP ->
ServerNodeName = binary_to_atom(<<"client#", CurrentIP/binary>>),
net_kernel:start([ServerNodeName, longnames, 15000]),
erlang:set_cookie(ServerNodeName, monster),
net_kernel:connect_node(binary_to_atom(<<"server#", CurrentIP/binary>>))
end
catch
_:_ ->
make_distributed(Tail, L - 1)
end.
Now that's kind of the code I would write in Erlang to achieve the same goal as your Elixir code.

Erlang sumif function

I'm trying to make a sumif function in Erlang that would return a sum of all elements in a list if the predicate function evaluates to true. Here is what I have:
sumif(_, []) -> undefined;
sumif(Fun, [H|T]) -> case Fun(H) of
true -> H + sumif(Fun, T);
false -> sumif(Fun, T)
end.
I also implemented my own pos function which returns true if a number is greater than 0 and false otherwise:
pos(A) -> A > 0.
I tried using pos with sumif but I'm getting this error:
exception error: bad function pos
Why is this happening? Is it because of my sumif function or pos? I have tested pos on its own and it seems to work just fine.
Edit: It might be because how I'm calling the function. This is how I'm currently calling it: hi:sumif(pos,[-1,1,2,-3]). Where hi is my module name.
Is it because of my sumif function or pos?
It's because of sumif. You should return 0 when an empty list is passed, as it'll be called from the 2nd clause when T is []:
-module(a).
-compile(export_all).
sumif(_, []) -> 0;
sumif(Fun, [H|T]) -> case Fun(H) of
true -> H + sumif(Fun, T);
false -> sumif(Fun, T)
end.
pos(A) -> A > 0.
Test:
1> c(a).
{ok,a}
2> a:sumif(fun a:pos/1, [-4, -2, 0, 2, 4]).
6
List comprehensions make things far simpler:
sumif(F, L) ->
lists:sum([X || X <- L, F(X)]).
Dobert's answer is of cousrse right, problem is your sum for empty list.
If your concern is performance a little bit you should stick to tail recursive solution (in this case it matter because there is not lists:reverse/1 involved).
sumif(F, L) ->
sumif(F, L, 0).
sumif(F, [], Acc) when is_function(F, 1) -> Acc;
sumif(F, [H|T], Acc) ->
New = case F(H) of
true -> H+Acc;
false -> Acc
end,
sumif(F, T, New).
Ways how to make correct function for first parameter:
F1 = fun pos/1, % inside module where pos/1 defined
F2 = fun xyz:pos/1, % exported function from module xyz (hot code swap works)
N = 0,
F3 = fun(X) -> X > N end, % closure
% test it
true = lists:all(fun(F) -> is_function(F, 1) end, [F1, F2, F3]).
There has tow error in your code:
1. sumif(_, []) -> undefined; should return 0, not undefined.
2. when you pass pos(A) -> A > 0. to sumif/2,you should use fun pos/1, please read http://erlang.org/doc/programming_examples/funs.html#id59138
sumif(F, L) ->
lists:foldl(fun(X, Sum) when F(X) -> Sum+X; (_) -> Sum end, 0, L).
You can use lists:foldl.

How to pass functions as arguments inside modules?

The question I am trying to solve is
Write a split(L) which returns {Even, Odd}.
The available code I have looks like
-export([even/1, odd/1, filter/2, split_using_filter/1]).
even(Integer) -> Integer rem 2 =:= 0.
odd(Integer) -> not even(Integer).
filter(F, L) -> [T || T <- L, F(T) =:= true].
split_using_filter(L) -> Even = filter(fun(X) -> X band 1 == 0 end, L),
Odd = filter(fun(X) -> X band 1 == 1 end, L),
{Even, Odd}.
What I am doing now is passing
fun(X) -> X band 1 == 0 end
as even function and similarily for odd
Question
Is there a way to pass even(Integer) function as argument to filter? rather than re-writing the logic?
Thanks
your code here:
split_using_filter(L) -> Even = filter(fun(X) -> X band 1 == 0 end, L),
Odd = filter(fun(X) -> X band 1 == 1 end, L),
{Even, Odd}.
did you mean you want to do it following???
split_using_filter(L) ->
Even = filter(fun even/1, L),
Odd = filter(fun odd/1, L),
{Even, Odd}.
Even = fun(X) -> X rem 2 =:= 0 end.
Odd = fun(X) -> X rem 2 /= 0 end.
filter(F, L) -> [T || T <- L, F(T) =:= true].
split_using_filter(L) ->
{filter(Even, L), filter(Odd, L)}.
You can read this http://learnyousomeerlang.com/higher-order-functions

Iterate over a cartesian product in Erlang without generating a list first

What's the Erlang equivalent to the following Python code:
for x in range(9):
for y in range(9):
for z in range(9):
foo(x, y, z)
I know I can generate the product first with C = [{X,Y,Z} || X<- lists:seq(1,9), Y<- lists:seq(1,9), Z<- lists:seq(1,9)] then foo([])->done; foo([H|T])->blah blah.
How do I do it without an auxiliary list, using recursion only?
You could do it with three recursive functions.
You might be able to do it with some complex pattern-matching in function head.
But easiest way to skip creation of auxiliary list is to call your function inside list comprehension
C = [foo(X, Y, Z) || X<- lists:seq(1,9),
Y<- lists:seq(1,9),
Z<- lists:seq(1,9)]
Where foo/3 process one element.
List comprehension still forces you to create auxiliary lists in memory.
In case of dealing with huge data sets you should avoid it. Writing recursive functions every time is also awkward so i came up with my own generic for function. It's a little bit slower in traversing than direct recursion or list comprehension but it's memory stable, generic and easy to use.
Usage:
(for({10}))(
fun (X) -> io:format("~p ",[X]) end).
> 1 2 3 4 5 6 7 8 9 10
(for({10, -10, -2}))(
fun (X) -> io:format("~p ",[X]) end).
> 10 8 6 4 2 0 -2 -4 -6 -8 -10
Works with lists too:
(for(lists:seq(10, -10, -2)))(
fun (X) -> io:format("~p ",[X]) end).
> 10 8 6 4 2 0 -2 -4 -6 -8 -10
It's also possible to define step or guard as a function:
(for({256, 1.1, fun (X) -> math:sqrt(X) end, fun (X, Range) -> X > Range end}))(
fun (X) -> io:format("~p ",[X]) end).
> 256 16.0 4.0 2.0 1.4142135623730951 1.189207115002721
If you pass to for a two parameter function, then you can use accumulator feature just like with lists:foldl/3. You also need to pass initial accumulator to for:
Fact = (for(1, {1, 5}))(
fun(X, Acc) ->
X * Acc
end),
io:format("~p", [Fact]).
> 120
e_fact(N) ->
{_, E} = (for({1, 1}, {1, N}))( % i assumed 1/0! equals 1
fun(X, {LastFact, Sum}) ->
Fact = LastFact * X,
{Fact, Sum + 1 / Fact}
end),
E.
io:format("e=~p", [e_fact(10)]).
> e=2.7182818011463845
Also step and guard functions can be dependent on accumulator. Just pass function with one more parameter.
Nested loops finding Pythagorean triples. Easy with closures:
pyth_lists(N) ->
[io:format("~p ", [{A, B, C}]) ||
A <- lists:seq(1, N),
B <- lists:seq(A + 1, N),
C <- lists:seq(B + 1, N),
A * A + B * B == C * C].
pyth_for(N) ->
(for({1, N}))(
fun(A) ->
(for({A + 1, N}))(
fun(B) ->
(for({B + 1, N}))(
fun(C) ->
case A * A + B * B == C * C of
true -> io:format("~p ", [{A, B, C}]);
false -> ok
end
end)
end)
end).
It's too small for external repository. I keep it in my utilities module.
If you find it helpful, here is code:
-export([for/1, for/2]).
for(Through) ->
for([], Through).
for(InitAcc, Opts) when is_tuple(Opts) ->
{Init, Range, Step, Guard} = for_apply_default_opts(Opts),
fun(Fun) ->
UpdFun = if
is_function(Fun, 1) ->
fun(I, _FAcc) -> Fun(I) end;
is_function(Fun, 2) ->
Fun
end,
for_iter(UpdFun, InitAcc, Init, Range, Step, Guard) end;
for(InitAcc, List) when is_list(List) ->
fun(Fun) -> for_list_eval(Fun, InitAcc, List) end.
for_iter(Fun, Acc, I, Range, Step, Guard) ->
case Guard(I, Range, Acc) of
false ->
Acc;
true ->
NewAcc = Fun(I, Acc),
for_iter(Fun, NewAcc, Step(I, NewAcc), Range, Step, Guard)
end.
for_list_eval(Fun, Acc, List) ->
if
is_function(Fun, 1) ->
lists:foreach(Fun, List);
is_function(Fun, 2) ->
lists:foldl(Fun, Acc, List)
end.
for_apply_default_opts({Range}) ->
DefaultInit = 1,
for_apply_default_opts({DefaultInit, Range});
for_apply_default_opts({Init, Range}) ->
DefaultStep = 1,
for_apply_default_opts({Init, Range, DefaultStep});
for_apply_default_opts({Init, Range, Step}) ->
DefaultGuard = case (Step > 0) or is_function(Step) of
true -> fun(I, IterRange, _Acc) -> I =< IterRange end;
false -> fun(I, IterRange, _Acc) -> I >= IterRange end
end,
for_apply_default_opts({Init, Range, Step, DefaultGuard});
for_apply_default_opts({Init, Range, Step, Guard}) when is_function(Guard, 2) ->
for_apply_default_opts({Init, Range, Step, fun(I, IterRange, _Acc) -> Guard(I, IterRange) end});
for_apply_default_opts({Init, Range, Step, DefaultGuard}) when is_number(Step) ->
for_apply_default_opts({Init, Range, fun(I, _Acc) -> I + Step end, DefaultGuard});
for_apply_default_opts({Init, Range, Step, DefaultGuard}) when is_function(Step, 1) ->
for_apply_default_opts({Init, Range, fun(I, _Acc) -> Step(I) end, DefaultGuard});
for_apply_default_opts({_Init, _Range, _Step, _DefaultGuard} = Opts) ->
Opts.

Does F# have loop exit statement?

I know recursive function is a powerful technique in F#. My question is: Is there an exit statement, which can jump out recursive functions, just like imperative languages. For example, Insert a node to a binary tree.
type Tree<'a> when 'a :> IComparable<'a> =
| Nil
| Leaf of 'a
| Node of Tree<'a> * 'a * Tree<'a>
let tt2 = Node(
Node(Leaf "D", "B",Node(Leaf "G", "E", Leaf "H" )),
"A",
Node(Nil, "C", Node(Nil, "F", Leaf "I")))
let rec contains (x : #IComparable<'a>) = function
| Nil -> false
| Leaf y -> if x.CompareTo(y) = 0 then true else false
| Node(l, y, r) ->
match l, y, r with
| l, y, Nil -> if x.CompareTo(y) = 0 then true else contains x l
| Nil,y, r -> if x.CompareTo(y) = 0 then true else contains x r
| _ -> if x.CompareTo(y) = 0 then true
else contains x r |>ignore
contains x l
let xx = contains "C" tt2 //It is wrong answer.
Is there an exit statement, which can jump out recursive functions, just like imperative languages.
No. The very reason is that you can encode imperative break/return by recursive functions and pattern matching. If you would like to break, just return a value, otherwise invoke another recursive call.
This question is more appropriate to ask for high-order functions. When you need early exit on high-order functions, writing custom recursive function is the way to go. If you are interested in imperative constructs in F#, take a look at the excellent series by #Tomas.
Your function will exit at some branch when the condition is determined. The only problem is that you should not discard contain x r in the second to last line.
You can remove superfluous if/else for clarity
let rec contains (x : #IComparable<'a>) = function
| Nil -> false
| Leaf y -> x.CompareTo(y) = 0
| Node(l, y, r) ->
match l, y, r with
| l, y, Nil -> x.CompareTo(y) = 0 || contains x l
| Nil,y, r -> x.CompareTo(y) = 0 || contains x r
| _ -> x.CompareTo(y) = 0 || contains x l || contains x r

Resources