Match treats `empty` as an identifier to bind, not as a pattern for the empty 
list. This means that the first case will always be matched. You can use 
`(list)` or `'()` to match an empty list.

Also, in the last catch-all case, you pass the function `rest` which returns 
the tail of a list to `filter-odds`. You probably wanted to shadow that binding 
by using `(cons first rest)` instead of `_`.

Here's the version with the fixes above:

(define (filter-odds v)
  (match v
    [(list) (list)]
    [(cons (? odd? first) rest)
     (cons first (filter-odds rest))]
    [(cons first rest) (filter-odds rest)]))

(filter-odds '(1 2 3 4 5)) ; ==> '(1 3 5)

Best,
Michael MacLeod

On Sunday, April 21, 2019 1:19:03 PM PDT Tim Meehan wrote:
> Forgive this naive question, I am having some trouble understanding some
> matching forms. If I wanted to filter out odd numbers like this:
> 
> (filter odd? '(1 2 3 4 5)) => '(1 3 5)
> 
> but I wanted to do this with a match:
> (define (filter-odds v)
>   (match v
>     [empty empty]
>     [(cons (? odd? first) rest) (cons first (filter-odds rest))]
>     [_ (filter-odds rest)]))
> 
> (filter-odds '(1 2 3 4 5)) => '(1 2 3 4 5)


-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to