A small question about macros

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

A small question about macros

Martin Hedberg


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
     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130516/b47f0849/attachment.html>

Reply | Threaded
Open this post in threaded view
|

A small question about macros

Bengt Kleberg-4
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

On Thu, 2013-05-16 at 06:04 +0200, Martin Hedberg wrote:

>
>
> 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-questions
> http://erlang.org/mailman/listinfo/erlang-questions


Reply | Threaded
Open this post in threaded view
|

A small question about macros

Richard A. O'Keefe
In reply to this post by Martin Hedberg
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.


Reply | Threaded
Open this post in threaded view
|

A small question about macros

Ivan Uemlianin-2
In reply to this post by Martin Hedberg
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


On 16/05/2013 05:04, Martin Hedberg wrote:

>
>
> 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-questions
> http://erlang.org/mailman/listinfo/erlang-questions
>

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

                     ivan
                      www.llaisdy.com
                          llaisdy.wordpress.com
               github.com/llaisdy
                      www.linkedin.com/in/ivanuemlianin

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

Reply | Threaded
Open this post in threaded view
|

Just want to say "thanks".

Martin Hedberg
In reply to this post by Martin Hedberg


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: skribent_har
To: erlang-questions
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
         
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130516/efabf28b/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Basic atom question

Martin Hedberg


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?

Best regards

Martin




From: skribent_har
To: erlang-questions
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
             
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130516/53287f57/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Basic atom question

David Mercer
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

 

From: erlang-questions-bounces [mailto:erlang-questions-bounces] On Behalf Of Martin Hedberg
Sent: Thursday, May 16, 2013 15:42
To: erlang-questions
Subject: [erlang-questions] Basic atom question

 



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?

Best regards

Martin






  _____  

From: skribent_har
To: erlang-questions
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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130516/40938c07/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Just want to say "thanks".

Lee Sylvester
In reply to this post by Martin Hedberg
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



On 16 May 2013, at 16:29, Martin Hedberg <skribent_har> wrote:

>
>
> 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: skribent_har
> To: erlang-questions
> 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-questions
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130517/4c6d6674/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Basic atom question

Raimo Niskanen-8
In reply to this post by Martin Hedberg
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: skribent_har
> To: erlang-questions
> 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-questions
> http://erlang.org/mailman/listinfo/erlang-questions


--

/ Raimo Niskanen, Erlang/OTP, Ericsson AB

Reply | Threaded
Open this post in threaded view
|

Basic atom question

Richard A. O'Keefe
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.


Reply | Threaded
Open this post in threaded view
|

Luerl and dynamically created lists

Martin Hedberg
In reply to this post by Martin Hedberg


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

     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130521/7760c301/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Basic atom question

Vance Shipley-2
In reply to this post by Martin Hedberg
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.
 On May 17, 2013 2:42 AM, "Martin Hedberg" <skribent_har> 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?
>
> Best regards
>
> Martin
>
>
>
>
> ------------------------------
> From: skribent_har
> To: erlang-questions
> 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-questions
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130521/791895db/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Luerl and dynamically created lists

Robert Virding-5
In reply to this post by Martin Hedberg
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

----- Original Message -----

> From: "Martin Hedberg" <skribent_har>
> To: erlang-questions
> Sent: Tuesday, 21 May, 2013 4:35:17 AM
> Subject: [erlang-questions] Luerl and dynamically created lists

> 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

> _______________________________________________
> erlang-questions mailing list
> erlang-questions
> http://erlang.org/mailman/listinfo/erlang-questions
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130521/2519a265/attachment.html>

Reply | Threaded
Open this post in threaded view
|

Luerl and dynamically created lists

Thomas Lindgren-5


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

Best,
Thomas



>________________________________
> From: Robert Virding <robert.virding>
>
>
>
>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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130521/5e50317e/attachment.html>