R での関数型プログラミングの感触をつかみたいと思います。その趣旨で、いくつかの構造を含む可能性があるので、ヴァンデルモンド行列計算を書きたいと思います。
命令スタイルでは次のようになります。
vandermonde.direct <- function (alpha, n) 
{
  if (!is.vector(alpha))  stop("argument alpha is not a vector")
  if (!is.numeric(alpha)) stop("argument n is not a numeric vector")
  m <- length(alpha)
  V <- matrix(0, nrow = m, ncol = n)
  V[, 1] <- rep(1, m)
  j <- 2
  while (j <= n) {
    V[, j] <- alpha^(j - 1)
    j <- j + 1
  }
  return(V)
}
関数型スタイルで R でエレガントに書くにはどうすればよいでしょうか。
以下は機能しません:
x10 <- runif(10)
n <- 3
Reduce(cbind, aaply(seq_len(n-1),1, function (i) { function (x) {x**i}}), matrix(1,length(x10),1))
関数に移動するError: Results must have one or more dimensions.関数のリストについて教えてくれるようにi in seq_len(3-1)x -> x**i.