From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Panicz Maciej Godek Newsgroups: gmane.lisp.guile.user Subject: Palindromes and pattern matching Date: Tue, 18 Sep 2012 22:08:11 +0200 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1347998905 16865 80.91.229.3 (18 Sep 2012 20:08:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 18 Sep 2012 20:08:25 +0000 (UTC) To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Sep 18 22:08:29 2012 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TE45p-0002Xp-9B for guile-user@m.gmane.org; Tue, 18 Sep 2012 22:08:25 +0200 Original-Received: from localhost ([::1]:52921 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TE45k-0003qJ-UH for guile-user@m.gmane.org; Tue, 18 Sep 2012 16:08:20 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:43083) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TE45f-0003pc-KS for guile-user@gnu.org; Tue, 18 Sep 2012 16:08:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TE45e-0006ju-0G for guile-user@gnu.org; Tue, 18 Sep 2012 16:08:15 -0400 Original-Received: from mail-lpp01m010-f41.google.com ([209.85.215.41]:37943) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TE45d-0006jY-Nv for guile-user@gnu.org; Tue, 18 Sep 2012 16:08:13 -0400 Original-Received: by lahd3 with SMTP id d3so179459lah.0 for ; Tue, 18 Sep 2012 13:08:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=vmmZG6bcC+63hymqSGBdVPYlTqJb97wSuWMhSciUSwo=; b=wqhvrp9G3XX6gba6v2fWP9ZDJ+hV60b+BTKjcN1dadv68Tz81YoSMogsbfPtOqyihF u5fIaH3HRx2avoNAuiOrRYBf3T6W7lTqHTlr6MBXT6XDXfVlV+6g2bot5C6EjbVXrnkv Wis2Z5et1+sk2NImfeVnaaE/ohJVqieKUJzGX72Vbnnhw14NEU8NAqCsrL6cBSXJWV0M DGMK/BErpXbilqn4twv9zbWpffSCoOcBQOBCGTqt/VOA8BeC56ZrJr8kfOZEP++Pn/md Hv0/yc8/NbPR/wYwfCZJ3grcPrNVeqWRjFlFPHETUEENaCrdcugsGMZ/jT9CeSaHd3D+ Uv4Q== Original-Received: by 10.112.47.100 with SMTP id c4mr305018lbn.49.1347998891955; Tue, 18 Sep 2012 13:08:11 -0700 (PDT) Original-Received: by 10.152.112.106 with HTTP; Tue, 18 Sep 2012 13:08:11 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.215.41 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:9583 Archived-At: 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: 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. 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: Pal { = True; s.1 = True; s.1 e.2 s.1 = ; e.1 = False; } The notation is a little bit funny, but it should be comprehensible. The 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.