Void Verbs in J
I'm learning how to use J via online reading and doing some old Java assignments over again using this language, and would like to know how to make a verb that doesn't take any operands, or return any result. The reason being: I would like to allow myself the ability to type in a verb, let's call it go
that would run a sequence of code on it's own and save whatever data it would produce in its execution, but would display nothing at all. The overall goal of this wo开发者_StackOverflow社区uld be to eventually be able to reproduce my vending machine class and interface which requires at least the void returnChange()
method.
Calling a J verb is always done with at least a right argument. Send anything to a monadic verb that ignores the right argument altogether (say 0
or ''
).
Functions always return something, but using i.0
or ''
minimises the data returned.
go =: 3 : 0
NB. do stuff
i. 0
)
go ''
MPelletier is correct that J verbs always require a right argument to produce a result, and that in executing they necessarily produce a result. The situation is similar for adverbs and conjunctions. Nothing in J is similar to a method that "returns void."
The example provided by MPelletier uses the keyword 'return.' In that context the keyword has no effect. A modified version of that program is offered here:
go =: 3 : 0
NB. do stuff
i. 0 0
)
A visible difference between this program and the one posted by MPelletier is that, if executed in the console, it does not produce a blank line before the next prompt. (Any result with zero in position _2 from its shape has this effect.)
精彩评论