Exploring Scheme implementations

Over the last 6 months, I have been learning Racket in my free time. One of my first posts on this blog laid out my reasons for choosing Racket. The relatively low barrier to entry (e.g., easy installation of Racket and packages, DrRacket IDE, good docs, etc.) allowed me to build momentum with Racket and I was feeling satisfied with my choice of Racket as an alternative to Python.

ASCII progress bar in R and Racket

In a previous post, I used GUI toolkits to make progress bars in R and Racket. However, I usually prefer the ASCII progress bars of the progress package in R. The progress package includes several options for formatting the progress bar. I particularly like the option to display the estimated time remaining. However, for this post, we will stick to the basic progress bar. library(progress) pb <- progress_bar$new(total = 100) for (i in 1:100) { pb$tick() Sys.

Progress bar widget in R and Racket

As an impatient person and an insecure programmer, I typically use progress bars for any code that takes more than a few minutes to run. In R, a progress bar widget is available through the tcltk package. library(tcltk) pb <- tkProgressBar(title = "Progress Bar", label = "0%", max = 100) for(i in 1:100) { Sys.sleep(0.05) setTkProgressBar(pb, value = i, label = paste0(i, "%")) } close(pb) In this code, we initialize a progress bar and assign it to pb.

Reading and writing JSON files in R and Racket

In learning about reading CSV files in Racket, I have started to reconsider whether storing small(ish) datasets in CSV files is the best default behavior.1 My default was set by primarily working in R, where reading and writing CSV files plays a central role in data analysis. When working solely in R, I expect that my old habits will die hard and CSV files will continue to play a prominent role.

Reading CSV files in R and Racket

In a previous post, I wrote about reading and writing data to file while retaining the structure and attributes of the data (i.e., data serialization). However, I more commonly pass data around as text files (usually, CSV files). For this post, I created an example CSV file with a tool for generating test data, which allows for including different data types (e.g., dates, integers, names, phone numbers) in the output file.

A simple microbenchmarking function in Racket

In a previous post, I wrote a function to perform repeated timings of untyped and typed versions of the same Racket functions. #lang racket (require math) (define (time-apply-cpu-old proc lst reps) (define out (for/list ([i (in-range reps)]) (define-values (results cpu-time real-time gc-time) (time-apply proc lst)) cpu-time)) (displayln (string-append "min: " (number->string (apply min out)) " mean: " (number->string (round (mean out))) " max: " (number->string (apply max out)) " function: " (symbol->string (object-name proc))))) time-apply-cpu-old is a wrapper function for time-apply from racket/base that runs time-apply repeatedly and prints the min, mean, and max cpu time.

Vectorized conditional statement in R and Racket

In a previous post, I wrote that Racket’s if is similar to ifelse in R. That’s not quite right. This is a short post to clarify the comparison. The more accurate description is that this Racket code (if (test-expr) true-expr false-expr) is the same as this R code. if (test_expr){ true_expr }else{ false_expr } In contrast, R’s ifelse function is vectorized meaning that the same operation is applied to multiple elements of a vector.

Stochastic population model in R, Rcpp, Racket, and Typed Racket

On my journey to learn Racket, I look for small pieces of R code to try to implement in Racket. A blog post about speeding up population simulations in R with the Rcpp package seemed like a good candidate for implementing in Racket. I was particularly interested in the (superficial?) parallels between R/Rcpp and Racket/Typed Racket. Running a single simulation First, let’s get some of the setup code1 out of the way.

Generating random numbers in R and Racket

R makes it easy to generate random numbers from a wide variety of distributions with a consistent interface. For example, runif, rnorm, and rpois generate random numbers from uniform, normal, and Poisson distributions, respectively. > x = runif(n = 1000, min = 4.6, max = 9.3) > min(x) [1] 4.60374 > max(x) [1] 9.288063 > > y = rnorm(n = 1000, mean = 81, sd = 9) > mean(y) [1] 81.

Data serialization in R and Racket

When programming in R, I generally pass data around by reading and writing text files (typically, CSV files). The ubiquity of CSV files means that many different types of software will open them easily (e.g., Notepad, Excel, TextEdit, etc.). However, if the data structure is not flat or contains other attributes, then writing to CSV requires flattening and/or dropping attributes. The general solution to writing data to a file while retaining structure and attributes is serialization.