# Break out of the cycle. What's the Racket way alternative?

**URL:** https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814
**Category:** Questions & Answers
**Created:** [March 20, 2024, 5:14pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814 "2024-03-20T17:14:22Z")
**Posts on this page:** 15
**Page:** 2

<div class="post-metadata">

### Author: ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)
#### Post date: [March 23, 2024, 9:39am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/21 "2024-03-23T09:39:16Z")

</div>

just for fun and for compare (and i admit a bit of publicity 😉 )i'm testing a little more concise solution in Scheme+ that i share with all:

```scheme
#lang reader "../src/SRFI-105.rkt"

(module prefix racket

(require "../Scheme+.rkt")

(define (prefix str)
  (define char* (string->list str))
  (define (vhile char* result) ;; while is already defined in Scheme+
    (condx
      [(empty? char*) (list->string (reverse result))]
      [exec (define c (first char*))]
      [(char-upper-case? c) (list->string (reverse (cons c result)))]
      [(char-lower-case? c) (vhile (rest char*) (cons c result))]
      [else (vhile (rest char*) result)]))

   (vhile (rest char*) (list (first char*))))

(define examples
  '[("alfa" "alfa")
    ("Alfa" "Alfa")
    ("DiCAp" "DiC")
    ("BRaVo" "BR")
    ("b" "b")
    ("B" "B")])

(for-each (λ (x)
            (display (prefix (first x)))
            (display " ")
            (display (second x))
            (newline)) examples)

) ; end module

```

and the result:

```scheme
Welcome to DrRacket, version 8.11 [cs].
Language: reader "../src/SRFI-105.rkt", with debugging; memory limit: 8192 MB.
SRFI-105 Curly Infix parser with optimization by Damien MATTEI
(based on code from David A. Wheeler and Alan Manuel K. Gloria.)

Options :

Infix optimizer is ON.
Infix optimizer on sliced containers is ON.

Parsed curly infix code result = 

(module prefix racket
  (require "../Scheme+.rkt")
  (define (prefix str)
    (define char* (string->list str))
    (define (vhile char* result)
      (condx
       ((empty? char*) (list->string (reverse result)))
       (exec (define c (first char*)))
       ((char-upper-case? c) (list->string (reverse (cons c result))))
       ((char-lower-case? c) (vhile (rest char*) (cons c result)))
       (else (vhile (rest char*) result))))
    (vhile (rest char*) (list (first char*))))
  (define examples
    '(("alfa" "alfa")
      ("Alfa" "Alfa")
      ("DiCAp" "DiC")
      ("BRaVo" "BR")
      ("b" "b")
      ("B" "B")))
  (for-each
   (λ (x)
     (display (prefix (first x)))
     (display " ")
     (display (second x))
     (newline))
   examples))

alfa alfa
Alfa Alfa
DiC DiC
BR BR
b b
B B

```

it uses condx a cond variant that allows execution of statement in it , so there is no more need of 2 nested cond ,a single condx is enought.

---

<div class="post-metadata">

### Author: ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)
#### Post date: [March 23, 2024, 9:48am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/22 "2024-03-23T09:48:49Z")

</div>

another solution in Scheme+ would be to use the def form that allows like def in python to return at any point of the procedure (from the current call or even all the recursive calls) , here is this solution:

```scheme
#lang reader "../src/SRFI-105.rkt"

(module prefix racket

(require "../Scheme+.rkt")

(define (prefix str)
  (define char* (string->list str))
  
  (def (vhile char* result) ;; while is already defined in Scheme+
       (when (empty? char*)
	     (return (list->string (reverse result))))
       
       (define c (first char*))
       (cond [(char-upper-case? c) (list->string (reverse (cons c result)))]
	     [(char-lower-case? c) (vhile (rest char*) (cons c result))]
	     [else (vhile (rest char*) result)]))

   (vhile (rest char*) (list (first char*))))

(define examples
  '[("alfa" "alfa")
    ("Alfa" "Alfa")
    ("DiCAp" "DiC")
    ("BRaVo" "BR")
    ("b" "b")
    ("B" "B")])

;;(require rackunit)

(for-each (λ (x)
            (display (prefix (first x)))
            (display " ")
            (display (second x))
            (newline)) examples)

) ; end module

```

and the result in the execution window with parsed code result and final result:

