That sounds like a job for agenda: collatz =: collatz_even`collatz_odd@.(2&|)
the expression to the right of the Agenda (@.) returns 0 or 1 for any scalar integer input; that will be used to select one of the verbs to the left to be executed, On Mon, Mar 3, 2014 at 11:30 AM, Jon Hough <jgho...@outlook.com> wrote: > Beginner question again.I quick task I set myself was to write ONE ITERATION > of the Collatz function for a given positive > integer.http://en.wikipedia.org/wiki/Collatz_conjecture > > So my verb is supposed to do 3*n+1 if n is odd and n/2 if n is even.In a more > imperative/OOE based language (C/C++/Java) I could write this in less than a > minute. Unfortunately, I fell at the very first hurdle in J. > I originally wrote my tacit verb for even ints:collatz_even =.2&(%~) > collatz_even 4 > 2 > This works, but I had a terrible time trying to put the brackets in the right > place. I am not sure why %~ needs to be bracketed. Won't J parse %~ as dyadic > and "know" that the left operand is 2? > Next I tried to do the case for odd n: > collatz_odd =. 1&+@(3&*) collatz_odd 3 > 10 > That seems to work ok.Now I am not sure how to do an if statement in J. In > plain English I want "If n is even do collatz_even else do collatz_odd".The > verb I wrote to test for even-ness is > ones =. {:@#: NB. Finds the ones column values. 1 => odd, 0=> even > 1 = ones 2 > 0 > So I have a test but I am not sure how to utilize this test. How should I go > about doing:"If n is even do collatz_even else do collatz_odd"? > Thanks and regards,Jon > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm -- - michael dykman - mdyk...@gmail.com May the Source be with you. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm