2016-02-22 22:46 GMT+01:00 Federico Ramírez <fedekil...@gmail.com>: (define (tokenize input) > (cond > ((match-identifier input) (consume-identifier input)) > ((match-equals input) (consume-equals input)) > ((match-number input) (consume-number input)) > (else '()))) > > What bothers me is that it's calling the matchers twice for each token, > which isn't very good for performance and it's not pretty :p >
You can use this strategy: (define (tokenize input) (cond ((match-identifier input) => (lambda (token) ... consume the identifier token...)) ((match-equals input) => (lambda (token) ... consume the equals token...)) ((match-number input) => (lambda (token) ... consume the number token...)) (else '()))) Here a clause of the form [expression1 => expression2] will calls the result of expression2 with the result of expression1 as input. That is: when (match-identifier input) returns a token the function (lambda (token) ... consume the identifier token...) will be called with the token. Given the token you can find it's length n and then skip n bytes of the input stream. -- Jens Axel Søgaard -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.