GotchaNoMore

From J Wiki
Jump to navigation Jump to search

Common troubles solved

"It worked in the interpreter, now my proverb fails"

Goal: find a sentence that computes sum of squares.

   +/ *: 3 4
25

Remove the noun y, assign a name, and test the new proverb.

   sum_of_squares_wrong=: +/ *:

   sum_of_squares_wrong 3 4
12 19
13 20

Explanation: the proverb expands as if parenthesized. Two verbs thus form a hook invoking +/ as a dyadic addition table rather than inserting plus.

   (+/ *:) 3 4  NB. hook forms a table
12 19
13 20

Solutions:

   sum_of_squares=: [: +/ *:  NB. Correct

   sum_of_squares 3 4
25

   ssq=: +/@:*:  NB. correct

   ssq 5 12
169

My ah-hah moment! Write the proverb-to-be in parenthesis from the start. Once correct, it will remain correct.

   ( norm=: [: %: [: +/ *: ) 3 4 12
13

   norm 12 5
13

Notes.

   mp=: $:~ : (+/ .*)  NB. ssq is a special case of matrix product
   norm=: +/&.:*:      NB. clever norm