```scheme
Welcome to DrRacket, version 8.11 [cs].
Language: reader "../src/SRFI-105.rkt", with debugging; memory limit: 8192 MB.
SRFI-105 Curly Infix parser with optimization by Damien MATTEI
(based on code from David A. Wheeler and Alan Manuel K. Gloria.)

Options :

Infix optimizer is ON.
Infix optimizer on sliced containers is ON.

Parsed curly infix code result = 

(module prefix racket
  (require "../Scheme+.rkt")
  (define (prefix str)
    (define char* (string->list str))
    (def
     (vhile char* result)
     (when (empty? char*) (return (list->string (reverse result))))
     (define c (first char*))
     (cond
      ((char-upper-case? c) (list->string (reverse (cons c result))))
      ((char-lower-case? c) (vhile (rest char*) (cons c result)))
      (else (vhile (rest char*) result))))
    (vhile (rest char*) (list (first char*))))
  (define examples
    '(("alfa" "alfa")
      ("Alfa" "Alfa")
      ("DiCAp" "DiC")
      ("BRaVo" "BR")
      ("b" "b")
      ("B" "B")))
  (for-each
   (λ (x)
     (display (prefix (first x)))
     (display " ")
     (display (second x))
     (newline))
   examples))

alfa alfa
Alfa Alfa
DiC DiC
BR BR
b b
B B
> 

```

---

<div class="post-metadata">

### Author: ![hendrikboom3](https://avatars.discourse-cdn.com/v4/letter/h/b5e925/32.png) [@hendrikboom3](https://racket.discourse.group/u/hendrikboom3)
#### Post date: [March 23, 2024, 12:34pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/23 "2024-03-23T12:34:52Z")

</div>

Is there a regular-expression package for Racket that uses S-expressions  
for regular expressions instead of this escaped-character by  
escaped-character gibberish?

-- hendrik

---

<div class="post-metadata">

### Author: ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)
#### Post date: [March 23, 2024, 12:51pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/24 "2024-03-23T12:51:00Z")

</div>

