[erlang-questions] A small question about macros

78 views
Skip to first unread message

Martin Hedberg

unread,
May 16, 2013, 12:04:00 AM5/16/13
to erlang-q...@erlang.org


Hi there

Have begun to take my first "baby steps" with Erlang (holding on to books and tutorials).
I read about how to make macros in the language. However I wonder how I do if I want
to have blank spaces in my commands? So I just can write, for example: "repeat 5 times".

Hope you don't see me as a heretic if I want to HyperTalk-ificat Erlang a little bit. :-)

I am very grateful for any help.

Best regards

Martin

Bengt Kleberg

unread,
May 16, 2013, 12:49:11 AM5/16/13
to erlang-q...@erlang.org
Greetings,

What commands do you want to have blank spaces in? Erlang shell
commands? It is not possible to use macros in the shell.


bengt
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Richard A. O'Keefe

unread,
May 16, 2013, 1:40:38 AM5/16/13
to Martin Hedberg, erlang-q...@erlang.org
On 16/05/2013, at 4:04 PM, Martin Hedberg wrote:
> I read about how to make macros in the language. However I wonder how I do if I want
> to have blank spaces in my commands? So I just can write, for example: "repeat 5 times".

What do you mean by "commands"? I'm not sure that Erlang has anything like
that.

Erlang macros were introduced as a quick and dirty solution to some real
problems (giving names to numbers so they can be used in patterns, abstracting
patterns the way functions abstract expressions). They are not meant as
general purpose language extension mechanism.

Nothing other than good taste and good sense will stop you doing

-define(repeat, loop(().
-define(times, ), fun () ->).
-define(end_repeat, end)).

loop(N, F)
when is_integer(N), N > 0 ->
F(),
loop(N-1, F);
loop(0, _) ->
ok.

foo(N) ->
?repeat N ?times
io:fwrite('Going against the grain of a language is not helpful.~n')
?end_repeat.

But please don't do that if you want me to read your code.

> Hope you don't see me as a heretic if I want to HyperTalk-ificat Erlang a little bit.

Erlang is a _practice_, not a religion. I don't see you as a heretic,
more as an isolationist.

Ivan Uemlianin

unread,
May 16, 2013, 3:41:05 AM5/16/13
to erlang-q...@erlang.org
Dear Martin

If by commands you mean function names, you can have spaces in functions
names. In erlang a function name is just an atom. If you want spaces
in an atom you enclose it in single quotes:

MyAtom = 'this atom is fine'.

Example module:

-module('te st').
-export(['do this'/0]).

'do this'() ->
'Ol Korekt!'.

Usage:

1> c('te st').
{ok,'te st'}
2> 'te st':'do this'().
'Ol Korekt!'

:D

Ivan
> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions
>

--
============================================================
Ivan A. Uemlianin PhD
Llaisdy
Speech Technology Research and Development

iv...@llaisdy.com
www.llaisdy.com
llaisdy.wordpress.com
github.com/llaisdy
www.linkedin.com/in/ivanuemlianin

festina lente
============================================================

Martin Hedberg

unread,
May 16, 2013, 11:29:56 AM5/16/13
to erlang-q...@erlang.org


Hi again

Hope I am not spamming the thread, by not posting a question. I just want to say thank you for your help.
It's wonderful to have some people to ask, when you work with a (what I think) difficult language like Erlang.

Hope I can repay you all (with answers) when my knowledge of the language gets a bit better.

Best regards

Martin






From: skribe...@hotmail.com
To: erlang-q...@erlang.org
Subject: A small question about macros
Date: Thu, 16 May 2013 06:04:00 +0200

Martin Hedberg

unread,
May 16, 2013, 4:41:43 PM5/16/13
to erlang-q...@erlang.org


Hello there

Newbie wan Kenobi here again with a small question. :-) When the system restarts atoms are cleared but
I wonder: Is it enough to restart the process that the atoms were declared in, to clear the memory from it?

David Mercer

unread,
May 16, 2013, 5:27:06 PM5/16/13
to Martin Hedberg, erlang-q...@erlang.org

No.  After all, the atoms could have been sent to another process.

 

If I understand it correctly, atoms defined in modules would be allocated when the module is loaded.  All other atoms allocated when first created.  Atoms I don’t think are ever garbage collected.

 

Cheers,

 

DBM

Lee Sylvester

unread,
May 17, 2013, 3:27:27 AM5/17/13
to Martin Hedberg, erlang-q...@erlang.org
I concur with Martin. However, I've not just had great help from the Erlang list, but from Erlang project lists, too.  Its rare to have such a great community. The only other community I've experienced this with is the Haxe community. It makes me think I've chosen the right language :-)

Lee


_______________________________________________

Raimo Niskanen

unread,
May 20, 2013, 3:25:18 AM5/20/13
to erlang-q...@erlang.org
On Thu, May 16, 2013 at 10:41:43PM +0200, Martin Hedberg wrote:
>
>
> Hello there
>
> Newbie wan Kenobi here again with a small question. :-) When the system restarts atoms are cleared but
> I wonder: Is it enough to restart the process that the atoms were declared in, to clear the memory from it?

