--- title: "A R list to a Haskell list, with the `clipr` package" author: "Stéphane Laurent" date: "2018-03-14" output: md_document: variant: markdown preserve_yaml: true html_document: keep_md: no prettify: yes linenums: yes prettifycss: twitter-bootstrap tags: R, haskell highlighter: kate editor_options: chunk_output_type: console --- ```{r setup, include=FALSE} knitr::opts_chunk$set(collapse = TRUE) ``` In the [previous post](https://laustep.github.io/stlahblog/posts/Rmatrix2HaskellList.html), I have shown how to convert a R matrix to a Haskell list. Now I will show how to convert a R list to a Haskell list. Let's take this list for illustration: ```{r} L <- list(c(1,2,3), c(1,2,3,4), c(1,2)) L ``` So, for Haskell, you want: ```haskell [ [1, 2, 3] ,[1, 2, 3, 4] ,[1, 2] ] ``` Again, the `clipr` package is your friend. First, write the matrix in the clipboard, like this: ```{r} library(clipr) write_clip(L, breaks="],\n", sep=", ") ``` No we will deal with `gsub`, `sub` and finally `cat` to write the output to a file. ```{r} myfile <- "list.txt" cat("[", sub("\\)", "]\n]", gsub("\\)]", "]", gsub("c\\(", "[" ,read_clip()))), sep="\n", file=myfile) ``` And then, here is the content of `list.txt`: ```txt [ [1, 2, 3], [1, 2, 3, 4], [1, 2] ] ``` Now copy-paste to Haskell, it is ready. ### Update 2018-04-18 I've realized there is an issue in the case when some elements of the list are vectors of length one. It is fixed in the following function: ```{r} list2list <- function(L, outfile="list.txt"){ singletons_idxs <- which(lapply(L,length)==1) singletons <- L[singletons_idxs] L <- replace(L, singletons_idxs, sprintf("c(%s)", singletons)) write_clip(L, breaks="],\n", sep=", ") cat("[", sub("\\)", "]\n]", gsub("\\)]", "]", gsub("c\\(", "[" ,read_clip()))), sep="\n", file=outfile) } ``` Let's test it. ```{r} L <- list(c(1,2,3), 0, c(1,2,3,4), c(1,2)) list2list(L) ``` Here is the content of `list.txt`: ```txt [ [1, 2, 3], [0], [1, 2, 3, 4], [1, 2] ] ``` Fine.