Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6195857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:32:30+00:00 2026-05-24T03:32:30+00:00

I am new to functional programming and just switched from haskell (Didn’t like it

  • 0

I am new to functional programming and just switched from haskell (Didn’t like it much) to erlang (quite fond of it). As I am learning as an autodidact, I stumbled over these Exercises and started doing them.

I came as far as this problem:

  1. Write a function which starts 2 processes, and sends a message M
    times forewards and backwards between them. After the messages have
    been sent the processes should terminate gracefully.

I resolved it like this and it works (maybe it can be done better; any comment highly appreciated):

-module (concur).
-export ( [pingpong/1, pingpong/2] ).

pingpong (Msg, TTL) ->
    A = spawn (concur, pingpong, ["Alice"] ),
    B = spawn (concur, pingpong, ["Bob"] ),
    B ! {A, TTL * 2, Msg}.

pingpong (Name) ->
    receive
        {From, 1, Msg} -> 
            io:format ("~s received ~p and dying.~n", [Name, Msg] ),
            exit (From);
        {From, TTL, Msg} ->
            io:format ("~s received ~p.~n", [Name, Msg] ),
            From ! {self (), TTL - 1, Msg},
            pingpong (Name)
    end.

The real problem is the next exercise:

2) Write a function which starts N processes in a ring, and sends a
message M times around all the processes in the ring. After the
messages have been sent the processes should terminate gracefully.

As I am not sending the message back to its originator, but to the next node in the chain, I somehow have to pass to the sending process the process of the recipient. So I imagined that the function would look something like this:

pingCircle (Name, Next) ->
...
    receive {TTL, Msg} -> Next ! {TTL - 1, Msg}
...

But how do I start this whole thing. When I spawn the first function in the circle, I still haven’t spawned the next node and hence I cannot pass it as an argument. So my naive approach doesn’t work:

First = spawn (concur, pingCirle, ["Alice", Second] ),
Second = spawn (concur, pingCirle, ["Bob", Third] ),
...

Also the approach of passing the spawn call of the next node recursively as a parameter to it predecessor, doesn’t solve the problem how to close the circle, i.e. passing the last node to the first.

The question is:
How can I build this circle?

EDIT:

Thanks to your great answers, I managed to what I intended. Hence this question is solved.

One possible solution is:

-module (concur).
-export ( [pingCircle/3, pingCircle/2] ).

pingCircle (Names, Message, TTL) ->
    Processes = lists:map (fun (Name) -> spawn (?MODULE, pingCircle, [Name, nobody] ) end, Names),
    ProcessPairs = lists:zip (Processes, rot1 (Processes) ),
    lists:map (fun ( {Process, Recipient} ) -> Process ! {setRecipient, Recipient} end, ProcessPairs),
    Circle = lists:map (fun ( {Process, _} ) -> Process end, ProcessPairs),
    hd (Circle) ! {Message, TTL - 1, lists:last (Circle) }.

rot1 ( [] ) -> [];
rot1 ( [Head | Tail] ) -> Tail ++ [Head].

pingCircle (Name, Recipient) ->
    receive
        {setRecipient, NewRecipient} ->
            pingCircle (Name, NewRecipient);
        {Message, 0, Originator} ->
            io:format ("~s received ~p with TTL 0 and dying.~n", [Name, Message] ),
            if
                Originator == self () -> io:format ("All dead.~n");
                true -> Recipient ! {Message, 0, Originator}
            end;
        {Message, TTL, Originator} ->
            io:format ("~s received ~p with TTL ~p.~n", [Name, Message, TTL] ),
            if
                Originator == self () -> Recipient ! {Message, TTL - 1, Originator};
                true -> Recipient ! {Message, TTL, Originator}
            end,
            pingCircle (Name, Recipient)
    end.

Here is my peer review link.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:32:31+00:00Added an answer on May 24, 2026 at 3:32 am

    Spawn them first, then send them a start signal.

    The start signal would be sent after all the processes are already running.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to that functional programming paradigm, but so far I like it.
I am new to functional programming, and now learn Haskell. As an exercise I
Being relatively new to functional programming, I expend lots of energy wondering is this
I'm new to javascript (functional programming is okay for me, though) and I am
I am a newbie to the realm of functional programming and have just started
I am really new to Haskell (Actually I saw Real World Haskell from O'Reilly
I am trying to create a little functional programming library for Java (just to
Im new in OOP programming and C++ and Im currently into learning design patterns.
I've been programming badly for quite a while and I only really just realised.
I'm fairly new the functional programming, so I'm going through some practice exercises. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.