[racket] on-key question

From: Roelof Wobben (r.wobben at home.nl)
Date: Wed Mar 14 12:05:31 EDT 2012

As far as I can see I have three different things.

1) Input is a character.    Append it to the world.
2) Input is a  like this one /t . Do nothing.
3) Input is a  double or more string like right. Do nothing.

I have to figure out how i can distinct the 1 and 2 from each other.  
They both have lenght 1.


Op 14-3-2012 15:09, Stephen Bloch schreef:
>> So I have to make a cond with all the special characters and use w.
> Not necessarily.
> You can write a "cond" based on how many different inputs you might get, or based on how many different things you want to happen.  There are probably dozens or hundreds of different characters you might be given, but probably only a few different things you might want to happen.
> In the example code, there were three possibilities: (world-go w +DELTA), (world-go w -DELTA), and w (i.e. "no change").  I would start by writing a cond with those three answers:
> (cond [???           (world-go w +DELTA)]
>             [???           (world-go w -DELTA)]
>             [???           w])
> and then figure out under what conditions each of those is the right answer.  In the example code you posted, the first case happens if the key is "right" or "down"; the second happens if the key is "left" or "up"; and the third happens in all other cases.  So it could have been written
> (cond [(or (key=? key "right") (key=? key "down")) (world-go w +DELTA)]
>             [(or (key=? key "left") (key=? key "up"))         (world-go w -DELTA)]
>             [else                                                                     w])
> The string-length test is convenient because it catches all and only the "ordinary" characters: letters, digits, punctuation marks, tab, newline, carriage return, etc.  So for example if you wanted all of those to be concatenated onto something in the world, you could detect them all in one case rather than having a separate case for each character.  Function keys, arrow keys, "home" and "help" keys, etc. all have multi-character names, so you can spot them easily with string-length.
> Stephen Bloch
> sbloch at adelphi.edu

Posted on the users mailing list.