Boolean Array in Julia

Hi guys,
I’m trying to translate this code into Julia. I was wondering the best way to do this:

BOOLEAN ARRAY PerfectConsonance[0:12];
SIMPLE PROCEDURE PerfInit;
  BEGIN
  PerfectConsonance[Unison]:=TRUE;
  PerfectConsonance[Fifth]:=TRUE;
  PerfectConsonance[Octave]:=TRUE;
  END; REQUIRE PerfInit INITIALIZATION;

Thanks,
Nakul

It will be easier to help you if you can explain what your code is trying to do (and what language it’s written in). It looks like it might be SAIL, a dialect of ALGOL (according to some frantic googling)?

2 Likes

Your area of work is not an expertise of mine. Nonetheless…

module Consonance

export PerfectConsonance,
      Unison, MinorSecond, MajorSecond,
      Fifth, Octave

#=
 Julia uses 1-based indexing, so the first
 index into a vector is `1` and the last
 index in a vector of length 13 is `13`.
 To use named musical intervals' semitone
 counts as indices, add 1 to each count.
=#

const Unison      =  0 + 1
const MinorSecond =  1 + 1
const MajorSecond =  2 + 1
# ...
const Fifth       =  7 + 1
const Octave      = 12 + 1

#=
  const Vec = [ ..values.. ] 
  means the assignment of the name Vec does not
  change.  The specific values Vec contains are
  alterable and we use this to set up a vector
  of 13 Boolean values, preinitialized false,
  and then initialized for use elsewhere.
=#

const PerfectConsonance = falses(13)

#=
  now assign `true` to each that is perfect
  `=` is used when assigning one thing to another
  `.=` is used when assigning one thing to others
=# 
PerfectConsonance[[Unison, Fifth, Octave]] .= true

end # module Consonance

#=
  `using ModuleName` works with externally stored packages
  `using .ModuleName` works with modules written interactively
      and also is used when one module contains another.
=#

using .Consonance

PerfectConsonance[Fifth]
# true

PerfectConsonance[MinorSecond]
# false
1 Like

Thank you very much for the explanation. const in conjunction with the preinitialized falses/trues is a very interesting concept.

It is code for counterpoint music generation. I just discovered that the SAIL code was also ported to C in the same GitHub. metasolfeggio/classics at master · namin/metasolfeggio · GitHub

The equivalent Boolean interpretation in C is yet another way to do it.

int PerfectConsonance[13] =   {1,0,0,0,0,0,0,1,0,0,0,0,1};
int ImperfectConsonance[13] = {0,0,0,1,1,0,0,0,1,1,0,0,0};
int Dissonance[13] =          {0,1,1,0,0,1,1,0,0,0,1,1,0};