Re: [racket-users] Replacing multiple characters in a string?
Or, arguably, not Rackety enough, until it's possible to embed one #lang within another (submodules at least, but hey why not lambdas?) I've been exploring this more and more. It is related to my shell project (rash, which does allow embedding at the expression level), and I've been thinking about it a lot in relation to other things I want to do or have otherwise been considering. Some time soon I'll have to write up my thoughts on the subject. -- 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] Replacing multiple characters in a string?
Keep reading? #lang lindenmayer racket ## axiom ## ABCABC ## rules ## A -> AC B -> BA C -> CCA ## variables ## n=1 (provide A B C start finish) (define (finish l vars) (writeln l)) (define (A s vars) (cons 'A s)) (define (B s vars) (cons 'B s)) (define (C s vars) (cons 'C s)) (define (start var) '()) On Sat, Jul 8, 2017 at 2:38 PM, Matthew Butterickwrote: > > >> On Jul 8, 2017, at 7:15 AM, Robby Findler >> wrote: >> >> Below is another approach that may be too much Rackety for your taste: :) >> >> http://docs.racket-lang.org/lindenmayer/ > > Or, arguably, not Rackety enough, until it's possible to embed one #lang > within another (submodules at least, but hey why not lambdas?) > > -- > 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] Replacing multiple characters in a string?
> On Jul 8, 2017, at 7:15 AM, Robby Findlerwrote: > > Below is another approach that may be too much Rackety for your taste: :) > > http://docs.racket-lang.org/lindenmayer/ Or, arguably, not Rackety enough, until it's possible to embed one #lang within another (submodules at least, but hey why not lambdas?) -- 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] Replacing multiple characters in a string?
I'd probably do this: (apply string-append (for/list ((letter (string->list str))) (cond [(equal? letter #\A) "AC"] [(equal? letter #\B) "BA"] [(equal? letter #\C) "CCA"]))) -- 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] Replacing multiple characters in a string?
Below is another approach that may be too much Rackety for your taste: :) http://docs.racket-lang.org/lindenmayer/ Robby #lang lindenmayer ## axiom ## ABCABC ## rules ## A -> AC B -> BA C -> CCA ## variables ## n=1 -- 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] Replacing multiple characters in a string?
This works, for instance (and I would much prefer case to hash-ref): #lang typed/racket (regexp-replace* #rx"[ABC]" "ABCABC" (λ ([c : String] . _) (case c [("A") "AC"] [("B") "BA"] [("C") "CCA"] [else (error 'bad-match c)]))) -Philip On Sat, Jul 8, 2017 at 8:14 AM, Alasdair McAndrewwrote: > Many thanks - that works fine! And it makes sense, to use regexp-replace* > with a hash table. (All these things I'm still finding out...) Is it > possible to do this - or something similar - in Typed Racket? > > On Saturday, 8 July 2017 22:45:04 UTC+10, Neil Van Dyke wrote: > > This is one OK way that people are likely to do: > > > > > > #lang racket/base > > > > (define (foo str) > >(regexp-replace* #rx"[ABC]" > > str > >(lambda (s) > > (hash-ref #hash(("A" . "AC") > > ("B" . "BA") > > ("C" . "CCA")) > >s > > > > (module+ test > >(require rackunit) > >(check-equal? (foo "") "") > >(check-equal? (foo "ABCABC") "ACBACCAACBACCA") > >(check-equal? (foo "ABCDABCD") "ACBACCADACBACCAD")) > > > > > > It's not necessarily the most maintainable (note the duplicated info > > between the regexp and the map) nor most efficient, but it's OK, IMHO. > > -- > 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] Replacing multiple characters in a string?
Many thanks - that works fine! And it makes sense, to use regexp-replace* with a hash table. (All these things I'm still finding out...) Is it possible to do this - or something similar - in Typed Racket? On Saturday, 8 July 2017 22:45:04 UTC+10, Neil Van Dyke wrote: > This is one OK way that people are likely to do: > > > #lang racket/base > > (define (foo str) >(regexp-replace* #rx"[ABC]" > str >(lambda (s) > (hash-ref #hash(("A" . "AC") > ("B" . "BA") > ("C" . "CCA")) >s > > (module+ test >(require rackunit) >(check-equal? (foo "") "") >(check-equal? (foo "ABCABC") "ACBACCAACBACCA") >(check-equal? (foo "ABCDABCD") "ACBACCADACBACCAD")) > > > It's not necessarily the most maintainable (note the duplicated info > between the regexp and the map) nor most efficient, but it's OK, IMHO. -- 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] Replacing multiple characters in a string?
This is one OK way that people are likely to do: #lang racket/base (define (foo str) (regexp-replace* #rx"[ABC]" str (lambda (s) (hash-ref #hash(("A" . "AC") ("B" . "BA") ("C" . "CCA")) s (module+ test (require rackunit) (check-equal? (foo "") "") (check-equal? (foo "ABCABC") "ACBACCAACBACCA") (check-equal? (foo "ABCDABCD") "ACBACCADACBACCAD")) It's not necessarily the most maintainable (note the duplicated info between the regexp and the map) nor most efficient, but it's OK, IMHO. -- 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] Replacing multiple characters in a string?
Suppose I have a string "ABCABC" and I want to replace all A's with "AC", all B's with "BA" and all C's with "CCA", so that the result is ACBACCAACBACCA What is the canonical way of doing this in Racket? -- 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.