If composers were hackers

  2023-04-01

Introduction

Music is my first true loveComputer games are my zeroeth true love.. The first time I got my hands on an old untuned guitar, I didn’t want to put it down. I found a summer job to buy my first instrument and pay for a teacher, and since then, music has been a constant source of joy in my life. Picking a degree in computer science at a university over a musical college education was a hard choice for me.

I can’t help but look at the world through my musical obsession. There are many deep similarities between music and computing but we won’t explore them in this article. Instead, I will focus on a silly and subjective question: If my favorite composers decided to write some code, which language would they pick?

Johan Sebastian Bach

Johan Sebastian Bach is the most talented and prolific musician in history.

Bach would love the APL programming language:

Representative Bach’s music: Wachet auf, ruft uns die Stimme, a deeply moving and masterfully harmonized choral.
An APL function computing 2n Gray codes for the given number n. This code comes from A History of APL in 50 functions by Roger K.W. Hui. Try it yourself!
{(0∘, ⍪ 1∘,∘⊖)⍣⍵⍉⍪⍬}

Resources

If you want to learn more about J.S. Bach:

If you want to learn more about APL:

Wolfgang Amadeus Mozart

Mozart’s musical genius was so bright and incomprehensible that mysteries and myths still surround his life.

Mozart would prefer the Scheme programming language.

Representative Mozart’s music: Piano Sonata 5 in G-major, featuring a joyful melody and with a simple but elegant arrangement.
Symbolic differentiation in Scheme (example 2.3.2 in SICP).
(define (deriv exp var)
  (cond ((number? exp) 0)
        ((variable? exp)
         (if (same-variable? exp var) 1 0))
        ((sum? exp)
         (make-sum (deriv (addend exp) var)
                   (deriv (augend exp) var)))
        ((product? exp)
         (make-sum
           (make-product (multiplier exp)
                         (deriv (multiplicand exp) var))
           (make-product (deriv (multiplier exp) var)
                         (multiplicand exp))))
        (else
         (error "unknown expression type -- DERIV" exp))))

Resources

If you want to learn more about Mozart:

If you want to learn more about Scheme:

Ludwig van Beethoven

Beethoven is the most influential composer in history. He single-handedly changed the direction of western music and the role of an artist in society.

One of the few languages worthy of Beethoven would be Haskell:

Representative Beethoven’s music: Sonata op. 13 in C minor, also known as the Pathétique Sonata. Note the operatic dramatism, the masterful use of piano’s sonority, and Beethoven’s way of building music landscapes from tiny memorable motifs and rhythms.
An idiomatic implementation of the Knuth-Morris-Pratt algorithm in Haskell. The code comes from the Knuth-Morris-Pratt in Haskell article by Twan van Laarhoven.
data KMP a = KMP { done :: Bool, next :: (a -> KMP a) }

makeTable :: Eq a => [a] -> KMP a
makeTable xs = table
   where table = makeTable' xs (const table)

makeTable' []     failure = KMP True failure
makeTable' (x:xs) failure = KMP False test
   where  test  c = if c == x then success else failure c
          success = makeTable' xs (next (failure x))

isSublistOf :: Eq a => [a] -> [a] -> Bool
isSublistOf as bs = match (makeTable as) bs
   where  match table []     = done table
          match table (b:bs) = done table || match (next table b) bs

Resources

If you want to learn more about Beethoven:

If you want to learn more about Haskell:

Alexander Nikolayevich Scriabin

Scriabin is a relatively obscure (at least outside of Russia) Russian composer. I find his music deeply expressive, emotional, and metaphysical. Also, he had the most stylish mustache ever.

Scriabin would love Scala:

Representative Scriabin’s music: Prelude in C#-minor from op. 11, one of my favorite short piano pieces.
The Scala way of saying that optional values are just an instance of a monoid in the category of endofunctors. This snippet comes from the scalaz library documentation.
implicit val option: Traverse[Option] with MonadPlus[Option] =
  new Traverse[Option] with MonadPlus[Option] {
    def point[A](a: => A) = Some(a)
    def bind[A, B](fa: Option[A])(f: A => Option[B]): Option[B] = fa flatMap f
    override def map[A, B](fa: Option[A])(f: A => B): Option[B] = fa map f
    def traverseImpl[F[_], A, B](fa: Option[A])(f: A => F[B])(implicit F: Applicative[F]) =
      fa map (a => F.map(f(a))(Some(_): Option[B])) getOrElse F.point(None)
    def empty[A]: Option[A] = None
    def plus[A](a: Option[A], b: => Option[A]) = a orElse b
    def foldR[A, B](fa: Option[A], z: B)(f: (A) => (=> B) => B): B = fa match {
      case Some(a) => f(a)(z)
      case None => z
    }
}

Resources

If you want to learn more about Scriabin:

If you want to learn more about Scala:

Closing words

There are many more great composers and excellent programming languages. Matching these is an exercise for the reader.

I encourage you to think more often about things and people you like and match them with others. For example, My Favorite Theorem podcast hosts ask their guests to match their favorite theorems with items and activities in their lives, such as pizza and rock climbing.

Enjoy your favorite things!

Similar articles