Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

erlang
communitycreator

Pattern matching in Erlang

harris910

Overview

Erlang like many other functional languages includes a robust pattern matching system. A pattern(left side) is usually matched against a phrase (right side).

Code

Following are the instances where we can use pattern matching:

While determining the value of a function

-module(main).
-export([main/0]).

promote({car, sedan}) -> "This car is sedan";
promote({car, hatchback}) -> "This car is hatchback";
promote({car, suv}) -> "This car is suv";
promote({car, _}) -> "This car does not belong to any category".

main() ->
    io:format("While determining the value of a function~n", []),
    io:format("~p~n", [promote({car, suv})]),
    io:format("~p~n", [promote({car, sedan})]),
    io:format("~p~n", [promote({car, hatchback})]),
    io:format("~p~n", [promote({car, sports})]).

Explanation

  • Lines 4–7: We define different functions that return different values based on the parameters sent to them.

  • Lines 9–14: This is the main function that tests the functions above by sending different arguments.

In a case-of block

-module(main).
-export([main/0]).

determine_car(Arg) when is_tuple(Arg) ->
    case Arg of
        {car, sedan} -> "This car is sedan";
        {car, hatchback} -> "This car is hatchback";
        {car, suv} -> "This car is suv";
        {car, _} -> "This car does not belong to any category";
        _ -> "This is not a car"
    end.
    
main() ->
    io:format("In a case-of block~n", []),
    io:format("~p~n", [determine_car({car, sedan})]),
    io:format("~p~n", [determine_car({car, sports})]),
    io:format("~p~n", [determine_car({bike})]).

Explanation

  • Lines 4–11: This function includes a case statement tackling different types of input parameters.

  • Lines 13–17: This is the main function which calls the determine_car function with different arguments.

In the form of a try expression

-module(main).
-export([main/0]).

is_multipleOf3(Number) ->
    Type = try Number rem 3 of
        0 when is_number(Number) -> true;
        1 when is_number(Number) -> false;
        2 when is_number(Number) -> false
    catch
        _ErrType:_Err -> "Its not a number"
    end,
    io:format("Is ~p multiple of 3? ~p~n", [Number, Type]).

main() ->
    io:format("In the form of a try expression~n", []),
    is_multipleOf3(6),
    is_multipleOf3(8),
    is_multipleOf3(b).

Explanation

  • Lines 4–12: This function includes a try statement.

  • Lines 14–18: The main function which calls the is_multipleOf3 function with different arguments.

Capturing the incoming messages

-module(main).
-export([main/0]).
-compile([export_all]).

postman_service() ->
    receive
        {send, {From, To, _Content} = Email} ->
            io:format("~n~s Sending Email to ~s : ~s~n~n", [From, To, _Content]),
            self() ! {rcv, Email},
            postman_service();
        {rcv, {To, From, Content} = _Email} ->
            io:format("~s got an Email: ~n~s ~nRegards, ~n~s~n", [From, Content, To]),
            postman_service();
        stop ->
            io:format("~nClosing the Postman connection ~n")
    end.

main() ->
    io:format("When capturing the incoming messages to a process in a receive block~n", []),
    ProcessID = spawn(?MODULE, postman_service, []),
    ProcessID ! {send, {"Jerry", "Tom", "Hello, i hope you are doing good."}},
    timer:sleep(100),
    ProcessID ! stop.

Explanation

  • Lines 5–16: We use the receive statement to send and receive an email.

  • Lines 18–23: The main function that initiates the process of postman_service and then uses ProcessID to send a function call in order to send an email from Jerry to Tom.

When using the = operator

-module(main).
-export([main/0]).

headOfList(List) ->
    [Head | _] = List,
    io:format("Head of the list is: ~p~n", [Head]).

main() ->
io:format("When using the = operator ~n", []),
headOfList([100, 200, 300]).

Explanation

  • Lines 4–6: We use this function, which includes a = operator, in order to send the head of the list.

  • Lines 8–10: We call the headOfList function.

RELATED TAGS

erlang
communitycreator
RELATED COURSES

View all Courses

Keep Exploring