Skip to content
kevinlawler edited this page Jun 5, 2011 · 6 revisions

Scan Monad

f:monad

Trace until repeat

f\x

Applies the function f to x collecting the result of each successive application until a result is repeated.

  (1!)\1 2 3 4
(1 2 3 4
 2 3 4 1
 3 4 1 2
 4 1 2 3)
  {(x+2%x)%2}\1    / Newton iteration
(1;1.5;1.416667;1.414216;1.414214;1.414214)

Trace n times

n f\x n:int

Applies the function f to x n times collecting the result of each application.

  10 (2*)\1
1 2 4 8 16 32 64 128 256 512 1024
  5 (|+\)\1 1    / Fibonacci numbers
(1 1
 2 1
 3 2
 5 3
 8 5
 13 8)

Trace while true

b f\x

Applies the function f to x while the expression b is true, the result of each application is collected and assembled into the final result.

  (100>)(3*)\1
1 3 9 27 81 243
  (1<){:[x!2;1+3*x;_ x%2]}\13    / Collatz sequence
13 40 20 10 5 16 8 4 2 1

Scan Dyad

f\x f:dyad

Applies the dyadic function f between the elements of each prefix of x giving a result with #x elements.
The prefixes of x are n#x, for n from 1 to #x: {(1+!#x)#:x}

  +\1 2 3 4 5
1 3 6 10 15
  *\10#2
2 4 8 16 32 64 128 256 512 1024
  ,\1 2 3 4 5
(1
 1 2
 1 2 3
 1 2 3 4
 1 2 3 4 5)

Scan (General)

f\[x0;x1;...;xn]