syntax question: f/ and p f/:

Hi all,Could you please explain how K parses the following expression (k3)l1:7 0 1l2:9 7 9+/‘+(l1; l2)16 7 10The question #1: how does K understand that it should do f/ for each?because ’ is described as f’ - it means only one function on the leftfrom ', but in example it uses two “+/”.The question #2: I easily understand ’ (each), but I am not clean with/: (eachright).If its correct “p f /: l” is it just syntax sugar for “{p f x}'l” ? IfI understand correct, for previous example I should write: +//:+(l1;l2), but it does not work: valence error at the first char.Thank you, Alexander.–

On Nov 4, 2011, at 10:08 AM, Alexander Epifanov wrote:

> Could you please explain how K parses the following expression (k3)
>
> l1:7 0 1
> l2:9 7 9
> +/‘+(l1; l2)
> 16 7 10
>
> The question #1: how does K understand that it should do f/ for each?
> because ’ is described as f’ - it means only one function on the left
> from ', but in example it uses two “+/”.

i can only answer relative to k4, but the same code works

the syntax isn’t really documented anywhere afaik, so this is basically
guesswork

adverbs are ternary (type 103), so conceptually they are “functions” of
three args: x, y, and f

x f a y

1 2,'3 4

(x can be omitted for over, scan, and prior)

i think the associativity of adverbs is one of the exceptions to q’s
right-to-left parsing–they seem to bind to the left, similar to square
brackets

+/'+(l1;l2)

is thus

((+/)')(+(l1;l2))

or as an sexp (conceptually)

(';(/;+);(+:;l1;l2))

(the actual implementation seems to apply a to f, then apply (a f) to (x
y): ((';(/;+));(+:;l1;l2)))

> The question #2: I easily understand ’ (each), but I am not clean with
> /: (eachright).
>
> If its correct “p f /: l” is it just syntax sugar for “{p f x}'l” ? If
> I understand correct, for previous example I should write: +//:+(l1;
> l2), but it does not work: valence error at the first char.

sometimes, not always, i think

hard stuff, i may look into it further later

There are also other examples, I cannot understand.

=A0 (sqrt;{x*x})@:4 9 - ok, it works=A0 (sqrt;{x*x})@4 - does not work,
is it function in (…)? if no, how is it possible to use it in other
examples?=A0 (sqrt;{x*x})@‘4 9 - it ignores 4, only 9
(_sqrt;{x*x})@’,4 - length error

Regards,
On Sat, Nov 5, 2011 at 2:12 AM, Aaron Davies <aaron.davies> wrote=
:
> On Nov 4, 2011, at 10:08 AM, Alexander Epifanov wrote:
>
>> Could you please explain how K parses the following expression (k3)
>>
>> l1:7 0 1
>> l2:9 7 9
>> +/‘+(l1; l2)
>> 16 7 10
>>
>> The question #1: how does K understand that it should do f/ for each?
>> because ’ is described as f’ - it means only one function on the left
>> from ‘, but in example it uses two “+/”.
>
> i can only answer relative to k4, but the same code works
>
> the syntax isn’t really documented anywhere afaik, so this is basically g=
uesswork
>
> adverbs are ternary (type 103), so conceptually they are “functions” of t=
hree args: x, y, and f
>
> x f a y
>
> 1 2,‘3 4
>
> (x can be omitted for over, scan, and prior)
>
> i think the associativity of adverbs is one of the exceptions to q’s righ=
t-to-left parsing–they seem to bind to the left, similar to square bracket=
s
>
> +/’+(l1;l2)
>
> is thus
>
> ((+/)’)(+(l1;l2))
>
> or as an sexp (conceptually)
>
> (‘;(/;+);(+:;l1;l2))
>
> (the actual implementation seems to apply a to f, then apply (a f) to (x =
y): ((’;(/;+));(+:;l1;l2)))
>
>> The question #2: I easily understand ’ (each), but I am not clean with
>> /: (eachright).
>>
>> If its correct “p f /: l” is it just syntax sugar for “{p f x}'l” ? If
>> I understand correct, for previous example I should write: +//:+(l1;
>> l2), but it does not work: valence error at the first char.
>
>
> sometimes, not always, i think
>
> hard stuff, i may look into it further later
>
> –
>

Submitted via Google Groups</aaron.davies>

On Nov 7, 2011, at 5:06 AM, Alexander Epifanov wrote:

> There are also other examples, I cannot understand.
>
> (sqrt;{x*x})@:4 9 - ok, it works (sqrt;{x*x})@4 - does not work,
> is it function in (…)? if no, how is it possible to use it in other
> examples? (sqrt;{x*x})@‘4 9 - it ignores 4, only 9
> (_sqrt;{x*x})@’,4 - length error

i take it from the _ that at least some of that is k3 again, so again,
note my comments apply only to k4/q

“(sqrt;{x*x})@:4 9” means “(sqrt 4 9;{x*x}4 9)” – i.e. “(x;y)@:z” is
“(x z;y z)”

“(sqrt;{x*x})@4” does “work”, for limited definitions of “work” – it
doesn’t throw an error, it just returns “::”, which is normally
invisible. it means “(sqrt;{x*x})[4]”, i.e. return element four of the
list “(sqrt;{x*x})”, and since that’s a general list of count 2,
non-existent elements have a value of (::).

i don’t know what k3 does with “(sqrt;{x*x})@'4 9”, in k4/q it means
“(sqrt 4;{x*x}9)” – i.e. “(a;b)@'(c;d)” is “(a c;b d)”

translating “(_sqrt;{x*x})@',4” to the presumably equivalent k4,
“(sqrt;{x*x})@',4” throws ‘length because the eachboth adverb “x f’y”
requires that if x and y are both vectors or lists, they be of the same
length, and you’re trying to @’ a list of length two and a vector of
length 1