Erlang/OTP Forums

Author Message

<  Erlang  ~  Creating a function that doesn't do anything

wolfgke
Posted: Sun Sep 16, 2007 10:35 am Reply with quote
User Joined: 16 Sep 2007 Posts: 16
Dear people,
I'm trying to learn Erlang at the moment. I got into the following problem:

Let's explain it like this:

The function initProcessesLoop has one parameter: a list of PIDs.

If the lists contains more than one item the (more complicated) function definition is to be used: a special message is sent to the first process in the list.

This definition seems to work.

But when only one item exits: how can I tell Erlang that it should NOT do anything:

Code:
initProcessesLoop([_]);


does not work.

And

Code:
initProcessesLoop([_]) -> 0;


returns 0 (but I don't want the function to return anything).

What is the typical way to do so in Erlang?
View user's profile Send private message
Mazen
Posted: Sun Sep 16, 2007 2:56 pm Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
Hiya,

May I ask why you want it to return nothing? Since function calls are sequential, the only way to "not returning" is to spawn of a process that potentially does something (or nothing).

In other words, as far as I know it is not possible to create a function wich does not return something. Are you handling the return value from this function? If not, then you can just return anything.
View user's profile Send private message
wolfgke
Posted: Mon Sep 17, 2007 7:00 pm Reply with quote
User Joined: 16 Sep 2007 Posts: 16
The reason why I don't want to return anything is simply that no return value is necessary. So: why should I be forced to do so?

I got my program to work by letting the function return something. But I still think this is not an elegant way.

---

For explaination:
The functions gets a list of processes PIDs as parameter and sends each processes (except the last one) a special message which tells them what the next process in the list is.

So the code looks like this:

Code:

initProcessesLoop([_]) -> {ok, initProcessesLoop};

initProcessesLoop([FirstPid, NextPid|PidTail]) ->
   FirstPid ! {init, NextPid},
   initProcessesLoop([NextPid|PidTail]).
View user's profile Send private message
francesco
Posted: Mon Sep 17, 2007 7:39 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Quote:
The reason why I don't want to return anything is simply that no return value is necessary. So: why should I be forced to do so?


Erlang has its origins in Functional Programming. A function has to always return a value, even if it is ignored by the caller of the function. This is fundamental to FP.

What you usually do when you are not interested in the return value is to send back the atom ok.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
wolfgke
Posted: Tue Sep 18, 2007 12:07 pm Reply with quote
User Joined: 16 Sep 2007 Posts: 16
Thanks to everybody for the explanations. Smile
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum