Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to compare/distinguish " ", "0", 0 and mysterious... #301

Open
weshinsley opened this issue Jan 7, 2022 · 17 comments
Open

How to compare/distinguish " ", "0", 0 and mysterious... #301

weshinsley opened this issue Jan 7, 2022 · 17 comments
Labels
vNext The next version of Rockstar
Milestone

Comments

@weshinsley
Copy link

I've run into some trouble trying to parse this string (AoC 2021 day 4...)

74 50 26  0 24

So, I want to make this into an array of exactly 5 ints, but I have two challenges
(1) there is double-spacing before the 0 (as this input comes as a nicely laid out grid), so a simple split will also include some empties which I don't want - and are difficult to remove, because...
(2) one of the integers is zero - and spotting what's a zero, and what's an empty is defeating me.

In essence... I've tried a number of elaborate methods of splitting and/or burning, but I end up failing because...

  1. ("0" is " ") is TRUE. So I am struggling to detect zero vs space, if I split and parse by single characters, or if I split by "space" then try and remove the erroneous empty.
  2. 0 is mysterious, as is " " and "0", so again, I can't distinguish that way.
  3. If I burn the characters, then I can end up with NaN for a space, and 0 for a zero, which looks promising. But then I'm not sure what comparison I can do to detect tha NaN, which I think is not mystious, but checking whether "x is NaN" doesn't seem valid synax, and if I have x and y, which are both NaN, then (x is y) returns FALSE (not surprisingly).

Sorry for slightly rambling description, but hope that makes the problem clear...! Any suggestions on how I can parse that wretched string and get this crucially important squid bingo game solved (in rock style)?

Many thanks - this language is great fun.

@weshinsley
Copy link
Author

OK - I think I can skirt around the comparison issues (for now) with

listen to input
split input into pieces with "  "
join pieces into newinput with " "
split newinput into newpieces with " "

but I might run into problems comparing the 0 with things later...

@weshinsley
Copy link
Author

weshinsley commented Jan 7, 2022

And

 6  3 47 94  2

with a leading space is tricky (although we know in this case the leading space is not a zero...)

@gaborsch
Copy link

gaborsch commented Jan 7, 2022

Here's a possible solution for this issue:

  • iterate thru the space-split segments
  • split all segments into character arrays
  • check the length of the character arrays, if it's greater than 0, then it's valid
X is " 0     23 "
split X with " "

Let i be 0
while i is less than X
  let Y be X at i
  build i up
  split Y into Z
  say "Y=" + Y + ", Z=" + Z
  if Z is greater than 0
    say "valid"
    cast Y
    (Y is numeric now)
