Using the name of a function as a local in itself

56 views
Skip to first unread message

douglas mennella

unread,
Jan 6, 2022, 11:28:05 PM1/6/22
to Kona Developers
Hi there,  I was trying to understand this sudoku solver which I believe is written in k4.  I started to implement the vs function in kona and came up with this solution:

  vs:{vs1:{(*y%x;(*|y),*y!x)};{*|{~0=*x}{vs1[y]x}[;y]/x}[;x](y;())}
  vs[2;27]
1 1 0 1 1

(This is just the part of the functionality of vs that's used in that solver.)

But what was interesting is that if I change the name of the internal function to be vs I get a a type error.  Shouldn't the inner vs be local and hide the global name?

  vs:{vs:{(*y%x;(*|y),*y!x)};{*|{~0=*x}{vs[y]x}[;y]/x}[;x](y;())}
  vs[2;27]
type error



douglas mennella

unread,
Jan 6, 2022, 11:31:15 PM1/6/22
to Kona Developers
Ugh.  Sorry.  I should always test from a fresh session.  Will post back when I have a clean example.

douglas mennella

unread,
Jan 7, 2022, 1:52:40 AM1/7/22
to Kona Developers
There's some more magic in k4, I'm trying to figure out but I'm still a little confused about the above.  Here's what I get from a fresh K session.

kona      \ for help. \\ to exit.


  vs:{vs:{(*y%x;(*|y),*y!x)};{*|{~0=*x}{vs[y]x}[;y]/x}[;x](y;())}
  vs[9;15]
type error


>  vs:{vs1:{(*y%x;(*|y),*y!x)};{*|{~0=*x}{vs1[y]x}[;y]/x}[;x](y;())}
>  \
  vs[9;15]
rank error


I'm not sure why I would get different errors just by changing the name.  It looks like you can make local functions:

kona      \ for help. \\ to exit.

  fn:{f1:{x*2};f1[x]}
  fn 3
6
  f1
value error
f1
^

This was unexpected though.  It looks like f1 is somehow simply ignored.  I would have expected a value error.

  fn:{f2:{x*2};f1[x]}
  fn 3
3
  f2
value error
f2


In the end I changed vs to the following:  vs:{{*|{~0=*x}{(*x%y;(*x!y),*|x)}[;y]/x}[;x](y;())} which doesn't use a local function.  It also fixes a bug where the digits are in the reverse order which is mostly why I'm reposting now.  I think I got the first line of the solver translated now.  There seems to be some magic in k4 which lines up vectors somehow which I had to do by hand below.  For instance p1 has shape 81 2 and p has shape 8, but joining them creates an object of shape 81 3.  Also, when generating base 9 digits with vs, some are two digit and others are one digit, but the shape is still somehow 81 2 instead of a list of mixed lengths.

The second line seems to mostly work, but there is some magic in @[x;y;:] somehow filling in possible values.  I'm not clear where those possible values are coming from.

sv:{:[0=#y;0;{z+x*y}[x]/y]}
vs:{{*|{~0=*x}{(*x%y;(*x!y),*|x)}[;y]/x}[;x](y;())}

p:sv[3]'(p1:{(&2-#x),x}'vs[9]'!81)%3
p:+{(*x),*|x}'+(p;p1)

douglas mennella

unread,
Jan 7, 2022, 6:13:32 AM1/7/22
to Kona Developers
FWIW, I now get that the missing values are from the each just after @[x;y;:;].  The following produces values which are each of the values of the puzzle with the first missing slot filled with available values.  So the only question I have left is how the converge works.  Presumably, you'd like to chase each of these filling in more and more values until you find one which fills in everything.  There's no explicit recursion so I assume it's done with a queue through converge.

{@[x;y;:;]'&21=x[&|/p[;y]=p]?/:!10}[t][1]
(2 4 0 3 7 0 0 0 9 0 0 9 2 0 0 0 0 7 0 0 1 0 0 4 0 0 2 0 5 0 0 0 0 8 0 0 0 0 8 0 0 0 9 0 0 0 0 6 0 0 0 0 4 0 9 0 0 1 0 0 5 0 0 8 0 0 0 0 7 6 0 0 4 0 0 0 8 9 0 0 1
 2 6 0 3 7 0 0 0 9 0 0 9 2 0 0 0 0 7 0 0 1 0 0 4 0 0 2 0 5 0 0 0 0 8 0 0 0 0 8 0 0 0 9 0 0 0 0 6 0 0 0 0 4 0 9 0 0 1 0 0 5 0 0 8 0 0 0 0 7 6 0 0 4 0 0 0 8 9 0 0 1
 2 8 0 3 7 0 0 0 9 0 0 9 2 0 0 0 0 7 0 0 1 0 0 4 0 0 2 0 5 0 0 0 0 8 0 0 0 0 8 0 0 0 9 0 0 0 0 6 0 0 0 0 4 0 9 0 0 1 0 0 5 0 0 8 0 0 0 0 7 6 0 0 4 0 0 0 8 9 0 0 1)


This fills more and more values always picking the first option, but runs into dead ends:

  {1{*{@[x;y;:;]'&21=x[&|/p[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