I found system verb assert in ~system\main\stdlib.ijs and learned how to use it from Roger's The
Ball Clock Problem below. Here is my final exam paper:
NB. From ~system\main\stdlib.ijs
NB. =========================================================
NB.*assert v assert value is true
NB. assertion failure if 0 e. y
NB. e.g. 'invalid age' assert 0 <: age
assert=: 0 0"_ $ 13!:8^:((0: e. ])`(12"_))
string =: 'domain error: arguments must be >0 and not _ '
f =: (^. [ string"_ assert 0&< *. _&>)"0 :[:
NB. Because of [ the result is ^. y provided the assert succeeds
f 1 2 3
0 0.693147 1.09861
f _
|domain error: arguments must be >0 and not _ : assert
| f _
f 0j1
|domain error: f
| f 0j1
2 f 3 NB. because of :[: verb f does not accept left arguments
|domain error: f
| 2 f 3
f b. 0
0 _ _
Thanks, teachers!
Roger Hui wrote:
http://www.jsoftware.com/jwiki/Essays/The%20Ball%20Clock%20Problem
assert=: 13!:8@(12"_)^:-.
Addition effects can be obtained by composition with [ or ]
----- Original Message -----
From: Henry Rich <[email protected]>
Date: Tuesday, May 26, 2009 7:46
Subject: Re: [Jprogramming] Tacit argument checking
To: Programming forum <[email protected]>
Viktor's way is OK, but I have had performance trouble with `...@.
especially on rank 0.
I would use
^.@:([:^:(errorcondition))"0
or, better, if you don't need the verb to be rank 0,
^.@([:^:(errorcondition))
where you can do everything on arrays
errorcondition =. 0&>: +. _&<: +. 0 ~: 11&o. NB. scalar
errorcondition =. [: +./@, 0&>: +. _&<: +. 0 ~: 11&o.
NB. array
If you want a custom error, use 13!:8 instead of [:
untested.
Henry Rich
Kip Murray wrote:
How do you check arguments for a tacit verb? Say it's
monadic"0 and I
want the argument to be real and positive excluding _
(infinity). If
no, halt with an error message; if yes, process the argument,
say take
the natural logarithm. /Kip Murray
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm