Panicz Maciej Godek <godek.mac...@gmail.com> writes:

> I don't know whether the Scheme's pattern matcher
> has any notation for getting the last element of a list.
> At first, I thought that maybe it could be done using
> the unquote-splicing operator, so the equivalent code
> would look like this:
> (define (palindrome? l)
>   (match l
>     (() #t)
>     ((s1) #t)
>     (`(,s1 ,@e2 ,s1) (palindrome? e2))
>     (else #f)))
>
> but apparently this code doesn't work.
> Is there a clean and simple way to achieve this using
> the aforementioned pattern matcher?

scheme@(guile−user)> (define (palindrome? l)
                        (match l
                          (() #t)
                          ((s1) #t)
                          ((s1 s2 ... s1)
                           (palindrome? s2))
                          (else #f)))
scheme@(guile−user)> (palindrome? '(1 2 1))
$9 = #t
scheme@(guile−user)> (palindrome? '(1 2 3 2 1))
$10 = #t
scheme@(guile−user)> 

... Seems to work. I believe ___ also works.

-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"

Reply via email to