From: Panicz Maciej Godek <godek.maciek@gmail.com>
To: guile-user@gnu.org
Subject: Palindromes and pattern matching
Date: Tue, 18 Sep 2012 22:08:11 +0200 [thread overview]
Message-ID: <CAMFYt2Z5UNHWtKO-0FCi3bUoMeb9Ge6+rPhbd9Q5NH7_YotFoA@mail.gmail.com> (raw)
Howdie,
I've been learning the refal language recently
(http://refal.botik.ru/book/html/ )
and I've been wondering, if there's any way
to simulate the behaviour of the refal's pattern
matcher using the Andrew K. Wright's/Alex Shinn's
pattern matcher for Scheme. Namely,
the Refal tutorial presents a following definition
of a palindrome:
<quote>
1. An empty string is a palindrome.
2. A string of one symbol is a palindrome.
3. If a string starts and ends with the same symbol, then it is a
palindrome if and only if the string which remains after the removal
of the first and the last letters is a palindrome.
4. If none of the above are applicable, the string is not a palindrome.
</quote>
The tutorial also presents the Refal code that
implements the above conditions to construct the
predicate that states whether a given object is a
palindrome:
<quote>
Pal { = True;
s.1 = True;
s.1 e.2 s.1 = <Pal e.2>;
e.1 = False; }
</quote>
The notation is a little bit funny, but it should be
comprehensible. The <function args> represents
application of a function, and the empty string
isn't mentioned explicitly.
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?
Best regards,
M.
next reply other threads:[~2012-09-18 20:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-09-18 20:08 Panicz Maciej Godek [this message]
2012-09-18 22:47 ` Palindromes and pattern matching Ian Price
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAMFYt2Z5UNHWtKO-0FCi3bUoMeb9Ge6+rPhbd9Q5NH7_YotFoA@mail.gmail.com \
--to=godek.maciek@gmail.com \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).