'''

@weshinsley
Copy link
Author

That's clever - I wouldn't have thought of the split Y into Z line...

@caseyc37
Copy link

caseyc37 commented Jan 7, 2022

Here's another possible solution: use the "with" keyword. Twice.

(Input contains the value to be tested)
Let Test be Input with 1
Let Test be Test with 1

After those lines, different values of Input will result in different values of Test:

Input Test
"" "11"
"0" "011"
0 2
mysterious "mysterious11"

...at which point it's fairly trivial to tell them apart by looking at the value of 'test'.

@weshinsley
Copy link
Author

weshinsley commented Jan 7, 2022

So... the crux of the matter for me is this... suppose you have 3 lines of input that goes:-

123
0
234

If you've coded in other languages, you might expect mysterious to be a kind of proxy for eof - like:

listen to my heart
while my heart isn't mysterious
  whisper my heart
  listen to my heart

which stops reading early, because the character/string "0" turns out to be mysterious. It's a bit of a surprise, since "0" is not something I think most coders would class as "undefined". (It has length after all...) I think the simplest working form might be:-

listen to my heart
split my heart into my pancreas
while my pancreas isn't mysterious
  unite my pancreas into my spleen
  shout my spleen
  listen to my heart
  split my heart into my pancreas

Or, to put it another way, is mysterious isn't always the most practical way of determining mysterious-ness, when there are zeroes floating about. Instead, we want to...

mysteriousness takes victim
  shatter victim into particles
  if particles greater than 0
    (victim isn't mysterious)


But this all feels a little bit clumsy, and I wonder if mysterious slightly overplays its hand here?

@dylanbeattie dylanbeattie added the vNext The next version of Rockstar label Jan 10, 2022
@dylanbeattie
Copy link
Collaborator

The point about mysterious proxying EOF is a very, very good one - it makes absolutely sense that you should be able to iterate across an array (or read from STDIN in a loop) and read empty strings, falses and zeros but still stop when you reach the end of the collection.

I'm starting to pull together issues for the next iteration of Rockstar, and this one is definitely one to include.

@weshinsley
Copy link
Author

weshinsley commented Jan 11, 2022

Great :-)

Related: To solve the AoC puzzle, I flattened my 5x5 grid of numbers into an array of 25 numbers, indexes 0..24 inclusive. I then wanted to have a lookup table - a sparse associative array that lets me ask: where (if at all) does this number occur in this array. The intention is that numbers not present return "mysterious", as no entry is found in my lookup.

So unless I work around it, I have a problem distinguishing between "mysterious" meaning undefined (the number is not in my 0..24 array at all), or that it is present at position zero.

As a suggestion on how it could improve in a reasonably backward-compatible way - what about an additional term "meaningless", meaning something that really is a null/undefined. Brief (but very deep) poetic support for this suggestion:-

Everything that's meaningless is mysterious
But not everything mysterious is meaningless

@caseyc37
Copy link

One simple workaround is to have your array return 1-25 for items in your array (subtract 1 to get the actual position) and mysterious for anything not in the array.

@weshinsley
Copy link
Author

Yeah, and that's what I do, and the occasional build up and knock down doesn't hurt. There may be other times though where you'd want to distinguish between zero and missing more immediately.

@gaborsch
Copy link

gaborsch commented Jan 11, 2022

Actually, it's rather a bug. The Spec says:

<Mysterious> <op> Mysterious => Equal.
<Non-Mysterious> <op> Mysterious => Non equal.

0 is non-mysteriuos, so 0 must not be equal to mysterious.

Also, the tests do not comply with this rule (equality/mysterious.rock): mysterious is null is expected to have true as a result (there is no test with 0 comparison)

@weshinsley
Copy link
Author

weshinsley commented Jan 11, 2022

All of these succeed at present (on the TRY IT page) :-

if "0" is mysterious
  shout "0 in quotes is mysterious"

if 0 is mysterious
  shout "0 without quotes is mysterious"

if "" is mysterious
  shout "empty string is mysterious"

if empty is mysterious
  shout "empty is mysterious"

if " " is mysterious
  shout "space is mysterious"
  

@weshinsley
Copy link
Author

weshinsley commented Jan 11, 2022

This also succeeds:-

if mysterious is mysterious
  shout "mysterious is mysterious"

but this (which is genuinely undefined) locks up - that's probably a different issue.

if potato is mysterious
  shout "where did that potato come from"

@gaborsch
Copy link

gaborsch commented Jan 12, 2022

@dylanbeattie To incorporate the EOF-as-mysterous feature, we need to clear the mystery around mysterious. It makes no sense for this feature if we cannot decide whether it's EOF, or we read empty line, 0, "0" or " " for the input.

It will cause a breaking change, though. Maybe worth to think on versioning as well.

@weshinsley
Copy link
Author

weshinsley commented Jan 12, 2022

My thought was that an additional meaningless might allow mysterious to stay as it is, avoiding any breakage... ALTHOUGH I suppose that will affect those who already have meaningless in their lyrics, so... hmm...!

@gaborsch
Copy link

@weshinsley True - and also, it makes mysterious meaningless (if you follow me :) ). So it would be a useless feature - but why should we keep it?

Dylan once said he is not afraid of breaking changes - and we can provide a proper compiler for each version (once there's a breaking change, I will save a version of Rocky to support the old version).

So, old codes will be able to run on old versions. We only need to distinguish them.

@LouisFr81
Copy link

LouisFr81 commented Sep 20, 2023

You can check if a value is NaN by comparing it to itself.

Let my motto be "74 50 26 0 24"
shout it
Scissors are for us
Burn them
Cut my motto into pieces with scissors
while pieces ain't mysterious
roll pieces into a blanket
burn it
if it is it
rock my world with it

shout nothing
while my world isn't nothing
roll my world into my heart
shout it

@dylanbeattie dylanbeattie added this to the 2.0 milestone Mar 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vNext The next version of Rockstar
Projects
None yet
Development

No branches or pull requests

5 participants