Skip to content

Instantly share code, notes, and snippets.

@Mozk0
Created May 6, 2010 13:54
Show Gist options
  • Save Mozk0/392146 to your computer and use it in GitHub Desktop.
Save Mozk0/392146 to your computer and use it in GitHub Desktop.
rexp.to.sexp <- function(exp){
exp <- substitute(exp)
if(is.call(exp)) {
s <- "("
i <- 0
while (i < length(exp)) {
i <- i + 1
separator <- if( i == 1 ) "" else " "
s <- paste(s,
eval(substitute(rexp.to.sexp(e), list(e = exp[[i]]))),
sep = separator)
}
paste(s, ")", sep = "")
} else if (is.list(exp)) {
eval(substitute(rexp.to.sexp(e), list(e = as.call(Map(as.name, names(exp))))))
} else if (is.character(exp)) {
paste("\"", as.character(exp), "\"", sep="")
} else {
as.character(exp)
}
}
rexp.to.scheme.exp <- function(exp){
exp <- substitute(exp)
if (is.call(exp)) {
if (exp[[1]] == quote(`function`)) {
exp <- exp[-length(exp)]
}
s <- "("
i <- 0
while (i < length(exp)) {
i <- i + 1
separator <- if( i == 1 ) "" else " "
s <- paste(s,
eval(substitute(rexp.to.scheme.exp(e), list(e = exp[[i]]))),
sep = separator)
}
paste(s, ")", sep = "")
} else if (is.list(exp)) {
eval(substitute(rexp.to.sexp(e), list(e = as.call(Map(as.name, names(exp))))))
} else if (is.character(exp)) {
paste("\"", as.character(exp), "\"", sep="")
} else if(exp == quote(`<-`)) {
"define"
} else if (exp == quote(`{`)) {
"begin"
} else if (exp == quote(`(`)) {
"begin"
} else if (exp == quote(`function`)) {
"lambda"
} else if (exp == quote(`[`)) {
"aref"
} else {
as.character(exp)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment