Just for fun: Hangman with gloss-web

So this will be brief. Ivan Miljenovic pointed me to a discussion about writing games with kids, and the question came up about how clearly one could write a hangman game. To I took that a challenge, and I present to you: Hangman in gloss-web!

Edit: I’ve now added a random number generator to the signature of the initial worlds, so it even chooses words at random.

Like this:

Related

8 Comments

In our ‘World’ and ‘Universe’ setting (Racket, ProgramByDesign, see ICFP ’09), there are several solutions to the random word problem. The first one would be to write a single function, hangman, which maps a word to a game process. In this setting, one player would launch the program and another one would play. Alternatively, your library would provide a higher-order function, say provide-random-word, and the kids could launch it like this: (provide-random-word hangman). The second solution would be to equip each callback function with an optional random letter or integer or word or whatever. In this setting, the hangman game comes with one extra handler, which uses the first or second or whatever random word from the handler to set the World state. And voila, you remain purely functional.

I thought a lot about the random function issue because our Racket-inspired teaching languages are purely functional. In the end, however, I decided to go with plain random in our teaching languages. I am not fanatical enough to push pure functional programming to the extreme in serious programming courses. In reality most kids will and should also program in imperative languages.

Absolutely, I agree that kids who go on to do computer programming will learn imperative languages. Ideological purity isn’t the issue here! Rather, it’s that I don’t see a good interface for it. Because the assumption of purity is built in to Haskell more deeply than Scheme, there would be some structural changes needed. I really do think StdGen is the cleanest interface here. Without that, I’d have an unappealing choice between hand-waving “ignore this, it’s just magic” stuff, teaching a fairly complex operational model, or moving all the code into a monad. All of those are non-starters for me. They’d also require that I build my own library, and so far I haven’t written any of my own libraries for this environment at all. I’m offering a thin top-level interface between the environment and the program (just “implement these names with these types”), but I like the idea of telling the students that the library they are using is one that’s out there and used by lots of people all over the world.

You already wave your hands because your framework picks up certain names from the kids’ files/programs. (Beyond that you provide take and map and other ‘magic’ functions). In our World/Universe library, they write something like

All of this is collected into a ‘big-bang’ construct for ‘Worlds’ so that kids can write functions that look like

fun hangman(initial-world)
bigbang initial-world onKeyDo ….

Our kids now know that this declarative description of the world asks the computer to run certain handlers when certain events happen.

Now read my reply from before and you will see that provide-random-word(hangman) introduces no magic, does not need to make the monad explicit, and looks like all other functional stuff to the kids. Best of all, kids can run the function on their own with their chosen word.

(BTW, this is exactly what I meant with my very first post: no monads needed.)

cdsmith / Oct 9 2011 12:44 pm

Matthias, I’m not sure I understand your suggestion here. Certainly I could make things work where the framework provides with a random word, but then I’m writing code specific to hangman in the framework, which of course isn’t a reasonable design. So I’d guess I’m missing something about your suggestion.

The closest equivalent seems to be what I mentioned in the post: defining initial to receive an StdGen. Then initial would still be responsible for using it to pick a random word, but that’s at least possible given an already-initialized pseudo-random number generator. And other programs could choose to store it, and thread it through other computations. Then I’d agree that a good design might be for a student to write a function with a word as a parameter, and a higher-order function from (String -> World) to (StdGen -> World), but that’s part of the student’s work, not the framework.

Of course, reading /usr/share/dict/words is also impossible, but I’m less concerned about that. The program will just have to get its list of words elsewhere.

Funny I was thinking about it just now, googled “gloss haskell random” and got here.
Aside that, we can read the mouse position to select a word from a dictionary.
It would be possible also to pass a random stream along time parameter in animation function.
Something like : animate’ :: Time -> Noise -> Picture.

That’s basically the same idea as StdGen, which would act like your Noise type. I think I’d leave it out of the animation mode, and introduce it into simulations and games, where you can either choose to store it in your world type, or not.

For example, the specialized type of randomR is

randomR :: Random a => (a, a) -> StdGen -> (a, StdGen)

You give it a range and a noise source, and it returns a value and an updated noise source.

I think providing a (lazy) list of all words in /usr/share/dict/words from Gloss should not be considered a hangman-specific idea. There are other games and animations that will need a dictionary. Then you could initialize the state to the empty word and use the first button-down event from the mouse to choose a word.

I think what will be most consistent with the design of this environment will be: (a) providing a pre-initialized pseudo random number generator to the initial world, and (b) handling this words issue outside of gloss, for example by exporting it as a symbol from a different package, which can contain reasonable logic across various environments, such as reading the file if it exists on the operating system, using a built-in list if not, etc.

I’ve pretty much decided already to provide a random number generator to initial, which is the starting state of the world (similar to the first parameter to your big-bang). I just need to implement it, and then hunt down and then deal with the resulting incompatibility.