chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-users] Re: matchable egg usage question


From: Alan Post
Subject: [Chicken-users] Re: matchable egg usage question
Date: Fri, 28 Jan 2011 16:59:40 -0700

On Thu, Jan 27, 2011 at 10:21:05PM -0700, Alan Post wrote:
> I'm trying to use the matchable egg to detect #!key parameters in
> functions I've constructed.  I have functions that accept multiple
> #!key parameters, and I'm not sure how to make the matchable egg
> match |(func ... mykey: myvalue ...)|.  That is, how to get the
> matchable egg to match two parameters anywhere in an input list.
> 
> This is what I've got so far.  I am interested in whether the
> #!key paramater 'a' is set to #t:
> 
>   (use matchable)
> 
>   (pretty-print (map
>     (match-lambda
>       ((_ 'a: #t . _) '(0 #t))
>       ((_ ... 'a: #t) '(1 #t))
>       (_              #f))
>     '((foo)
>       (foo bar)
>       (foo a: #f)
>       (foo a: #f b: #t)
>       (foo a: #f b: #t c: #t)
>       (foo a: #f c: #t b: #t)
>       (foo b: #t a: #f c: #t)
>       (foo b: #t c: #t a: #f)
>       (foo a: #t)
>       (foo a: #t b: #t)
>       (foo a: #t b: #t c: #t)
>       (foo a: #t c: #t b: #t)
>       (foo b: #t a: #t c: #t)
>       (foo b: #t c: #t a: #t))))
>   (exit)
> 
> This works for every case except when the #!key I want to match
> appears in the middle of a list of arguments, as happens in the
> second-to-last case I'm trying to match.
> 
> Is there a way to make match work in this case?  What pattern
> should I add to match values anywhere in the list, not just the
> front and end?
> 

I've solved my problem by matching every sublist of my argument
list until I find the pattern I'm looking for:

  (use matchable)
  (use srfi-1)

  (define (key-match? pat r)
    (or r (match pat
            (`(a: #t . ,_) #t)
            (_             #f))))

  (pretty-print (map
    (match-lambda
      ((_ . args) (pair-fold key-match? #f args))
      (_ #f))
    '((foo)
      (foo bar)
      (foo a: #f)
      (foo a: #f b: #t)
      (foo a: #f b: #t c: #t)
      (foo a: #f c: #t b: #t)
      (foo b: #t a: #f c: #t)
      (foo b: #t c: #t a: #f)
      (foo a: #t)
      (foo a: #t b: #t)
      (foo a: #t b: #t c: #t)
      (foo a: #t c: #t b: #t)
      (foo b: #t a: #t c: #t)
      (foo b: #t c: #t a: #t))))
  (exit)

(The match-lambda here is wordier in my real example, I know it is
pointless in this example.)

-Alan
-- 
.i ko djuno fi le do sevzi



reply via email to

[Prev in Thread] Current Thread [Next in Thread]