JPhrases/Hooks

From J Wiki
Jump to: navigation, search

2F. Hooks

A pair of functions in isolation form a hook whose monadic case is defined as in:

 (= <.) y is equivalent to y = <. y

hence the function h=: =<. compares its argument with its integer part, and therefore provides a test for integers. Hooks occur frequently in most sections.

m0=: It=: =<. Integer test
m1=: Rt=: =+ Real test
d2=: $,: x copies of y
d3=: $, Reshape as in APL
m4=: cf=: (+%)/ Continued fraction
m5=: cfc=: (+%)/\ Continued fraction convergents
m6=: ifb=: # i.@# Integers from boolean list
m7=: [m ([m)y invokes m y , then returns y

For example:

   cf 3 7 15 1		NB. Approximation to pi
3.14159

   cfc 3 7 15 1		NB. Convergents to pi
3 3.14286 3.14151 3.14159	

   cfc 1 1 1 1 1 1 1	NB. Convergents to golden mean
1 2 1.5 1.66667 1.6 1.625 1.61538	

   cfc 10$1x		NB. As above in extended precision
1 2 3r2 5r3 8r5 13r8 21r13 34r21 55r34 89r55