From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ian Price Newsgroups: gmane.lisp.guile.user Subject: Re: Palindromes and pattern matching Date: Tue, 18 Sep 2012 23:47:14 +0100 Message-ID: <87mx0nkky5.fsf@googlemail.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-2022-jp X-Trace: ger.gmane.org 1348008450 30859 80.91.229.3 (18 Sep 2012 22:47:30 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 18 Sep 2012 22:47:30 +0000 (UTC) Cc: guile-user@gnu.org To: Panicz Maciej Godek Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Sep 19 00:47:34 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 1TE6Zq-0005VX-Hx for guile-user@m.gmane.org; Wed, 19 Sep 2012 00:47:34 +0200 Original-Received: from localhost ([::1]:45664 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TE6Zm-0000Z1-1H for guile-user@m.gmane.org; Tue, 18 Sep 2012 18:47:30 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:53921) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TE6Zg-0000Yv-Uf for guile-user@gnu.org; Tue, 18 Sep 2012 18:47:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TE6Zf-00060l-VT for guile-user@gnu.org; Tue, 18 Sep 2012 18:47:24 -0400 Original-Received: from mail-bk0-f41.google.com ([209.85.214.41]:63604) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TE6Zf-00060h-LS for guile-user@gnu.org; Tue, 18 Sep 2012 18:47:23 -0400 Original-Received: by bkcjc3 with SMTP id jc3so170849bkc.0 for ; Tue, 18 Sep 2012 15:47:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=CGI1+a4i+6eZOeBnSr6/HmmvPCLaGn7J09JZ4/7iMps=; b=c8FQeJ/jFTMGr4PdyY0Gevx+1Dt2FMfYqTVSgHkY0reb3ctZOnRwXzS5UdSzXdlmQ6 9jdBItpPZO451AUYIDAyJrMNP4648Cwx/2WF2xKm/ACSiMNkRH8wFI0oRjpALqbTosPg Dj+7xEdmckdPhy5rFNdQ+Fe76F6e+uHammvncBb/VNT30Xbc2vli71Wv2txInLV1gH1L iM/MEX7mZQvVaWaMTbzEl8OpjhkqD+So8OVDkd5N4Dim4x36bMlzbtDRmEdm/gsGxJdf /JlXTN9BChIbFLIhpcMv64XkVZ7kp65XsRemsQEji6+s0hRPDcQcOS86lRQ8Z6ecUDou Fe9g== Original-Received: by 10.204.145.82 with SMTP id c18mr607084bkv.133.1348008441979; Tue, 18 Sep 2012 15:47:21 -0700 (PDT) Original-Received: from Kagami (host81-159-200-112.range81-159.btcentralplus.com. [81.159.200.112]) by mx.google.com with ESMTPS id a17sm659175bkw.5.2012.09.18.15.47.18 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 18 Sep 2012 15:47:20 -0700 (PDT) In-Reply-To: (Panicz Maciej Godek's message of "Tue, 18 Sep 2012 22:08:11 +0200") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.214.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:9584 Archived-At: Panicz Maciej Godek 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"