-module(helloworld).
-export([start_fridge/0, fridge/0, store/2, take/2, main/0]).

% Main function to start the fridge and interact with it.
main() ->
    % Start the fridge process.
    FridgePid = start_fridge(),

    % Store some items in the fridge.
    store(FridgePid, apple),
    store(FridgePid, banana),
    store(FridgePid, carrot),

    % Try to take some items.
    take(FridgePid, banana),
    take(FridgePid, apple),

    % Attempt to take an item that is not in the fridge.
    take(FridgePid, orange),

    % End the simulation by sending terminate to the fridge process.
    FridgePid ! terminate.

% Start the fridge process.
start_fridge() ->
    spawn(helloworld, fridge, []).

% Fridge process function.
fridge() ->
    fridge_loop([]).

% Recursive loop to maintain state of the fridge (list of stored items).
fridge_loop(Items) ->
    receive
        {store, Item} ->
            % Add the item to the list of stored items and continue looping.
            fridge_loop([Item | Items]);
        {take, Item} ->
            % Check if the item is in the fridge.
            case lists:member(Item, Items) of
                true ->
                    % Remove the item from the list and continue looping.
                    NewItems = lists:delete(Item, Items),
                    sender ! {taken, Item}, % Confirm item has been taken
                    fridge_loop(NewItems);
                false ->
                    % If the item is not in the fridge, do nothing.
                    sender ! {error, not_found},
                    fridge_loop(Items)
            end;
        terminate ->
            % Terminate the fridge process.
            ok
    end.

% Send a message to store an item in the fridge.
store(FridgePid, Item) ->
    FridgePid ! {store, Item}.

% Send a message to take an item from the fridge.
take(FridgePid, Item) ->
    FridgePid ! {take, Item},
    receive
        {taken, Item} ->
            io:format("Item taken: ~p~n", [Item]);
        {error, not_found} ->
            io:format("Error: Item not found~n")
    end.
 
by

Erlang Online Compiler

Write, Run & Share Erlang code online using OneCompiler's Erlang online compiler for free. It's one of the robust, feature-rich online compilers for Erlang language, running on the latest version 21. Getting started with the OneCompiler's Erlang compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Erlang and start coding.

About Erlang

Erlang is a special niche functional programming language by Ericcson and first appeared in the year 1986. Many Big companies are using Erlang like Whatsapp, Facebook, Amazon, RabbitMQ and many others and ofcourse Ericsson.

Key Features

  • Open source
  • Mini operating system with a number of interesting features
  • Initially designed as phone language but later it's used in variety of applications.
  • Largely parallel and distributed
  • updates can be made with no downtime at all
  • Super clean error handling model
  • Automatic application migration.

Syntax help

Variables

Variables syntax is as follows

variable-name = value

Data types

Data typeUsageDescription
Numericstart() -> io:fwrite("~w",[10+10])Erlang supports both integer and float values.
Atomstart() -> io:fwrite(true)Atoms should start with lower case leters and can contain lowercase and uppercase characters, numbers, _ and @. You can also put atom in single quotes
Booleanstart() -> io:fwrite(10 =< 8)Output will be either true or false based on the values given
Bit Stringstr = <<10,20>>Strings are enclosed in << >> and are used to store untyped memory
Lists[1,2,3]Lists is a compound data type with Variable number of elements.
Tuplest = {apple, 100, {orange,50}}Tuple is a compound data type with fixed number of elements.
Mapmap = #{name => onecompiler, message => Learning}Map is a compound data type with a variable number of key-value pairs

Loops

1. If-Else:

If the condition is true then true-statement will get executed else false-statement.

if
condition ->
   true-statement;
true ->
   false-statement
end.

2. Case:

If the value is equal to any of the values(value1,value2,..Valuen) then corresponding statements will get executed.

case value of
   value1 -> statement1;
   value2 -> statement2;
   valuen -> statementn
end.

Note:

As Erlang is a functional programming language, there are no direct constructs for while, for and other loops. Recursion is the technique followed to implement loops in Erlang.