gmauer
2021-10-14 03:53:24

A dumb question on how to use match. I have the following data structure '(ip-program "\n" "\n" (ip-row (ip-cell (ip-vertical-wall "\|") (ip-whitespace " ") (ip-mark (ip-land "X")) (ip-whitespace " ")) (ip-cell (ip-vertical-wall "\|") (ip-whitespace " ") (ip-mark (ip-water " ")) (ip-whitespace " ")) (ip-cell (ip-vertical-wall "\|") (ip-whitespace " ") (ip-mark (ip-land "X")) (ip-whitespace " ")) (ip-vertical-wall "\|"))) I want to write a function that will match on each car I can do the following (define/match (parse-island ip-program) [((cons head args)) #:when (eq? 'ip-program head) args]) but surely that’s not the best way to match on (ip-program . rest)


gmauer
2021-10-14 03:54:18

secondary question to the above. Is there a way to filter out the "\n"s (there can be an arbitrary number) via the match clause itself? That’s the sort of thing that just feels like there might be a way to do it


samth
2021-10-14 03:54:38

You can write ’ip-program instead of head


samth
2021-10-14 03:55:18

And you can use the list pattern or the list-rest pattern along with … to handle the newlines


gmauer
2021-10-14 03:56:09

(define/match (parse-island ip-program) [((cons ip-program args)) args]) like that? Doesn’t that just match on any list just deconstructing the head and putting it in a variable named ip-program?


gmauer
2021-10-14 03:57:26

Yeah, that’s what it does #+begin_src racket (require racket/match) (define/match (parse-island ip-program) [((cons ip-program args)) args]) (parse-island (list 1 2 3)) #+end_src #+RESULTS: : '(2 3)


sorawee
2021-10-14 04:28:28

@samth said 'ip-program, not ip-program.

So:

(define/match (parse-island ip-program) [((cons 'ip-program args)) args])


sorawee
2021-10-14 04:29:03

If you want it to mirror your data, you can even use quasiquote pattern:

(define/match (parse-island ip-program) [(`(ip-program ,@args)) args])


sorawee
2021-10-14 04:32:01

To filter out \n, you could do something like:

(define/match (parse-island ip-program) [(`(ip-program "\n" ... ,@args)) args])


sorawee
2021-10-14 04:32:50

or if you don’t want to use quasiquote pattern:

(define/match (parse-island ip-program) [((list 'ip-program "\n" ... args ...)) args])


gmauer
2021-10-14 04:35:36

ah, thank you!, I kept trying different variations but couldn’t quite get it


jcoo092
2021-10-14 05:47:59

Discussions like this one remind that I have a lot to learn about Racket (possibly Scheme or LISP in general) still :sweat_smile: