Ad

Add, remove and display the items in the fridge. There is some room for improvement here!

Code
Diff
  • Proc = fun(Y, Items) ->
    	receive
        {Sender, Item} when is_pid(Sender) -> 
        	case dict:find(Item, Items) of
           {ok, Value} ->
              NewCount = Value - 1,
            	io:format("One ~p coming right up!~n", [Item]),
           		Sender ! Item,
              Y(Y, dict:store(Item, NewCount, Items));
          	_ -> 
            	io:format("We're all out of ~p~n", [Item]),
              Y(Y, Items)
          end;
          
        {count, Item} ->
            case dict:find(Item, Items) of
              {ok, Value} -> io:format("~p: ~p~n", [Item, Value]);
               _ -> io:format("We're all out of ~p~n", [Item])
            end,
            Y(Y, Items);
            
        {Item, Count} when is_atom(Item) andalso is_integer(Count) -> 
        	io:format("~p added.~n", [Item]),
          NewCount = case dict:find(Item, Items) of
            {ok, Value} -> Value + Count;
          	_ -> Count
          end,
          Y(Y, dict:store(Item, NewCount, Items));
          
        count ->
          io:format("~p~n", [dict:to_list(Items)]),
          Y(Y, Items);
          
        Item when is_atom(Item) -> 
        	io:format("~p added.~n", [Item]),
          NewCount = case dict:find(Item, Items) of
            {ok, Value} -> Value + 1;
          	_ -> 0
          end,
          Y(Y, dict:store(Item, NewCount, Items));
          
        _ -> io:format("Killing Fridge...")
      end
    end,
    
    Pid = spawn(fun() -> Proc(Proc, dict:new()) end),
    
    Pid ! {beer, 6},
    Pid ! {pudding, 8},
    Pid ! salsa,
    Pid ! soda,
    Pid ! eggs,
    Pid ! steak,
    
    Pid ! count,
    Pid ! {count, beer},
    Pid ! {self(), beer},
    Pid ! {count, beer},
    
    init:stop().
    • Proc = fun(Y, Items) ->
    • receive
    • Item when is_atom(Item) ->
    • {Sender, Item} when is_pid(Sender) ->
    • case dict:find(Item, Items) of
    • {ok, Value} ->
    • NewCount = Value - 1,
    • io:format("One ~p coming right up!~n", [Item]),
    • Sender ! Item,
    • Y(Y, dict:store(Item, NewCount, Items));
    • _ ->
    • io:format("We're all out of ~p~n", [Item]),
    • Y(Y, Items)
    • end;
    • {count, Item} ->
    • case dict:find(Item, Items) of
    • {ok, Value} -> io:format("~p: ~p~n", [Item, Value]);
    • _ -> io:format("We're all out of ~p~n", [Item])
    • end,
    • Y(Y, Items);
    • {Item, Count} when is_atom(Item) andalso is_integer(Count) ->
    • io:format("~p added.~n", [Item]),
    • Y(Y, [Item | Items]);
    • NewCount = case dict:find(Item, Items) of
    • {ok, Value} -> Value + Count;
    • _ -> Count
    • end,
    • Y(Y, dict:store(Item, NewCount, Items));
    • count ->
    • io:format("~p~n", [dict:to_list(Items)]),
    • Y(Y, Items);
    • Item when is_atom(Item) ->
    • io:format("~p added.~n", [Item]),
    • NewCount = case dict:find(Item, Items) of
    • {ok, Value} -> Value + 1;
    • _ -> 0
    • end,
    • Y(Y, dict:store(Item, NewCount, Items));
    • _ -> io:format("Killing Fridge...")
    • end
    • end,
    • Pid = spawn(fun() -> Proc(Proc, []) end),
    • Pid = spawn(fun() -> Proc(Proc, dict:new()) end),
    • Pid ! beer,
    • Pid ! {beer, 6},
    • Pid ! {pudding, 8},
    • Pid ! salsa,
    • Pid ! soda,
    • Pid ! eggs,
    • Pid ! steak,
    • Pid ! pudding,
    • Pid ! count,
    • Pid ! {count, beer},
    • Pid ! {self(), beer},
    • Pid ! {count, beer},
    • init:stop().
Code
Diff
  • Proc = fun (Index) -> 
    	timer:sleep(10),
    	io:format("~p~n", [Index])
    end,
    
    [spawn(fun() -> Proc(X) end) || X <- lists:seq(1, 40)],
    
    init:stop().
    • Proc = fun () ->
    • io:format("Hello Process.~n")
    • Proc = fun (Index) ->
    • timer:sleep(10),
    • io:format("~p~n", [Index])
    • end,
    • Pid = spawn(Proc),
    • io:format(" Hello's Pid: ~p~n", [Pid]),
    • [spawn(fun() -> Proc(X) end) || X <- lists:seq(1, 40)],
    • init:stop().
Proc = fun () -> 
	io:format("Hello Process.~n")
end,

Pid = spawn(Proc),

io:format(" Hello's Pid: ~p~n", [Pid]),

init:stop().

Spawning a more complex function as a process.

Proc = fun(Y, Items) ->
	receive
  	Item when is_atom(Item) -> 
    	io:format("~p added.~n", [Item]),
      Y(Y, [Item | Items]);
    _ -> io:format("Killing Fridge...")
  end
end,

Pid = spawn(fun() -> Proc(Proc, []) end),

Pid ! beer,
Pid ! salsa,
Pid ! soda,
Pid ! eggs,
Pid ! steak,
Pid ! pudding,

init:stop().
Recursion
Algorithms
Computability Theory
Logic
Theoretical Computer Science

A Tail recursive factorial function

Code
Diff
  • Factorial = fun(N) when N > 0 ->
    
      Tail = fun (Y, Acc, 0) ->  Acc;
                 (Y, Acc, N) -> Y(Y, Acc * N, N - 1) 
             end,
             
      Tail(Tail, 1, N)
    end,
    
    io:format("~s~n", [Factorial(5)]),
    
    • Factorial = fun (Y, 0) -> 1;
    • (Y, N) -> N * Y(Y, N - 1)
    • end,
    • Factorial = fun(N) when N > 0 ->
    • io:format("~p~n", [Factorial(Factorial, 5)]),
    • Tail = fun (Y, Acc, 0) -> Acc;
    • (Y, Acc, N) -> Y(Y, Acc * N, N - 1)
    • end,
    • Tail(Tail, 1, N)
    • end,
    • init:stop().
    • io:format("~s~n", [Factorial(5)]),
Recursion
Algorithms
Computability Theory
Logic
Theoretical Computer Science

A recursive factorial function.

Factorial = fun (Y, 0) ->  1;
      (Y, N) -> N * Y(Y, N - 1)
  end,

io:format("~p~n", [Factorial(Factorial, 5)]),

init:stop().
io:format("Hello World!~n"),

init:stop().