From: Joel VanderWerf Date: 2002-12-06T08:31:33+09:00 Subject: Re: [ANN] ratlast 0.1 -- embedded FORTH in Ruby Mark Probert wrote: > Hi, Harry. > > Harry Ohlsen wrote: > >>On Fri, 6 Dec 2002 07:57, Mark Probert wrote: >> >> >>>Most of all, it is a -lot- of fun to use! >>> >>>$ cat ex1.rb >>>require 'Atlast' >>>t = Atlast.new >>> >>>t.eval(": linear ( a b -- ) create swap , , does> dup >r @ * r> 4 + @ >>>+ ;") t.eval("3 17 linear aline") >> >>Ah, yes, I can see how that's so much more intuitive than ... >> >> def linear(x) >> 3 * x + 17 >> end >> > > hmmmmm. > > Not quite. The colon definition of linear is more like a lambda > function in lisp, it defines a series of functions, for "a*x + b". > The binding of a and b happens in the "linear aline" part. I think > you can do this in Ruby using closures. But closures can do something quite different--they can share a variable binding with the enclosing scope: a = 1 b = 2 linear = proc { proc {|x|a*x+b} } l1 = linear[] p l1[5] # ==> 7 a = 10 p l1[5] # ==> 52 If you want linear to behave more like the Forth example: linear2 = proc { |aa,bb| proc {|x|aa*x+bb} } l2 = linear2[1,2] p l2[5] # ==> 7 aa = 10 p l2[5] # ==> 7