[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Palindromes and pattern matching
From: |
Ian Price |
Subject: |
Re: Palindromes and pattern matching |
Date: |
Tue, 18 Sep 2012 23:47:14 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) |
Panicz Maciej Godek <address@hidden> 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"