# [erlang-questions] help with message-passing syntax

Richard A. O'Keefe <>
Fri Jul 4 04:15:53 CEST 2008

```On 3 Jul 2008, at 9:04 pm, not norwegian swede wrote:
> squarer(X) ->
>     Pattern [when Pattern > 7] ->
>         [X*X || X <- [seq(1, 7]];
>     end

Why the square brackets around the guard?

You are receiving Pattern, and checking it,
but then you do nothing with it!
Pattern is a bad name because it tells us nothing
whatever about the kind of messages you are expecting

You have an argument X, and then you appear to be
trying to bind a *different* X in the list comprehension.

[X*X || X <- [seq(1, 7)]]

has an extra pair of square brackets, for no apparent
reason.  The list [seq(1, 7)]] has only one element,
so X will be bound (just once) to [1,2,3,4,5,6,7].
However, [1,2,3,4,5,6,7]*[1,2,3,4,5,6,7] is not understood.
Presumably you meant

[X*X || X <- seq(1, 7)]

However, having computed the list [1,4,9,...,49], what do
you want to do with it?

I would expect a squaring process to be something like

squarer(Destination) ->
Destination ! (Number * Number),
squarer(Destination)
end.

> squarer2(X) ->
>     when X > 7 -> [X*X || X <- [seq(1, 7]];
>     end

This has two of the same defects as the previous version:
X used as argument and as list element pattern,
extra brackets around seq(1, 7).
saying _what_ you want to receive or letting you remember
what it is.
>
>
> squarer(X) ! 2+6

This will call the *FUNCTION* squarer, in the expectation
that it will return a process ID.  The function squarer()
will enter the 'receive', and wait forever for a message.

I think you mean something like this:

destination() ->
stop ->
ok
; Number ->
io:write(Number), io:nl(),
destination()
end.

squarer(Destination) ->
stop ->
Destination ! stop
; Number ->
Destination ! (Number * Number),
squarer(Destination)
end.

sender(Squarer, Numbers) ->
[Squarer ! Number || Number <- Numbers],
Squarer ! stop.

example() ->
Destination = spawn(fun () -> destination() end),
Squarer = spawn(fun () -> squarer(Destination) end),
sender(Squarer, seq(1, 7)).

(Tested.)
>

```