[plt-scheme] Pattern matching help

From: Sam TH (samth at ccs.neu.edu)
Date: Thu Feb 12 10:22:25 EST 2009

On Thu, Feb 12, 2009 at 10:04 AM, Veer <diggerrrrr at gmail.com> wrote:
> Hello,
>
> Code :
>
> (match-let ([(list item rest-of-lst ...) (list (list 'eat 'cake))])
>  (match (list (list 'some 'dah) (list 'hmm 'ta) (list 'life 'sucks))
>    ((list item next-item rest-items ...)
>     (printf "~a: ~a\n"  item (cons next-item rest-items)))
>
>    (_ 'false)))
>

The easiest way to do this is:

(match-let ([(list item rest-of-lst ...) (list (list 'eat 'cake))])
 (match (list (list 'some 'dah) (list 'hmm 'ta) (list 'life 'sucks))
   [(list (? (lambda (e) (equal? e item))) next-item rest-items ...)
    (printf "~a: ~a\n"  item (cons next-item rest-items))
   [_ 'false]))

Hope that helps,
-- 
sam th
samth at ccs.neu.edu


Posted on the users mailing list.