This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' R implementation of `cond` from Lisp | |
#' allows for arbitrary numbers of conditionals without ugly nested if statements | |
#' conditionals are entered as pairs of expressions (clauses), | |
#' first the expression to be evaluated and second the return value if the expression is true | |
#' @param ... an even number of expressions as pairs (see example) | |
#' @param true the `else` expression (Taken from the Lisp (T resultN) see http://www.cis.upenn.edu/~matuszek/LispText/lisp-cond.html) | |
#' @return The paired value of the first true conditional expression or the value of true | |
#' @examples | |
#' x <- runif(1) | |
#' cond(x < 0.2, "lower tail", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Scheme Interpreter in R | |
## (more R-ish implementation of "lisp.R") | |
addGlobals <- function(env) { | |
procs <- list("+" = sum, | |
"*" = prod, | |
"-" = function(...) Reduce(`-`, list(...)), | |
"/" = function(...) Reduce(`/`, list(...)), | |
"=" = `==`, | |
"eq?" = `==`, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun foldr (f lst) | |
(if (null (cdr lst)) (car lst) | |
(funcall f (car lst) (foldr f (cdr lst))))) | |
(defun foldl (f lst) | |
(if (null (cdr lst)) (car lst) | |
(funcall f (foldl f (butlast lst)) (car (last lst))))) | |
(defun foldr-tail (f a lst) | |
(if (null lst) a |