Hash tables in I7

Thanks, I’ll take a look at it. (And, in fact, I did code up a poker hand evaluator in I6 a while back, albeit with a different implementation. It was…fine.) I actually like the idea of a domain-specific language; I just don’t want it to be so specific that I have to sign up for the developers’ idea of IF design as well.

1 Like

I’ve written a couple of games in I6 and liked it, though at the time support for certain futures beyond the bare-bones Z-machine was sketchy, and it would be unfortunate to give up the nice development environment that I7 has. I’d consider C and Java to be equivalent for this sort of thing (though not in general), and I’m totally fine with LISP.

If it’s just a matter of outputting to a standardized web page, great. There are a few other concerns though: finding collaborators, integrating graphical modifications like in-game maps into the system, etc. Not crucial or insurmountable, but not trivial either.

It’s actually come up in a couple of places, and the question is really about whether this is a feature present in I7 or something I should work around. The documentation does have a desultory intro-computer-science section explaining that structures like sets and ring buffers can be implemented using lists— and of course they can, but there are some issues with runtime complexity, and I don’t want to reinvent something that already exists.

Two particular recent examples where this has come up are a literal dictionary with hundreds of entries and something similar to a memory heap. There are certainly other ways of implementing them, but hash tables are useful enough that I want to make sure I’m not missing something.

If you do want to implement something yourself, and if you’re targeting Glulx, then the search opcodes are an easy way to get pretty decent performance. Especially if you can sort the keys, the binary search will then be extremely efficient. But even if unsorted, the linear search opcode is likely to be more efficient than most things you could implement manually.

But note that I7 lists are not stored contiguously, so you can’t use the search opcodes on them. But you can on tables.

1 Like

The reason that I’m asking is that there is a good chance that what you want to do can be supported in a different way. For example, if someone asks how to set up an array in I7, what they usually want is an array to do something with, e.g. act as a lookup table to map code numbers to strings. In that case, an enumerated value is the ready-made and far superior choice versus trying to handcraft something. [EDIT: I see xyzoe said the same thing above.]

The same situation may be true here. A dictionary could be a relation of texts to texts. As Zed notes, there are some bugs for certain types of relations, but the current versions of the necessary functions can be backported in without much trouble.

There is also a memory heap management system that you can take advantage of at the I6 level, but again, it’s likely that a paradigm switch would make for a better answer.

2 Likes

I get not liking I7, but this part’s not accurate—it has a very real and considerable benefit! English-speakers are generally a lot faster at skimming English text to get the gist of it than skimming source code, so making the code read like English makes people a lot faster at skimming it to find the part they’re looking for.

It definitely makes it harder to write, but the idea is that any particular line of code is read many more times than it’s written, so “easy to read, hard to write” is a better tradeoff than the reverse. (I imagine you’ve seen the jokes about write-only languages!)

4 Likes

I’m a native English speaker, and no, that isn’t the case for me at all. I don’t get any benefit from the extra verbiage of “There is a truth state that varies called foo” over “bool foo.” (And good luck trying to code in I7 if you’re not an English speaker.) Instead, I want to know what the code I write does before and after I write it. I7 gives up on the former with its uncanny valley version of English, and its attempts to bolster the latter are trying to solve a problem I don’t have and don’t care about. At best, it’s a problem to be solved with organization and comments. Do other people get some benefit from I7’s verbose and quirky syntax? I don’t know, but there’s no shortage of people who can read or write code in C, Java, LISP, etc. without difficulty.

2 Likes

Without in any way trying to imply that the annoyances you’ve faced here aren’t real, and understanding that the way Inform has set up its syntax doesn’t work well with your brain - yeah, definitely, it works very well for me. I’ve got basically no formal training in coding (I took two classes 25 years ago) so things like organization and comments don’t come at all naturally to me, and trying to keep my code readable by doing that stuff while grappling with unfamiliar symbol-based logic and abstract data structures would be very challenging! On the other hand, I’m a lawyer by training, so parsing the difference between two convoluted, technical English sentences which appear the same on the surface but only one of them “works” because it uses the exact right words in exactly the right sequence is second nature.

So just to say even though Inform has made choices that make it a worse tool for some people, some of them do make it a better tool for other people. There definitely are aspects of the language and documentation that are frustrating for basically everybody, and I dunno how to evaluate whether that trade-off was a good or smart one to have made, or if there should have been alternate syntaxes and stuff like that built in for people with more traditional programming backgrounds, of course! But my sense is that there’s no shortage of languages designed for people with those kinds of backgrounds; the case for having Inform do something different seems pretty reasonable to me.

8 Likes

Inform 7 is certainly idiosyncratic as a programming language. It does not align with how experienced users of mainstream programming languages construct computer programs, neither in the large nor in the small (syntax). But that group is not the sole (or perhaps even main) audience. Clearly, it wants to make IF authoring more approachable, more “like writing a book” than like coding. As an experienced coder myself, I’m not in a good position to judge how successful that is.

