Help please with list syntax

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

Help please with list syntax

Donald Steven
Hi all,

I'm sorry to ask such a simple question, but "I've tried everything" and
I'm still getting nowhere.

----------------------------------------------------------------------------------

The intent is simply at create a list 'L' of (MIDI) velocities, of
length 'Events'.  The calling function is:

VelocityL = makeVelocityL(Events, SoftestNote, LoudestNote, []),

which should return something like (if 'Events' were to equal 3): [40,
42, 91]

I'm testing this by following the above line of code with:

io:format("VelocityL ~p~n", [VelocityL]),

but this produces nonsense like: "(*["  (including the double quotes at
either end

----------------------------------------------------------------------------------

The function that is called is:

makeVelocityL(     0,           _,           _, L) -> lists:reverse(L);
makeVelocityL(Events, SoftestNote, LoudestNote, L) ->

     io:format("(for testing purposes) Events: ~p, SoftestNote: ~p,
LoudestNote: ~p, L: ~p~n", [Events, SoftestNote, LoudestNote, L]),
     Velocity = trunc(rand:uniform() * (LoudestNote - SoftestNote)) +
SoftestNote,
     io:format("(For testing purposes) Velocity: ~p~n", [Velocity]),
     makeVelocityL(Events - 1, SoftestNote, LoudestNote, [Velocity | L]).

and the output of the io:format statements is:

(for testing purposes) Events: 3, SoftestNote: 36, LoudestNote: 96, L: []
(For testing purposes) Velocity: 40
(for testing purposes) Events: 2, SoftestNote: 36, LoudestNote: 96, L: "("
(For testing purposes) Velocity: 42
(for testing purposes) Events: 1, SoftestNote: 36, LoudestNote: 96, L: "*("
(For testing purposes) Velocity: 91

so clearly the values are OK but 'L' is nonsense.  So, the problem must
be in the recursion statement:

     makeVelocityL(Events - 1, SoftestNote, LoudestNote, [Velocity | L]).

but I've tried every combination and order of | and ++ to no avail.

----------------------------------------------------------------------------------

Your help would be greatly appreciated.

Thanks.

Don

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Help please with list syntax

Stanislaw Klekot
On Fri, Oct 14, 2016 at 01:48:49PM -0400, Donald Steven wrote:

> The intent is simply at create a list 'L' of (MIDI) velocities, of
> length 'Events'.  The calling function is:
>
> VelocityL = makeVelocityL(Events, SoftestNote, LoudestNote, []),
>
> which should return something like (if 'Events' were to equal 3):
> [40, 42, 91]
>
> I'm testing this by following the above line of code with:
>
> io:format("VelocityL ~p~n", [VelocityL]),
>
> but this produces nonsense like: "(*["  (including the double quotes
> at either end

~p format tries to write strings as strings, and since string is just
a list of small integers, it matches what you have there. In fact, 40
stands for '(', 42 is '*', and 91 is '['. You can check yourself:

#v+
1> [40, 42, 91] == "(*[".
true
#v-

What you want probably is to use ~w format, since it prints lists of
integers as just lists of integers.

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions