spdegabrielle
2022-2-24 09:01:37

Welcome @bsilverstrim


bsilverstrim
2022-2-24 13:40:29

:wave:


sschwarzer
2022-2-24 16:55:08

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.


sorawee
2022-2-24 16:57:03

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?


sorawee
2022-2-24 16:59:14

Not sure if you meant something like this?


sorawee
2022-2-24 16:59:15

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


sschwarzer
2022-2-24 17:00:01

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.


sschwarzer
2022-2-24 17:01:14

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


sorawee
2022-2-24 17:01:31

Yes, ... means 0 or more


sschwarzer
2022-2-24 17:02:08

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.


sschwarzer
2022-2-24 17:02:39

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


sschwarzer
2022-2-24 17:03:49

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


laurent.orseau
2022-2-24 17:04:00

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


sschwarzer
2022-2-24 17:05:22

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:


sorawee
2022-2-24 17:05:33

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


laurent.orseau
2022-2-24 17:07:42

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.


sorawee
2022-2-24 17:14:12

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:


sschwarzer
2022-2-24 17:39:51

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


samdphillips
2022-2-24 17:59:27

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


dan.ml.901
2022-2-24 18:06:39

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


dan.ml.901
2022-2-24 18:07:03

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


dan.ml.901
2022-2-24 18:07:35

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
2022-2-24 18:10:14

@jmcelmurray has joined the channel


sschwarzer
2022-2-24 18:12:05

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.


samdphillips
2022-2-24 18:13:28

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


sschwarzer
2022-2-24 18:13:53

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


sorawee
2022-2-24 18:15:31

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


samdphillips
2022-2-24 18:15:36

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


sschwarzer
2022-2-24 18:15:46

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



sschwarzer
2022-2-24 18:16:03

> 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.


sorawee
2022-2-24 18:17:21

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


sorawee
2022-2-24 18:19:44

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


sschwarzer
2022-2-24 18:20:00

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


dan.ml.901
2022-2-24 18:20:02

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?


sorawee
2022-2-24 18:20:09

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


dan.ml.901
2022-2-24 18:20:52

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


dan.ml.901
2022-2-24 18:21:14

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


sorawee
2022-2-24 18:21:53

ah, I thought you are using DrRacket


dan.ml.901
2022-2-24 18:22:47

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


sorawee
2022-2-24 18:22:49

A basic test suggests the opposite:


sorawee
2022-2-24 18:23:04

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


sorawee
2022-2-24 18:23:17

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


sorawee
2022-2-24 18:23:29

displays

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


dan.ml.901
2022-2-24 18:23:50

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


sschwarzer
2022-2-24 18:31:27

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.


sschwarzer
2022-2-24 18:31:41

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


sorawee
2022-2-24 18:33:38

It works in the command-line


sorawee
2022-2-24 18:33:48

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


ryanc
2022-2-24 18:35:55

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”.


dan.ml.901
2022-2-24 18:38:34

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?


dan.ml.901
2022-2-24 18:39:11

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


sorawee
2022-2-24 18:44:15

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


dan.ml.901
2022-2-24 18:45:31

Yes.


dan.ml.901
2022-2-24 18:46:00

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


samth
2022-2-24 18:50:48

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


christos.perivolaropo
2022-2-24 19:19:13

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


christos.perivolaropo
2022-2-24 19:19:15

?


christos.perivolaropo
2022-2-24 19:20:15

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


samdphillips
2022-2-24 19:24:13

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


sorawee
2022-2-24 19:24:29

I think this is _ from threading


samdphillips
2022-2-24 19:24:41

I think it has the same restriction


christos.perivolaropo
2022-2-24 19:26:39

ah ok


christos.perivolaropo
2022-2-24 19:26:43

thnaks!


samdphillips
2022-2-24 19:26:58

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


laurent.orseau
2022-2-24 19:48:37

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.


laurent.orseau
2022-2-24 19:57:29

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


laurent.orseau
2022-2-24 19:59:14

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


laurent.orseau
2022-2-24 19:59:50

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


rokitna
2022-2-24 20:23:45

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.


sschwarzer
2022-2-24 23:22:44

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.


rokitna
2022-2-24 23:28:14

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.