What I do know that I have more fun engaging with Inform on its own terms than bickering about its syntax. I don’t disagree that it can sometimes be difficult to find the best (or any) way to express what you want to do, but reading Inform code, even ripped out of context, can be a treat. (Unless you’re currently pulling your hair out trying to find the bug in it.) Consider this snippet I just randomly pulled from WWI, specifically the Timeless example:

The plural of zombie wolf is zombie wolves. A zombie wolf is a kind of animal. Four zombie wolves are in Cleft.
Rule for writing a paragraph about zombie wolves:

say “The good news is that there isn’t much space in which for the zombie wolves to attack.”;
now every zombie wolf is mentioned.

Maybe I’m weird, but reading this as pseudo-English prose gives me more joy than treating it as a conventional computer program and engaging with it as such. In contrast, I probably never giggled at a particularly clever or punny line of C, Java, Lisp, (…) code.

7 Likes

You’re right that there are places where I7 has to twist itself into strange contortions to express what would be simple in a traditional language. But constructs like

let L be the list of red rubber balls carried by the player;

make it worth it to me.

4 Likes

Personally, I’d say just Foo is initially false (or true as appropriate).

But I agree that Inform is sometimes gratuitously verbose and this sometimes makes things harder to write correctly the first time than is generally true of conventional languages.

2 Likes

No one has mentioned the worst thing about Inform 7. The thing that is definitely wrong. The thing I can hardly bear to say.

Inform 7 doesn’t perform order of operations correctly. :scream:

(which is particularly strange for a language designed by a mathematician.)

4 Likes

But at least it’s also inconsistent between the associativity of operators as symbols and operators as English words!

4 Likes

I7 definitely gave me a quick boost when I discovered it. I was able to do things I’d long hoped I could do, and pretty well. It had limitations, but it taught me more about object-oriented programming than a textbook could. I had stuff I wanted to do and try, and I realized objects were good to use.

When something does go wrong I take a deep breath and remember the implementors aren’t paid to iron out all the kinks.

It’s been 10+ years since I started learning I7 and, yeah, sometimes it’s frustrating that you can’t do something technical which is a lot easier with, say, python or PERL. I’ve found that the default responses are a bit annoying even as a programmer.

I’ve realized it’s painful for some of the trickier stuff I want to do, and in some cases Twine is easier. Like for Trail Stash, when I wanted to track if one branch of rooms was solved, it was easier.

As for hashes I used I7 table hashes in my anagram games, but then I realized I only used them for parsing the player’s command, and I was able to streamline things into I6, making things run faster and not having to use indexed text.

So I see the power of I6 when it’s needed, but I7 is a good stopoff point for me.

For a comparison of how I use I6 and I7 hashes (not quite what the author asked for, but useful I hope):

I6 anagram hash on GitHub

I7 anagram hash on GitHub

I7 was easy for David White to whip up years ago and really got 95% of what I wanted. I6 wasn’t too bad for me to port to, once I put my mind to it. The i6 code is 40% faster.

4 Likes

Yeah. I’ve actually got cheap python scripts that spin out trivial I7 definition code. I mean they’re really trivial!

Yeah. Though I guess a few parentheses are a minor inconvenience.

In the “Inform complaints” department (maybe it’s just an old version) I noticed this code goes haywire too without parentheses. Inform 7 does a good job of guessing English syntax but often guessing semi-English syntax is a problem. You need parentheses here too. Again, I’m okay with these inconveniences.

to decide which number is boolval of (ts - a truth state):
    if ts is true, 1;
    0;

to decide which number is wander-score:
	decide on (boolval of sco-squandering-square) + (boolval of sco-laundering-lair) + (boolval of sco-pondering-pair) + (boolval of sco-maundering-mare);
1 Like

I’m still bitter from the wasted time I spent debugging before I figured it out, though. I’ll never forgive, dammit!

2 Likes

uh, in what version of Inform does this compile?

Oops. I was writing stuff from memory.

to decide what number is boolval of (ts - a truth state): (- {ts} -).

Or

to decide which number is bv of (ts - a truth state):
	if ts is true, decide on 1;
	decide on 0;

Both work in 6G60.

I had tested the original as written in 6G60 in borogove.app, hence my confusion. :smiley:

1 Like

Well, I didn’t check my own code before typing it in. I figured “Oh, I know this from memory, right?”

I didn’t quite.

Inform 7 is like that. You forget when you can shorthand stuff. You forget if it’s “of” and “in” when looking for a list element. I’m okay with having to bounce around a bit and play guess-the-preposition (there aren’t too many!) I confess I don’t remember exact Inform syntax and sometimes get close and let the compiler remind me. That’s nice to have.

But yeah. Usually I try to check valid code before pasting. Here though I was sure* I got it right!

3 Likes