> I want to create a verb "defverb" such that
> 'myname' defverb '<my sentence>'
> assigns <my sentence> to a new verb 'myname'
A programming comparison on the web had this question:
> The problem: Write a function foo that takes a number n and returns
> a function that takes a number i, and returns n incremented by i.
The concept of embedding context within a function definition is
called a "closure". So, I guess I'm asking is if there is a
"standard" way to accomplish closures in J?
So, here's my solution (using the technique that Mr. Ric Sherlock just
shared):
foo =: 4 : ('(x)=: 3 : ((": y),''+y'')';'$0')
foo
┌
─
┬
─
┬
───────────────────────┐
│4│:│(x)=: 3 : ((": y),'+y')│
│ │ │$0 │
└
─
┴
─
┴
───────────────────────┘
'bar' foo 7
bar
┌─┬─┬───┐
│3│:│7+y│
└─┴─┴───┘
bar 3
10
Is there a "better" way to do this?
Is there a way to define the function result of "foo" as a tacit
function?
Is it necessary to define "foo" as a dyad? Can't a monad return
another monad as a result? (Or, does that make it an adverb?)
Thanks!
--
Alan
Here are some samples of other languages:
> Lisp: Common Lisp (defun foo (n) (lambda (i) (incf n i)))
>
> Mathematica: foo = Module[{s=#},s+=# &] &
>
> Ruby: def foo (n) ; lambda {|i| n += i } ; end
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm