jjwiseman
2018-3-28 07:07:57

@notjack hm. did you try that? or maybe i’m doing something wrong? engine.rkt> (match '(x) [(or (list) (list v)) v]) ; stdin::461: match: variable not bound in all or patterns ; at: v ; in: (or (list) (list v))


notjack
2018-3-28 07:13:18

@jjwiseman I didn’t, I skimmed the match grammar and took a shot in the dark :)


notjack
2018-3-28 07:24:17

@jjwiseman I got this to work, but it’s pretty unpleasant to read:

> (define (f lst)
    (match lst
      [(or (and (list) (app (const #f) v)) (list v)) v]))
> (f '(x))
'x
> (f '())
#f
> (f '(x y))
match: no matching clause for '(x y)

greg
2018-3-28 19:57:19

Yes, (or pattern-that-binds-id (app (const #f) id)) is the only “trick” I know for that. [Note that if #f could be a valid value for id, you could use some symbol like 'not-found or singleton struct or whatever to mean “didn’t match anything to bind to id”.] I’ve thought about defining a match-expander for this to reduce the tedium… But in my own experience the need for zero-or-one is usually just a detail of some bigger pattern I need to address with a separate match clause, anyway.


greg
2018-3-28 19:59:18

Like, if in the match handler for that clause, I need a sub-test whether id was bound to a non-#f value? Then I probably just need a separate match clause altogether.


greg
2018-3-28 19:59:50

At least, that’s been my experience. Yours may vary. @notjack @jjwiseman


jjwiseman
2018-3-28 20:02:00

i was looking for a way to match something that looked like keyword arguments. it felt like it should have been relatively simple, i was surprised that match didn’t support it directly.


greg
2018-3-28 20:03:45

I’m not sure what you mean by something that looks like keyword arguments. There is a hash-table pattern.


lexi.lambda
2018-3-28 20:04:29

FWIW, syntax-parse can match raw datums (non-syntax objects) and is very good at parsing keyword arguments. :)


greg
2018-3-28 20:04:29

If you mean literally keyword args, then maybe you mean a macro and want syntax-parse instead?


greg
2018-3-28 20:04:38

jinx


lexi.lambda
2018-3-28 20:04:41

:)


jjwiseman
2018-3-28 20:34:59

e.g i would like to match (rule #:lhs (foo) #:rhs (bar)) or something like that, where lhs and rhs can appear in any order and are optional.


jjwiseman
2018-3-28 20:35:23

oh, syntax-parse. i didn’t realize it could handle datums (data?)


jjwiseman
2018-3-28 20:36:35

thanks, everyone!


lexi.lambda
2018-3-28 20:37:44

I think syntax-parse will wrap values in syntax objects even if you feed it non-syntax, though, which might not be what you want.