Next: , Previous: , Up: k9: Manual

This chapter may be one of the most difficult for users new to array langauges but one must master adverbs to exploit the full power of k9. Nouns and verbs naturally come but adverbs need to be learned so that one can reach for them when needed.

```Adverb
f'  each
[x]f/  over      c/ join
[x]f\  scan      c\ split
[y]f': eachprior
f/: eachright g/:over
f\: eachleft  g\:scan
```

Adverbs modify verbs to operative iteratively over nouns. This previous sentence likely will make sense only once you understand it so let’s jump into an example. Imagine you have a list of 4 lists. Using `first` you will retrieve the first sub-list. In order to retrieve the first element of each sub-list you’ll have to modify the function `first` with a modifier `each`.

``` x:(1 2; 3 4; 5 6; 7 8);x
1 2
3 4
5 6
7 8

*x   / first
1 2

*'x  / each first
1 3 5 7
```

Adverbs are also used where one might use a `for`, `do`, or `while` loop to calculate over a list of value or recursively until a condition is met.

``` f:{x*y}
f\1 .1 .1 .1 / f\ scan over list
1
0.1
0.01
0.001

g:{x*x}
g\:.99       / g\: scan until convergence
0.99 0.9801 0.960596 .. 8.87948e-287 0
```

### 3.1 each ⇒ f’x

Apply function f to each value in list x in a single thread. Multithread operation is done with eachprior.

``` *((1 2 3);4;(5 6);7)   / first element of the list
1 2 3

*'((1 2 3);4;(5 6);7)  / first element of each element
1 4 5 7

!3                     / simple list
0 1 2
+/!3                   / sum of the list
3
2#'!3                  / take 5 copies of the list
0 0
1 1
2 2
+/2#'!3                / sum
3 3
+/'2#'!3               / sum each, eg. (0 1 2)+(0 1 2)
0 2 4
```

### 3.2 over ⇒ f/y or x f/y

Over has the same function as scan but returns only the last value.

### 3.3 scan ⇒ f\y or x f\y

Compute f, a function of two arguments, over list y with optional initial value x. On the first iteration return y0. On each subsequent iteration function will be called with previous output and the next element of y as arguments, i.e. fi=f[fi-1;yi]. Calling with f, a function of one argument, is covered in each.

 i x f[x] a f[x] x:0 1 4 9 16 25 +\x 3+\x +/x 3+/x 0 x0 x0 f0[a;x0] 0 0 3 1 x1 f1[f0;x1] f1[f0;x1] 1 1 4 .. .. .. .. .. .. .. j xj fj[fj-1;xj] fj[fj-1;xj] .. .. .. .. .. .. .. .. .. .. n-2 xn-2 fn-2[fn-3;xn-2] fn-2[fn-3;xn-2] 16 30 33 n-1 xn-1 fn-1[fn-2;xn-1] fn-1[fn-2;xn-1] 25 55 58 55 58

Examples

``` (,\)("a";"b";"c")   / scan and cat to build up a list
a
ab
abc

+\1 20 300          / scan and plus to sum over a list
1 21 321

{y+10*x}\1 20 300   / scan and user func
1 30 600

{y+10*x}/1 20 300   / over and user func
600

x:1 2 3               / x+y0, +y1, +y2
y:10 10 10
x+\y
11 12 13
21 22 23
31 32 33

{x+.5*y}\[1 2 3;20 20 20]  / f\y syntax
11 12 13
21 22 23
31 32 33

1 2 3{x+.5*y}\20 20 20    / i f\x syntax
11 12 13
21 22 23
31 32 33
```

### 3.4 join ⇒ c/x

Join a list of strings x using character c between each pair.

``` "-"/("Some";"random text";"plus";,".")
"Some-random text-plus-."
```

The notaion c/x converts a list of integers x via base c into base 10. In the example below, assuming 2 0 1 are digits in base 8, they convert to 1 + (0*8) + (2*8*8) = 129

``` 8/2 0 1
129
```

