views:

70

answers:

2
+1  Q: 

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 would be to eventually be able to reproduce my vending machine class and interface which requires at least the void returnChange() method.

+2  A: 

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 return.
)

go ''

EDIT:

kaleidic is correct, return. has no use here. I wonder why I put it in.

go =: 3 : 0
    NB. do stuff
    i. 0
)

go ''
MPelletier
MPelletier is right -- one of the differences between J and APL is that you can't specify nil-adic operators. The only way to replicate the behavior of a void function is to pass an argument that doesn't return anything interesting, and rely on side-effects to accomplish what you want to get done.
estanford
A: 

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.)

kaleidic