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)
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
You can write ’ip-program instead of head
And you can use the list pattern or the list-rest pattern along with … to handle the newlines
(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
?
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)
@samth said 'ip-program
, not ip-program
.
So:
(define/match (parse-island ip-program)
[((cons 'ip-program args)) args])
If you want it to mirror your data, you can even use quasiquote pattern:
(define/match (parse-island ip-program)
[(`(ip-program ,@args)) args])
To filter out \n
, you could do something like:
(define/match (parse-island ip-program)
[(`(ip-program "\n" ... ,@args)) args])
or if you don’t want to use quasiquote pattern:
(define/match (parse-island ip-program)
[((list 'ip-program "\n" ... args ...)) args])
ah, thank you!, I kept trying different variations but couldn’t quite get it
Discussions like this one remind that I have a lot to learn about Racket (possibly Scheme or LISP in general) still :sweat_smile: