[racket] key event problem

From: Stephen Bloch (bloch at adelphi.edu)
Date: Wed Apr 18 06:59:36 EDT 2012

On Apr 18, 2012, at 5:17 AM, Roelof Wobben wrote:

> The function does only have to react on backspace , left , right .
> Give no reaction on tab , rubout or the other keys with a length more then 1.
> When given another key with length 1 it has to insert the key.
> 
> So i thought this would work :
> 
> (define (edit e k)
>  (cond
>     [  (and(key=? "right" k) (> (string-length (editor-post e)) 0)) (make-editor(string-append (editor-pre e) (string-first (editor-post e) )) (string-rest (editor-post e) ))]
>     [  (and(key=? "left" k) (> (string-length (editor-pre e)) 0)) (make-editor ( string-remove-last (editor-pre e) ) (string-append (string-last(editor-pre e))  (editor-post e)))]
>     [  (and (key=? "\b" k) (> (string-length (editor-pre e)) 0)) (make-editor (string-remove-last (editor-pre e)) (editor-post e))]
>     [  (not ( or (key=? "\t" k ) (key=? "\u007F" k) (key=? "right" k))) ( string-append (editor-pre e) k)]
>     [else e]
>     ))
> 
> but now when someone press the right-key the first get executed but also the not rule.
> I thought that cond works if one rule is true the other gets ignored. But it looks like that's not true.

That definitely should be true.  Can you show us a test case that triggers the incorrect behavior?

Remember, you shouldn't be running an animation using this event handler until the event handler passes all of its test cases in isolation.

My first guess was mismatched parentheses, so I copied and pasted your code into DrRacket, then inserted a bunch of newlines and tabs so I could spot mismatches by indentation.  The result was clearer, but I didn't see any parenthesis problems.  What I did notice is that one case doesn't obey the contract: it returns the wrong type.


Stephen Bloch
sbloch at adelphi.edu



Posted on the users mailing list.