[erlang-questions] Newbie question: function include/1 undefined

Donald Steven t6sn7gt@REDACTED
Mon Oct 3 02:51:30 CEST 2016


Richard,

The program is a musical composition.  There are three modules: base, 
orbiter and composer.  The 16 orbiter processes fly elliptical orbits 
and, when they're close, they 'resonate' and output musical material.  
The orbits have to be initialized, so you end up with this kind of sprawl:

main(Args) ->

     io:format("~nbase.erl, version 1.13, 2 October 2016~n,... working 
... "),

     Orbiter1 = spawn(orbiter, start, []),
     Orbiter1 ! {self(), #orbit{xaxis =    780.0, yaxis = 187.0, xorigin 
= 0.0, yorigin = 0.2, eccentricity = 0.7, rotation = 0.0, direction = 
?ROTX, translation = 0.2, speed = 265}},

     Orbiter2 = spawn(orbiter, start, []),
     Orbiter2 ! {self(), #orbit{xaxis =    171.0, yaxis = 112.0, xorigin 
= 0.1, yorigin = 0.3, eccentricity = 0.6, rotation = 0.1, direction = 
?ROTY, translation = 0.3, speed = 111}},

     Orbiter3 = spawn(orbiter, start, []),
     Orbiter3 ! {self(), #orbit{xaxis =    40.0, yaxis = 140.0, xorigin 
= 0.0, yorigin = 0.5, eccentricity = 0.5, rotation = 0.2, direction = 
?ROTZ, translation = 0.4, speed = 63}},

... and so on (up to 16), before getting to more attractive code such as:

====================================================================================================================================

baseloop(         _,           _, _           , _,           _, ?DONE) 
-> ok;
baseloop(SystemTime, OrbiterList, ComposerList, L, ?MAXORBITER, _    ) ->

     proximitytest(SystemTime, OrbiterList, ComposerList, 
lists:reverse(L), 1, ?NOTDONE),
     baseloop(SystemTime, OrbiterList, ComposerList, [], 1, ?NOTDONE);

baseloop(SystemTime, OrbiterList, ComposerList, L,           I, ?NOTDONE) ->

     Orbiter = lists:nth(I, OrbiterList),
     receive
         {Orbiter, {{X, Y, Z}}} -> ok
     end,
     L1 = [{X, Y, Z} | L],

     CurrentTime = os:system_time(?HUNDREDTHS) - SystemTime,
     if CurrentTime >= ?LENGTHOFPIECE -> stopcomposers(ComposerList, 
?NUMTRACKS),
                                         baseloop(SystemTime, 
OrbiterList, ComposerList, L1, I + 1, ?DONE);
                                 true -> baseloop(SystemTime, 
OrbiterList, ComposerList, L1, I + 1, ?NOTDONE)
     end.

====================================================================================================================================

My thinking was that, if I move this all outside main(), I'll have to go 
through contortions to retrieve the pids (I trust that self() is 
available everywhere in the module and doesn't have to be passed 
explicitly from one fun to another).

I apologize if all this seems naive.  I'm was 'brought up' on c and am 
transitioning to erlang, and just an old man having some fun.

Don




On 10/02/2016 08:03 PM, Richard A. O'Keefe wrote:
>
>
> On 3/10/16 12:37 PM, Donald Steven wrote:
>> Thanks Richard.  I had (alas) come to that conclusion.
>>
>> In c (or m4 or the like), I can include blocks of text or code quite
>> freely.
>
> I used to maintain pdm4.
>
>> In this case, primarily as a matter of aesthetics, I wanted to
>> off load some repetitive initializations to a file which could be
>> included at the appropriate point, with a neat % comment on the side to
>> keep my head straight.
>
> Colour me stupid, but I don't see why you can't have
>
>    % biginit.hrl
>
>    biginit(Arguments...) ->
>       big,
>       ugly,
>       block.
>
>
>    % main.erl
>    ...
>    -include('biginit.erl').
>
>    main(...) ->
>       biginit(...),
>       rest of main.
>
> OK, so it's *two* lines instead of one line, but is that a problem?
>
> Failing that, what stops biginit being a single big ugly macro?
> Again, at the point of use there would be two lines, not one.
>
> I'm trying to think of anything I might want to include in the
> body of a function that couldn't be in another function, and failing.
>
>




More information about the erlang-questions mailing list