# A R matrix to a Haskell list, with the clipr package

Posted on March 13, 2018 by Stéphane Laurent

Assume you have a matrix in R, and you want to use it in Haskell as a list. Here is a way to go.

I will take a small matrix for the illustration.

M <- rbind(
c(1,2,1.5),
c(0.5,2,3),
c(5,4.3,7)
)
M
##      [,1] [,2] [,3]
## [1,]  1.0  2.0  1.5
## [2,]  0.5  2.0  3.0
## [3,]  5.0  4.3  7.0

[ [1.0,  2.0,  1.5]
,[0.5,  2.0,  3.0]
,[5.0,  4.3,  7.0] ]

It would be very painful to do it by hands. But the clipr package is your friend. First, write the matrix in the clipboard, like this:

library(clipr)
write_clip(M, breaks="],\n", sep=", ")

No we will use cat to write the output to a file.

library(magrittr)
myfile <- "matrix.txt"
cat(sep="\n", file=myfile) %>%
cat("]", sep="", file=myfile, append=TRUE)

And then, here is the content of matrix.txt:

[1, 2, 1.5],
[0.5, 2, 3],
[5, 4.3, 7

]

Well, not totally perfect. But now it’s a child game to complete the output before copying it to Haskell:

[
[1, 2, 1.5],
[0.5, 2, 3],
[5, 4.3, 7]
]

### Update 2018-03-29

I’ve written a function which gives a better result:

library(clipr)
library(magrittr)
write_clip(M, sep=", ")
paste0(c("[", paste0(" [", clipboard))) %>%
cat(sep="\n", file=outfile) %>%
cat("]", sep="", file=outfile, append=TRUE)
}

For example, matrix2list(M) generates the text file

[
[1, 2, 1.5],
[0.5, 2, 3],
[5, 4.3, 7]
]

Nothing to change manually now. Just copy-paste.