* Palindromes and pattern matching
@ 2012-09-18 20:08 Panicz Maciej Godek
2012-09-18 22:47 ` Ian Price
0 siblings, 1 reply; 2+ messages in thread
From: Panicz Maciej Godek @ 2012-09-18 20:08 UTC (permalink / raw)
To: guile-user
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.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Palindromes and pattern matching
2012-09-18 20:08 Palindromes and pattern matching Panicz Maciej Godek
@ 2012-09-18 22:47 ` Ian Price
0 siblings, 0 replies; 2+ messages in thread
From: Ian Price @ 2012-09-18 22:47 UTC (permalink / raw)
To: Panicz Maciej Godek; +Cc: guile-user
Panicz Maciej Godek <godek.maciek@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"
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-09-18 22:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-18 20:08 Palindromes and pattern matching Panicz Maciej Godek
2012-09-18 22:47 ` Ian Price
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).