Novice racket coder here.
I have an xexpr that I'd like to filter newlines when they are next to certain tags. The idea is that I'd like to avoid new lines before and after certain elements, like the 'eq tag or 'figure tag.
Suppose I have the following:
(define tx1
'(div (div "this is" "\n\n" (eq "y=x") "\n\n" "my equation" (eq "z=y"))
"to fire" (eq "h=u") "up" "\n\n\n" (eq "tm=re") "\n" "this test" "\n"))
I'd like to remove the newline strings ("\n", "\n\n", "\n\n\n") around the 'eq tags to produce the following:
(define result
'(div (div "this is" (eq "y=x") "my equation" (eq "z=y"))
"to fire" (eq "h=u") "up" (eq "tm=re") "this test" "\n"))
My first step, is to recognize the newlines. I've found that pattern match is a possible solution.
(match tx1
[(list a ... (regexp #rx"^\n+") b ...) `(,@a ,@b)]
[(list a ...) `(,@a)])
However, I have to run this match function once for every occurrence of a newline. In this case, I have to run it 3 times. I could test the result every time to see if it has changed, but this seems sub-optimal.
The second step, which I have not reached yet, is to match previous and subsequent items (a and b) to lists with an 'eq tag. The third step is to find sublists and recurse over those.
My question is, first is there a better approach to fixing this sort of problem? I believe there likely is such a solution. My second question is, if matching is best, is there a way to conduct a multiple replace? I've played with cons matching, and that works to some extent, but it discards the first item.
Thank you.