### [racket-users] Re: table lookup shorthands?

```On Monday, August 7, 2017 at 12:16:12 AM UTC+2, Jordan Johnson wrote:
> Hi all,
>
> I’m writing some music-related code for which I find myself using a lot of
> lookup tables, following the pattern of making an alist or hash table and
> then writing a function that consults the table. I wound up writing a macro
> for this pattern; one example of its use is:
>
> ;; key->fifths : PitchName -> Int[-7..7]
> ;; tells how many perfect fifths above C the given pitch is (and
> ;; therefore how many sharps or flats its key signature has)
> (define-lookup-function key->fifths
>   [C 0] [G 1] [D 2] [A 3] [E 4] [B 5]
>   [F# 6] [F♯ 6]
>   [C# 7] [C♯ 7]
>   [F -1]
>   [Bb -2] [Eb -3] [Ab -4] [Db -5] [Gb -6] [Cb -7]
>   [B♭ -2] [E♭ -3] [A♭ -4] [D♭ -5] [G♭ -6] [C♭ -7])
> ;; Ex:
> (key->fifths 'Gb) ;; -> -6
>
> This seems like a simple and common enough pattern that there must be such a
> facility in some Racket library out there already, but I haven’t found one.
> Am I reinventing a wheel here?
>
> Thanks,
> Jordan

Jordan, just to add one more thing just in case.

If you just need an alternative to the lookup table only for this function, you
could also rely on some math. In particular, since the number of semitones of a
perfect fifth (7) (the same holds for the number of semitones of a perfect
fourth if you want to use that fact) and 12 are co-primes I think you could use
what follows to compute the number you are looking for, basically a way to
express in a formula how many jumps of 7 steps in a clock of 12 positions you
need to do to go from point 0 (the C pitch-class) to your destination.

(modulo (* 7 p) 12)

where p stands for the pitch class passed to the function;

and then adapt and extend this as needed to get the kind of output you
mentioned.

--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email

```

### Re: [racket-users] Re: table lookup shorthands?

```Hi, phillip

As far as I'm concerned, professional musician too, I wrote a little app, just
a prototype, using a similar representation of pitch classes and intervals for
basic chord analysis. Since actual chords can be seen as sequences of
intervals, its analysis can be reduced to determine the chord patterns actual
chords match. This leads to at least all possible interpretations. Further
work, some AI for sure, would be needed to select proper interpretations.
Without that, though, a summary of possible matches cuold be as is useful for
students in the first years of Harmony

--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email

```

### Re: [racket-users] Re: table lookup shorthands?

```Others have given good reasons to take alternate approaches, but if I were
writing your define-lookup-function macro, I would avoid using a dictionary
type and have the macro expand to `case`, like this:
(define-syntax define-lookup-function
(syntax-parser
[(_ name:id
[lhs:expr rhs:expr] ...)
#'(define (name arg)
(case arg
[(lhs) rhs] ...
[else (error 'name (format "no matching clause for ~e"
arg))]))]))

For a pre-made solution, I'd look at match-lambda.

I'm interested to see what you're doing with this code! (I'm a musicology
student when I'm not playing with Racket.)

-Philip

On Mon, Aug 7, 2017 at 1:01 PM, Luis Sanjuán
wrote:

> On Monday, August 7, 2017 at 6:39:58 PM UTC+2, Luis Sanjuán wrote:
> > EDIT.
> >
> > In the description of the 'Interval' data type, replace 'notes'  with
> 'pitch-classes'
> >
> > In the signature of the function `interval`, replace 'Nat' with
> 'Interval'
>
> Another one:
>
> In the signature of `fifths-from-to` replace the returned type `Nat` with
> `Integer`
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> For more options, visit https://groups.google.com/d/optout.
>

--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email

```

### [racket-users] Re: table lookup shorthands?

```On Monday, August 7, 2017 at 6:39:58 PM UTC+2, Luis Sanjuán wrote:
> EDIT.
>
> In the description of the 'Interval' data type, replace 'notes'  with
> 'pitch-classes'
>
> In the signature of the function `interval`, replace 'Nat' with 'Interval'

Another one:

In the signature of `fifths-from-to` replace the returned type `Nat` with
`Integer`

--
You received this message because you are subscribed to the Google Groups
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email