Another K puzzlement for a newbie

120 views
Skip to first unread message

Matt Crawford

unread,
Apr 7, 2018, 6:23:22 PM4/7/18
to Kona Users
I am looking at "99 K problems" and something is strange right from the start.

  last: *|:

  last "abcdefg"

"g"

  last 0 1 2 3 4

4

  last !5

type error

last !5

     ^

>  


Whatever the problem is, it seems to violate the right-to-left ordering.

After building, the result of ./k_test ended with

Test pass rate: 1.0000, Total: 1097, Passed: 1065, Skipped: 32, Failed: 0, Time: 0.360482s


Which seems to be all in order, yes?

Matt Crawford

unread,
Apr 7, 2018, 6:27:45 PM4/7/18
to Kona Users
Just as strangely (to me), this works fine:

  last 0+!10

9

Bakul Shah

unread,
Apr 7, 2018, 8:08:06 PM4/7/18
to kona...@googlegroups.com
On Sat, 07 Apr 2018 15:23:22 -0700 Matt Crawford <matt...@gmail.com> wrote:
>
> I am looking at "99 K problems" and something is strange right from the
> start.
>
> last: *|:
>
> last "abcdefg"
>
> "g"
>
> last 0 1 2 3 4
>
> 4
>
> last !5
>
> type error
>
> last !5
>
> ^

This has to do with k's grammar rules. Roughly, the grammar
is as follows.

E: E;e | e
e: nve | te
t: n | v
v: tA | V
n: t[E] | (E) | [E] | {E} | N

Where
A is a language token that is an adverb,
N is a language token that is a noun,
V is a language token that is a verb.

Rules borrowed from Whitney's k5.txt.

With "last !5" you are expecting that the "te" rule will
apply, where "e" is "!5" but instead the grammar says to apply
the "nve" rule. And now last has the wrong type. It is a verb
while it needs to an int or int vector.

With "last 0 1 2 3 4" it is indeed the "te" rule that applies.

Hope this points you in the right direction.

Steve Graham

unread,
Feb 7, 2019, 2:00:27 AM2/7/19
to Kona Users
Could you perhaps explain more about the rules and give examples for them?

Thanks, Steve 

Jack Andrews

unread,
Feb 7, 2019, 2:55:11 AM2/7/19
to kona...@googlegroups.com
the simplest explanation i can think of is that ! is interpreted as infix in last!5
if the expression was 1 2!3 4, with an infix !, then it could be evaluated to produce a dictionary.
to force a prefix !, parens could be used as in last(!5) or you could even use last@!5 
a more detailed explanation will involve deriving the parse tree for these expressions

--
You received this message because you are subscribed to the Google Groups "Kona Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kona-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bakul Shah

unread,
Feb 7, 2019, 3:06:26 AM2/7/19
to kona...@googlegroups.com
Not sure what you need explaining. These rules have to do with
parsing. Conceptually an input line would be tokenized, resulting
in a sequence of tokens, which are then parsed to create a parse
tree and then evaluated. Parsing starts by "eating" one token at
a time from left to right and incrementally constructing a parse
tree using rules that may apply in a given context. For example,
given "last !5" as input, the lexer creates a stream of 3 tokens:
N-last, V-! & N-5. Then the parser pushes the first token N-last
on the stack. Next it peeks at the next token, which is V-!. This
can only be valid if the e:nve rule applies. But before that it
has to replace N with n using rule "n:N". So the stack contains n.
Next it will push V-! on the stack. Next it will peek at N-5 and
replace V-! on the stack with v. In effect it has recognized "nv"
from rule "e: nve". And so on.

This is all standard parsing -- if you are not familiar, I
recommend reading some papers or book on parsing. You may even be
able to write a yacc parser for it!

Steve Graham

unread,
Feb 7, 2019, 3:25:00 PM2/7/19
to Kona Users
Thanks, Jack.

Bakul:  I started my foray into K-like languages with Klong, which evaluates from right to left and differs from K in other ways, too.  So, your explanation does help some (thanks!), although I still don't understand the rules asa well as I would like.  Is there a source where I could review them in more detail? 


Steve

Bakul Shah

unread,
Feb 7, 2019, 4:41:42 PM2/7/19
to kona...@googlegroups.com
On Thu, 07 Feb 2019 12:25:00 -0800 Steve Graham <solitary....@gmail.com> wrote:
>
>
> On Thursday, February 7, 2019 at 12:06:26 AM UTC-8, Bakul wrote:
> >
> >
> >
> > > On Feb 6, 2019, at 11:00 PM, Steve Graham <solitary....@gmail.com
k/kona are similar in that (roughly speaking) *evaluation* is
from right to left. But note that *parsing* is from left to
right. Another way of saying is that verbs are right
associative.

The rules are BNF (Backus-Naur Form) syntax rules that define
the structure of a program. You should best read some text on
parsing as it is a vast subject.

In standard parsing texts the above rules may be specified
more verbosely. For instance instead of

E:E;e|e
e:nve|te|
v:V|tA
t:n|v
n:N|(E)|t[E]|{<[E]>E}

can be written as

expr-list: expr-list ";" expr | expr ;
expr: noun verb expr | term expr | /* empty */ ;
verb: VERB | term ADVERB ;
term: noun | verb ;
noun: NOUN | "(" expr-list ")" | term "[" expr-list "]" |
"{" parameters expr-list "}" ;
parameters: "[" expr-list "]" | /* empty */ ;

Where capitalized words are language tokens. NOUN is
an int, float, string, symbol or name.
ADVERB is ' / \ etc.
VERB is +-*%&|<>=$~#_,^!?@.;:

Note that this is simplified & not complete but should give
you an idea.

douglas mennella

unread,
Dec 12, 2021, 9:46:29 PM12/12/21
to Kona Users
Said differently, last !5 sees the ! as dyadic taking its arguments from the left and the right rather than a monad taking its argument exclusively from the right.  You can ensure that you get the monadic version, though by appending a :.  Try last !:5.  The colon is used to force a monadic interpretation.
Reply all
Reply to author
Forward
0 new messages