`scramble` has one: [https://docs.racket-lang.org/scramble/index.html#%28mod-path.\_scramble%2Fregexp%29](https://docs.racket-lang.org/scramble/index.html#%28mod-path._scramble%2Fregexp%29)

---

<div class="post-metadata">

### Author: ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)
#### Post date: [March 23, 2024, 3:53pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/25 "2024-03-23T15:53:38Z")

</div>

> but why it doen not cause an error ,i do not really know , but i suppose rackunit and check-equal? encapsulate the error, but i'm not sure!  
> but the algorithm is perfectly valid, this is a minor bug.

In the for-each that runs the tests, there’s `~a` that converts symbols to strings. There’s no bug.

---

<div class="post-metadata">

### Author: ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)
#### Post date: [March 23, 2024, 6:13pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/26 "2024-03-23T18:13:00Z")

</div>

[https://docs.racket-lang.org/parser-tools/Lexers.html#(mod-path.\_parser-tools%2Flex-sre)](https://docs.racket-lang.org/parser-tools/Lexers.html#%28mod-path._parser-tools%2Flex-sre%29)

---

<div class="post-metadata">

### Author: ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)
#### Post date: [March 23, 2024, 7:12pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/27 "2024-03-23T19:12:24Z")

</div>

ah... ok.I did not have noticed it.thank

Damien

---

<div class="post-metadata">

### Author: ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)
#### Post date: [March 24, 2024, 1:33am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/28 "2024-03-24T01:33:45Z")

</div>

There's an old port of Alex Shinn's irregex library to Racket but it''s a few years behind the current release.

---

<div class="post-metadata">

### Author: ![jbclements](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/jbclements/32/11_2.png) [@jbclements](https://racket.discourse.group/u/jbclements)
#### Post date: [March 24, 2024, 8:23pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/30 "2024-03-24T20:23:02Z")

</div>

I like the irregex library a lot, especially the fact that it makes Olin Shivers' SRE's available... but when I've tried to use it in practice, it turns out to be far far slower than the built-in regexps. I think that the right solution here is to build a structured front-end for the existing regexp package. Or to make irregex much faster, that would be nifty too. I also have a vague recollection that there was something like this for Rhombus, might have been more of a proof-of-concept? Maybe @usao would know more?

---

<div class="post-metadata">

### Author: ![usao](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/usao/32/1375_2.png) [@usao](https://racket.discourse.group/u/usao)
#### Post date: [March 25, 2024, 2:42am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/31 "2024-03-25T02:42:37Z")

</div>

A regexp sublanguage is shown in [the Rhombus paper](https://doi.org/10.1145/3622818), just to demonstrate how powerful the macro system can be. The same sublanguage is also used as test cases, in [`rhombus/tests/rx-space.rhm`](https://github.com/racket/rhombus-prototype/blob/3b5d18eb0534aa037c75c92c806ef302726cce98/rhombus/tests/rx-space.rhm). I think Cooper is working on a more complete version of that.

A structured notation for regexps isn’t anything new, afaik. Emacs Lisp has [the `rx` notation](https://www.gnu.org/software/emacs/manual/html_node/elisp/Rx-Notation.html), which in turn is influenced by [Scheme Regular Expressions (SRFI 115)](https://srfi.schemers.org/srfi-115/srfi-115.html).

---

<div class="post-metadata">

### Author: ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)
#### Post date: [March 25, 2024, 10:02am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/32 "2024-03-25T10:02:13Z")

</div>

about the rx notation , is there a way to use it like emacs do it:  
(rx " ...." )

insteatd of #rx" .... "

because i do not know how to modify the SRFI-105.rkt parser i use that do not support #rx" ... " notation

seems only Racket use this notation.

i mean is there a way to use a single string as a regexp :

```scheme
> (regexp-match #px"^[[:blank:]]*[;]*[[:ascii:]]*$" " (;b")
'(" (;b")
> (regexp-match "^[[:blank:]]*[;]*[[:ascii:]]*$" " (;b")
#f

```

with the same result of course.

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [March 25, 2024, 1:58pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/33 "2024-03-25T13:58:07Z")

</div>

There are regexp and pregexp constructors. The advantage of the reader syntax is some compile-time checks.

In your example, wrap the string with `(pregexepg)`.

---

<div class="post-metadata">

### Author: ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)
#### Post date: [March 25, 2024, 3:01pm UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/34 "2024-03-25T15:01:59Z")

</div>

great , i haven't found it in the doc, perheaps i could even modify the parser now....

```scheme
> (regexp-match #px"^[[:blank:]]*[;]*[[:ascii:]]*$" " (;b")
Error: SRFI-105 REPL :Unsupported # extension unsupported character causing this message is character:p
. . ../Scheme-PLUS-for-Racket/main/Scheme-PLUS-for-Racket/src/SRFI-105.rkt:136:17: SRFI-105 REPL :Unsupported # extension unsupported character causing this message is character:p
> "^[[:blank:]]*[;]*[[:ascii:]]*$"
"^[[:blank:]]*[;]*[[:ascii:]]*$"
> pregexepg
pregexepg: undefined;
 cannot reference an identifier before its definition
> regexp
#<procedure:regexp>
> pregexp
#<procedure:pregexp>
> (pregexp "^[[:blank:]]*[;]*[[:ascii:]]*$")
#px"^[[:blank:]]*[;]*[[:ascii:]]*$"

```

---

<div class="post-metadata">

### Author: ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)
#### Post date: [March 27, 2024, 11:08am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/35 "2024-03-27T11:08:51Z")

</div>

i upgraded the SRFI 105 for Racket parser to support Racket's regular expressions notation:

```scheme
;; Racket's regular expressions special syntax
	    ((char=? c #\r) (if (not (equal? (read-char port) #\x))
				(error "process-sharp : awaiting regexp : character x not found")
				(let ((str (my-read port)))
				  (if (not (string? str))
				      (error "process-sharp : awaiting regexp : string not found" str)
				      (list 'regexp str)))))

	    ((char=? c #\p) (if (not (equal? (read-char port) #\x))
				(error "process-sharp : awaiting regexp : character x not found")
				(let ((str (my-read port)))
				  (if (not (string? str))
				      (error "process-sharp : awaiting pregexp : string not found" str)
				      (list 'pregexp str)))))

```

commited in version 7.9 : [GitHub - damien-mattei/Scheme-PLUS-for-Racket: Scheme+ for Racket by Damien Mattei](https://github.com/damien-mattei/Scheme-PLUS-for-Racket)

---

<div class="post-metadata">

### Author: ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)
#### Post date: [March 30, 2024, 2:53am UTC](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814/36 "2024-03-30T02:53:04Z")

</div>

> [@benknoble](#):
>
> There are regexp and pregexp constructors. The advantage of the reader syntax is some compile-time checks.

Since regexp and pregexp values can be embedded in compiled code, a macro can expand e.g. `(rx ".")` to `'#rx"."`. One of my packages has [`rx` and `px` macros](https://docs.racket-lang.org/adjutor/Stable.html#%28def._%28%28lib._adjutor%2Fmain..rkt%29._rx%29%29) that do so. In particular, with `#lang at-exp racket`, you can write `@px{\s}` without the extra escaping of `#px"\\s"`.

[Previous page](https://racket.discourse.group/t/break-out-of-the-cycle-whats-the-racket-way-alternative/2814.md?page=1)
