2 min read

R and Haskell, meant for each other?

R has a notoriously steep learning curve. I once moaned that help() needed it’s own help(help), which it does, but did nothing to help me understand the paradigm of function signatures. And why were control loops like for so frowned upon. All sorts of great functionality, but how do you, like program it.

As I progressed in my ability to overcome my underlying lack of grok, I took a dip in Haskell, and discovered the concept of a functional language, essentially everything is \(f(x) = y\) as opposed to an imperative/procedural language – first do this, then do that, then perform some operation on both of them, and then, and then … .

A lonely neuron potentiated its axon and discharged a signal that tapped into several related networks that did their hidden layer human intelligence thing and agreed: R is a functional language, like Haskell.

Suddenly I learned to take R function signatures much, much more seriously, along with linear alegbra type operations as the efficient substitute for control loops.

R being an interpreted language is sometimes called upon to perform calculations that push its available memory and speed past the point of usefulness. For that, we have the ability to plug in compiled modules, to expose their functionality to the interpreter. See rccp

Another lonely neuron fired today. In scaling many R scripts to large-production scale, at least outside of the RShiny Server gui context, the script, its packages, dependences, functions, transformations and logic have to be reverse engineered into C++ or another compiled language. That involves the trailing sea anchor of translating a functional language into an imperative language. And, if you’re conscientious, building and maintaining libraries for the frequently used algorithms so you don’t have to start from scratch everytime, documenting them in native fashion while also being R friendly to enable the original developers to follow along.

Why not do this in Haskell a compiled functional language? Write an “Rh” package similar to Rcpp and give back to the R author as a hook to a faster implementation of standard R? In time, you would have a domain specific dialect of R that was essentially compiled. Haskell is also said (above my pay grade) to have features of Curry-Howard correspondence, a sort of self-proving program paradigm with the potential, as I understand it, to minimize the possibilities of logical errors and the stated aim of precluding side effects.

#rstats #haskell