
Welcome @bsilverstrim

:wave:

Is there a match
variant so that I can match items of a list, but with interspersed other items? Something like (match '(1 2 3 4 5 6)
[(.. 2 .. 5 ..) x y])
-> x = 2 and y = 5
Of course, the matches in the pattern “list” on the left side will be more complicated. In my case (? pred? item)
forms.
I checked the <https://docs.racket-lang.org/reference/match.html|Racket reference>, but I’m not sure if anything mentioned there matches my use case.

I don’t think that makes sense. x
and y
are not in the pattern in your example, right? How could it bind x
and y
to 2
and 5
?

Not sure if you meant something like this?

(require math)
(match '(4 6 7 8 9 10 11 12)
[(list _ ... (? prime? x) _ ... (? prime? y) _ ...)
(list x y)])
;=> '(7 11)

Maybe my example is confusing. I try to explain it differently. From some web scraping I get a list of xexpr and want to extract only certain h3
and div
elements from this list. Before the h3
and between the h3
and div
elements (and between the div
elements are strings). I want to extract only the h3
and (certain) div
matches.

Maybe. :wink: Will _ ...
also match if there are no items? In your example, if the list to match starts with a prime.

Yes, ...
means 0 or more

But actually, in my concrete case, as far as I can tell, the interspersed strings should only contain whitespace, so I can filter them out relatively easily with filter-not
.

Good. I guess I’ll try this. Thank you! :slightly_smiling_face:

The match
patterns are a DSL in itself. :satisfied:

~r
throws an exception when given +inf.0
. Why not just print +inf.0
directly? I always need to wrap it with a test

Sometimes I think I should use other (presumably easier to understand) ways to do something because I’m not sure I’ll understand a match pattern a few months from now. :wink:

Well, ~r
consumes a rational number. +inf.0
is not a rational number :slightly_smiling_face:

Well, sure, but I don’t see why it couldn’t consume +inf.0 and +nan.0 too. I often end up with numbers too large to stay rational, and instead of printing +inf.0
my program just crashes.

You can search for “pattern matching is pretty powerful,” in http://sorawee.com/blog/2017-01-03-colorful-parentheses.html and then feel disgusted about it :wink:

Learning about continuations is also on my list. :wink:

match
expressions can be like regexp in that way. Write only. :smile:

Will parameterize
work across a dynamic-require
? Like so:

(parameterize ([error-print-width 256])
(dynamic-require (submod (file "file.rkt") submodule) 'name))

We’re dealing with some very long path names in the errors and click-to-jump-to-error won’t work when they get abbreviated.

@jmcelmurray has joined the channel

Yes, I had the comparison with regexp on my mind. :slightly_smiling_face: That said, by making regexps not too complicated and distributing them over several lines with some structure/indentation makes them much more manageable than writing them in one long string on a single line.

I first thought it was to be consistent with format
, but it doesn’t support ~r
!?!

Is there a way to drop into a REPL from a running program, like for looking at some data structures for debugging?

are you allowed to edit the program to indicate the checkpoint to run the REPL?

match
also has an advantage (or disadvantage if abused) of extending the pattern language.

I guess that would be tricky though because when the program runs, many identifiers are gone.


> are you allowed to edit the program to indicate the checkpoint to run the REPL? Yes, for now that would be for my own code.

well, I think ~r
is most useful from all options like precision parameters, etc. It’s impossible to specify these parameters in format
.

are these compile-time error or run-time error?

Yes, the extensibility of Racket really is a double-edged sword.

It looks like parameters are default across a dynamic-require
— is there any other way to parameterize
across a dynamic-require
? Maybe a config file? Or any other way to adjust the error-display-handler?

I think “jump to error” only appears for syntax error, right?

Oh, the error we’re jumping from appears from a command line invocation of Racket within VS Code

We invoke Racket that in turn calls dynamic-require
which in turn encounters an error.

ah, I thought you are using DrRacket

No, this is Racket tooling embedded in a hybrid C++-ish IDE workflow.

A basic test suggests the opposite:

;; a.rkt
#lang racket
(parameterize ([error-print-width 10])
(dynamic-require '(submod "b.rkt" test) #f))

;; b.rkt
#lang racket
(module test racket
(begin-for-syntax
(println (list 'compile-time (error-print-width))))
(println (list 'run-time (error-print-width))))

displays
'(compile-time 10)
'(compile-time 10)
'(run-time 10)

Oh, interesting! Ok, we’ll go back and verify the behavior, thanks!

I added (debug-repl)
to my code. I get a text entry line in DrRacket, but when I type an expression, I only get printed what I typed, but not the evaluated expression value. Debugging is active in DrRacket.

I have changed the #lang
as well, as written in the package documentation.

It works in the command-line

Doesn’t appear to work in DrRacket. Have no idea why

The contract is rational?
because I specifically wanted to exclude the infinities and NaNs rather than try to just make up a behavior for “print negative infinity to exactly two decimal places in a 10 character field, 0 padded”.

Ok, to rephrase our problem — I think we’re getting a syntax error exception thrown by Racket that truncates the filename with ...
. The error looks like: /long/path/name/and/a/file-that-goes-on-and-on-an...:203:5: some-name: unbound indentifier
What parameter controls that filename truncation?

I thought it was error-print-width
, but that doesn’t seem to control the truncation.

What’s your actual setup? If you run the file from the command-line (not through VSCode), does it truncate the file name?

Yes.

We invoke racket which in turn calls dynamic-require
on a file as passed in via the commandline.

@dan.ml.901 parameters definitely can effect the code inside a module that you dynamic-require
.

why does (~> (set a b c) (filter (lambda (x) (eq? 'a x)) (in-set _)))
say _: wildcard not allowed as an expression

?

I guess it’s that in-set
gets expanded first, is there a way to expand ~>
first?

The ~fancy-app
~ threading
underscore can only be one level deep iirc.

I think this is _
from threading

I think it has the same restriction

ah ok

thnaks!

This is how to get that to work with threading
: (~> (set a b c) set->list (filter (lambda~>> (eq? 'a) _))

Why would you have to print infinity to decimal places? Just print +inf.0. Throwing an exception forces to wrap ~r with all its keyword arguments, and that’s pretty inconvenient in base racket.

(but thanks in any case for ~r because it’s very convenient I must say :slightly_smiling_face:)

Related, I want to write a 'number
alignment for text-table
and I welcome opinions for this

(which would include +inf.0 and +nan.0)

match
patterns are like pseudocode to me. I can hardly imagine code that’s easier to understand… unless perhaps the ellipses ...
were a prefix notation to be more Lispy. When I first learned about regexes in Java, it felt transformational, and I wondered why they stopped at strings and didn’t match all kinds of sequences and data structures. Addressing that gap was the focus of some of my earliest DSL implementations.

For me, something like (? something?)
isn’t that clear. :wink:
I also have to remind myself that I can use literals in the pattern, but not existing names because they’ll refer to new names created by match
, not the values for already existing names. I mean, on a rational level the difference is clear, but I have to watch out for that trap because it’s the opposite of what I’d intuitively think.
I guess you don’t notice these problems anymore after you’ve “learned” them. Maybe like not noticing certain irregularities of a natural language if you’re a native speaker or are very familiar with the language.

The (? something? x)
notation is pretty weird, yeah. I understood it fast because it’s the kind of thing I would have made for my regex-like DSLs a long time ago, but I think I would have called it something that explained itself more. At the same time, it already feels a little verbose when I use it, so maybe that wouldn’t make it better.