Wednesday, January 30, 2008

Do I think in closures?

R:
> a <- "Yes"
> f <- function() { return(a) }
> a <- "No"
> f()
[1] "No"
Okay. That's not what I was expecting.
Scheme:
> (define a "Yes")
> (define f (lambda () a))
> (define a "No")
> (f)
"No"
Hm. But (in R):
> f <- (function() {a <- "What?"; list(yes = function() a <- "Yes!", huh = function() cat(a, "\n"))})()
> f$huh()
What?
> f$yes()
> f$huh()
What?

I guess I'll have to take a look at the manual to see exactly what <- means.

No comments: