11 ms·
Macros produce kickass code: http://neverfriday.com/blog/?p=10#more-10 http://neverfriday.com/blog/?p=10#more-10 If I didn't create a string-case= macro, I wou
by omouse 19y ago
Macros produce kickass code: http://neverfriday.com/blog/?p=10#more-10 http://neverfriday.com/blog/?p=10#more-10
If I didn't create a string-case= macro, I would have to write the final expression over and over again. With multiple string-case matches I would have to write string=? many many times:
(cond ((or (string=? "hello" my-string)
(string=? "world" my-string))
(print "match")))
To do that with the string-case= macro:
(string-case= my-string
(("hello" "world")
(print "match")))
Saves quite a bit of typing and now I have a good example of why macros are good to have around :D
edit: formatting ftw.
- centipede 19y agoThat should possible with a higher-order-function (and a lambda), too.
- euccastro 19y agoThat only saves you one quote and one lambda for each clause, right? (define (string-case= my-string . clauses) (cond ((null? clauses) 'whatever) ((string-member? my-string (caar clauses)) ((cadar clauses))) (else (apply string-case= my-string (cdr clauses))))) Where string-member? is trivial to define as a procedure too. Then you use it like (string-case= ('("hello" "world") (lambda () (print "match")))) I myself wouldn't use a macro for this little gain. [Edit: I had completely misunderstood the macro.]
- brlewis 19y agoIf you put spaces in front of your code, news.yc will indent nicely. (cond ((or (string=? "hello" my-string) (string=? "world" my-string)) (print "match"))) (string-case= my-string (("hello" "world") (print "match")))