### 3.5 split ⇒ c\x

Split the string x using character c to determine the locations.

``` " "\"Here is a short sentence."
Here
is
a
short
sentence.
```

c\x splits a number x into base c digits.

``` 8\129
2 0 1
```

### 3.6 eachprior ⇒ f’:[x;y] or y f’:x

Compute f, a function of two arguments, over list x with optional initial value y. Concretely fi=f[xi;xi-1] where f0 returns f[x0;i].

``` ,':[`i;(`\$"x",'\$!5)]
x0 i
x1 x0
x2 x1
x3 x2
x4 x3

%':[100;100 101.9 105.1 102.3 106.1] / compute returns
1 1.019 1.031403 0.9733587 1.037146

100%':100 101.9 105.1 102.3 106.1    / using infix notation
1 1.019 1.031403 0.9733587 1.037146

101.9{(\$x)," % ",(\$y)," = ",\$x%y}':100.1 101.9 105.1 102.3 106.1
100.1 % 101.9 = 0.9823356
101.9 % 100.1 = 1.017982
105.1 % 101.9 = 1.031403
102.3 % 105.1 = 0.9733587
106.1 % 102.3 = 1.037146
```

Additionally eachp (’:) is a multithreaded (parallel) each. This will allow a function to run using multiple threads over a list of arguments.

```\t {L:x?1.;sum L%sin L*cos L*sin L+L*L}':8#_50e6 / multiple threads
831

\t {L:x?1.;sum L%sin L*cos L*sin L+L*L}'8#_50e6  / single thread
1202
```

### 3.7 eachright ⇒ f/:[x;y]

Apply f[x;y] to each value of y, i.e. the right one.

``` (!2)+/:!3
0 1
1 2
2 3

{x+10*y}/:[!2;!3]
0 1
10 11
20 21

"pre-",/:("apple";"banana";"cherry")
pre-apple
pre-banana
pre-cherry
```

### 3.8 eachleft ⇒ f\:[x;y]

Apply f[x;y] to each value of x, i.e. the left one.

``` (!2)+\:!3
0 1 2
1 2 3

{x+10*y}\:[!2;!3]
0 10 20
1 11 21

("apple";"banana";"cherry"),\:"-post"
apple-post
banana-post
cherry-post
```

### 3.9 Converge Over ⇒ g/:x or (n;g)/:x

Converge Over is the same function as Converge Scan but only returns the final value(s).

``` r:((1;2);3;(,4));r
1 2
3
,4

,//:r  / raze over the list until it converges to a flat list
1 2 3 4
```

### 3.10 Converge Scan ⇒ g\:x or (n;g)\:x

Compute function g where gi=g[gi-1] and g0=x. If n is specified then it is either (DO) the number of iterations or (WHILE) a function returning a non-zero result.

```  {x*x}\:.99           / converge to value
0.99 0.9801 0.9605961 ... 1.323698e-18 1.752177e-36 0.

(3;{x*x})\:.99        / do 3 times, return 4 values (first is initial)
0.99 0.9801 0.960596 0.9227448

(.5<;{x*x})\:.99      / while .5 < r, i.e. r is greater than 0.5
0.99 0.9801 0.960596 0.9227448 0.8514579 0.7249806 0.5255968 0.276252

(4;{x,x+/x+1})\:,0    / do function 4 times
,0
0 1
0 1 3 4
0 1 3 4 12 13 15 16
0 1 3 4 12 13 15 16 72 73 75 76 84 85 87 88

(10>#;{x,x+/x+1})\:,0 / while output is less than 10 long
,0
0 1
0 1 3 4
0 1 3 4 12 13 15 16
0 1 3 4 12 13 15 16 72 73 75 76 84 85 87 88

x:3 4 2 1 4 99        / until output is x
x\:0
0 3 1 4

/ the latter example worked out manually
x 0
3
x 3
1
x 1
4
x 4                   / x[4]=4 so will stop
4
```

Next: Noun, Previous: Verbs, Up: k9: Manual