As others have answered; atoms are never garbage collected.
After system start no atom is ever deleted on that VM.

There have been discussions about atom garbage collect but it
is a tricky problem since you want to keep the properties of
atoms especially size and speed..

Therefore the current recommendation is to not use atoms in excess:

http://www.erlang.org/doc/efficiency_guide/commoncaveats.html#id61516

The number of atoms allowed is supposed to be enough for any number
that will be introduced by loaded code and by communicating with
other node's loaded code, but the number can be adjusted if your
system requires it.

It is when you start creating atoms dynamically you have a
disaster waiting to happen...


>
> Best regards
>
> Martin
>
>
>
>
> From: skribe...@hotmail.com
> To: erlang-q...@erlang.org
> Subject: A small question about macros
> Date: Thu, 16 May 2013 06:04:00 +0200
>
>
>
>
>
>
> Hi there
>
> Have begun to take my first "baby steps" with Erlang (holding on to books and tutorials).
> I read about how to make macros in the language. However I wonder how I do if I want
> to have blank spaces in my commands? So I just can write, for example: "repeat 5 times".
>
> Hope you don't see me as a heretic if I want to HyperTalk-ificat Erlang a little bit. :-)
>
> I am very grateful for any help.
>
> Best regards
>
> Martin
>

> _______________________________________________
> erlang-questions mailing list
> erlang-q...@erlang.org
> http://erlang.org/mailman/listinfo/erlang-questions


--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB

Richard A. O'Keefe

unread,
May 20, 2013, 9:31:17 PM5/20/13
to Raimo Niskanen, erlang-q...@erlang.org
It's really time something was done about Erlang atoms.
I note that SWI Prolog has threads, uses atoms a _lot_
(more than I would recommend) and has atom garbage collection.

Martin Hedberg

unread,
May 20, 2013, 10:35:17 PM5/20/13
to erlang-q...@erlang.org


Hi again

I know that it is a big "no no" to create variables dynamically in Erlang. However I am planning to use Mr Virdings "Luerl"
modules so Lua-engienes (on top of Erlang processes) can handle some of the more simple programming tasks. While the
Erlang processes handle the communication between them.

When the Lua-processes produce variables dynamically I thought it could just pass it via the Erlang layer . However as I have
understood it Erlang is just sending copies. Which makes me a bit worried about the memory.

Is there some way to erase The Lua-content from an Erlang process after it have been sent?

Best regards

Martin

Vance Shipley

unread,
May 20, 2013, 11:33:42 PM5/20/13
to Martin Hedberg, Questions erlang-questions

No, there is a one atom table for the entire emulator.  If you create an atom it is added to the table. In the current implementation the atom table is not garbage collected. Normally atoms are created by loading a module which references them and normally there is a fixed set of modules in use.

Using erlang:list_to_atom/1 is where the trouble starts. You may use erlang:list_to_existing_atom/1 to keep from adding new atoms if you must.  Dynamically loading a large number of modules over time would also be a problem.

Robert Virding

unread,
May 21, 2013, 2:05:47 PM5/21/13
to Martin Hedberg, erlang-q...@erlang.org
Variables in Erlang are no problem. In fact in one sense they don't even exist. An Erlang variable is not like a variable in imperative or OO languages, a "hole" where you can store data, but more just a reference to data which the compiler uses.

Atoms, however, definitely do exist. :-) They are literal constants with a name. And you should definitely avoid dynamically creating atoms in an uncontrolled fashion or you will eventually fill the atom table and crash the system.

With Luerl this is no problem as it never creates or uses atoms. Well, to be honest it uses 3: true, false and nil. All Luerl state is kept in a big data structure (a deep nested structure of tuples, lists, records, dicts and arrays) which is passed in when you "call" Luerl, modified as execution proceeds and finally returned to be used in the next call to Luerl. As this is a normal Erlang data structure so the Erlang GC will cleanup and reclaim unused data. You should, however, call the Luerl GC occasionally, either from Erlang by doing:

NewState = luerl:gc(State),

and then continue with NewState, or from inside Lua with "collectgarbage('collect')".

Each Luerl invocation has its own separate state with no sharing. Note that this state contains everything, all tables, stack data, local variables everything. Whether this is a good or bad thing can be discussed. This means that if you want to send Lua data, for example a table, from one process to another you must first decode into an Erlang representation, send it, then encode into Lua data in the new state. After the data has been sent there is no need to worry about erasing or reclaiming the data in the sending process, the Erlang GC will take care of it. As Luerl state is just normal Erlang data there is no need to erase or reclaim its data as it too will be reclaimed by the Erlang GC when there are no references to it.

It is actually impossible to explicitly erase or reclaim data in Erlang. This is common to most languages which have automatic memory management, like Java or Ruby, or Lua for that matter.

Robert


Thomas Lindgren

unread,
May 21, 2013, 6:02:45 PM5/21/13
to erlang-questions

I think the erlang community has gotten wedged in a needlessly unconstructive state regarding atoms. Not sure why?

Best,
Thomas


From: Robert Virding <robert....@erlang-solutions.com>
Reply all
Reply to author
Forward
0 new messages