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:
#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:
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
>
Is there a regular-expression package for Racket that uses S-expressions
for regular expressions instead of this escaped-character by
escaped-character gibberish?
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.
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?
A regexp sublanguage is shown in the Rhombus paper, 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. I think Cooper is working on a more complete version of that.
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 that do so. In particular, with #lang at-exp racket, you can write @px{\s} without the extra escaping of #px"\\s".