Re: [racket-users] Doing pattern matching by reader macro
Your answer is perfect. Thanks a lot! I sent you a private message already to have some further discussion. Mianlai On Thu, May 21, 2015 at 12:43 PM, Alexander D. Knauth alexan...@knauth.org wrote: Here’s one way to do something like that with a macro: https://github.com/AlexKnauth/define-match-spread-out/blob/master/define-match-spread-out/main.rkt And using it: https://github.com/AlexKnauth/define-match-spread-out/blob/master/define-match-spread-out/tests/test.rkt On May 18, 2015, at 9:39 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Matthias, Thanks for answer. However, this is not what I wanted: I want to persist that I can write the code below (where define can be changed to other name): (define (f #t) 2) (define (f #f) 3) (define (f _) 0) to do what I want to do. So how should I define my define in order to do it? Your answer is perfect but it is a different form from my original code above. Could you give me an answer? Thanks. M. On Mon, May 18, 2015 at 9:20 PM, Matthias Felleisen matth...@ccs.neu.edu wrote: #lang racket (define/match (f n) [(#t) 2] [(#f) 3] [(_) 0]) (list (f #t) (f #f) (f where's my homework)) On May 18, 2015, at 9:04 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Racketeers, I am a new user of Racket. I would want to be able to write the following segment of code: (define (f #t) 2) (define (f #f) 3) (define (f _) 0) My intention is to define a function f, such that given an argument #t it will produce 2, or in case given an argument #f it will produce 3. For any other value (shown by underscore), it will give 0. This is done by pattern matching. Obviously, I cannot do this straightforward, because in current Racket the definitions in the prior lines will be overshadowed by definitions given in the lines after. Moreover, I cannot give the real values of argument, #t, #f and underscore, directly. If I do want to write the code as shown above, probably I have to use reader macro. The question now is, how to do it? How to implement a pattern-matching-style syntax by means of reader macro? Thank you all for helping me, a real new beginner in Racket language. M. -- 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. -- 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. -- 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. -- 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.
Re: [racket-users] Doing pattern matching by reader macro
Here’s one way to do something like that with a macro: https://github.com/AlexKnauth/define-match-spread-out/blob/master/define-match-spread-out/main.rkt And using it: https://github.com/AlexKnauth/define-match-spread-out/blob/master/define-match-spread-out/tests/test.rkt On May 18, 2015, at 9:39 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Matthias, Thanks for answer. However, this is not what I wanted: I want to persist that I can write the code below (where define can be changed to other name): (define (f #t) 2) (define (f #f) 3) (define (f _) 0) to do what I want to do. So how should I define my define in order to do it? Your answer is perfect but it is a different form from my original code above. Could you give me an answer? Thanks. M. On Mon, May 18, 2015 at 9:20 PM, Matthias Felleisen matth...@ccs.neu.edu wrote: #lang racket (define/match (f n) [(#t) 2] [(#f) 3] [(_) 0]) (list (f #t) (f #f) (f where's my homework)) On May 18, 2015, at 9:04 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Racketeers, I am a new user of Racket. I would want to be able to write the following segment of code: (define (f #t) 2) (define (f #f) 3) (define (f _) 0) My intention is to define a function f, such that given an argument #t it will produce 2, or in case given an argument #f it will produce 3. For any other value (shown by underscore), it will give 0. This is done by pattern matching. Obviously, I cannot do this straightforward, because in current Racket the definitions in the prior lines will be overshadowed by definitions given in the lines after. Moreover, I cannot give the real values of argument, #t, #f and underscore, directly. If I do want to write the code as shown above, probably I have to use reader macro. The question now is, how to do it? How to implement a pattern-matching-style syntax by means of reader macro? Thank you all for helping me, a real new beginner in Racket language. M. -- 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. -- 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. -- 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.
[racket-users] Doing pattern matching by reader macro
Hi Racketeers, I am a new user of Racket. I would want to be able to write the following segment of code: (define (f #t) 2) (define (f #f) 3) (define (f _) 0) My intention is to define a function f, such that given an argument #t it will produce 2, or in case given an argument #f it will produce 3. For any other value (shown by underscore), it will give 0. This is done by pattern matching. Obviously, I cannot do this straightforward, because in current Racket the definitions in the prior lines will be overshadowed by definitions given in the lines after. Moreover, I cannot give the real values of argument, #t, #f and underscore, directly. If I do want to write the code as shown above, probably I have to use reader macro. The question now is, how to do it? How to implement a pattern-matching-style syntax by means of reader macro? Thank you all for helping me, a real new beginner in Racket language. M. -- 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.
Re: [racket-users] Doing pattern matching by reader macro
#lang racket (define/match (f n) [(#t) 2] [(#f) 3] [(_) 0]) (list (f #t) (f #f) (f where's my homework)) On May 18, 2015, at 9:04 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Racketeers, I am a new user of Racket. I would want to be able to write the following segment of code: (define (f #t) 2) (define (f #f) 3) (define (f _) 0) My intention is to define a function f, such that given an argument #t it will produce 2, or in case given an argument #f it will produce 3. For any other value (shown by underscore), it will give 0. This is done by pattern matching. Obviously, I cannot do this straightforward, because in current Racket the definitions in the prior lines will be overshadowed by definitions given in the lines after. Moreover, I cannot give the real values of argument, #t, #f and underscore, directly. If I do want to write the code as shown above, probably I have to use reader macro. The question now is, how to do it? How to implement a pattern-matching-style syntax by means of reader macro? Thank you all for helping me, a real new beginner in Racket language. M. -- 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. -- 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.
Re: [racket-users] Doing pattern matching by reader macro
Your best bet is to define a new language that overrides define after taking over the body of the module. This may even be an example in the docs. On May 18, 2015, at 9:39 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Matthias, Thanks for answer. However, this is not what I wanted: I want to persist that I can write the code below (where define can be changed to other name): (define (f #t) 2) (define (f #f) 3) (define (f _) 0) to do what I want to do. So how should I define my define in order to do it? Your answer is perfect but it is a different form from my original code above. Could you give me an answer? Thanks. M. On Mon, May 18, 2015 at 9:20 PM, Matthias Felleisen matth...@ccs.neu.edu wrote: #lang racket (define/match (f n) [(#t) 2] [(#f) 3] [(_) 0]) (list (f #t) (f #f) (f where's my homework)) On May 18, 2015, at 9:04 AM, Mianlai Zhou mianlai.resea...@gmail.com wrote: Hi Racketeers, I am a new user of Racket. I would want to be able to write the following segment of code: (define (f #t) 2) (define (f #f) 3) (define (f _) 0) My intention is to define a function f, such that given an argument #t it will produce 2, or in case given an argument #f it will produce 3. For any other value (shown by underscore), it will give 0. This is done by pattern matching. Obviously, I cannot do this straightforward, because in current Racket the definitions in the prior lines will be overshadowed by definitions given in the lines after. Moreover, I cannot give the real values of argument, #t, #f and underscore, directly. If I do want to write the code as shown above, probably I have to use reader macro. The question now is, how to do it? How to implement a pattern-matching-style syntax by means of reader macro? Thank you all for helping me, a real new beginner in Racket language. M. -- 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. -- 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. -- 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.