From: Fearless Fool Date: 2011-03-23T05:27:13+09:00 Subject: Re: scope of assignments with lambda 7stud -- wrote in post #988636: > How do recursive methods grab you? > ... > You might want to ponder the difference between your example and a > recursive method. @7stud: Pardon if I wasn't clear in my OP, but I was asking about recursion. [For the record, Lisp was one of my first programming languages, and I often find it easier to think in terms of recursion than in iteration. I'd be more impressed if you wrote your factorial routine as a lazy-eval'd stream, ala Abelson & Sussman SICP. ;)] What I was surprised about was the scoping of variables in assignment statements. I guess it makes sense. Assume that p is previously undefined, then a reference on the RHS to p like this: p = p + 1 will raise an error. But wrapped inside a lambda, the reference to p is deferred, so it all works: p = lambda { |n| p.call(n-1) if (n > 0) } As I said, that's the power of a lexical closure. -- Posted via http://www.ruby-forum.com/.