From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: 'length' function for lists and cons cells? Date: Sat, 23 Mar 2013 13:19:34 +0100 Organization: Informatimago Message-ID: <877gkypauh.fsf@kuiper.lan.informatimago.com> References: <87sj3opjfw.fsf@kuiper.lan.informatimago.com> <87ip4kkvl8.fsf@gmail.com> <20130322072749.GD13119@earth> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1364051946 9599 80.91.229.3 (23 Mar 2013 15:19:06 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 23 Mar 2013 15:19:06 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Mar 23 16:19:33 2013 Return-path: Envelope-to: geh-help-gnu-emacs@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 1UJQEC-0008H3-7c for geh-help-gnu-emacs@m.gmane.org; Sat, 23 Mar 2013 16:19:28 +0100 Original-Received: from localhost ([::1]:41921 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UJQDo-0004Aj-LO for geh-help-gnu-emacs@m.gmane.org; Sat, 23 Mar 2013 11:19:04 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 70 Original-X-Trace: individual.net +TJ/2dODqGq6XyUdqV+NxwqnwFRhqt8oxagTuxbMA+cHlbMhpX Cancel-Lock: sha1:ZjI1MzUzNTUzMzYzN2JhNzE3YTI4OGUxNjE0MjdjODA3ZDUxMjY1OQ== sha1:YvSIyTEJyG5SgTTIKul4ayOFVcA= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:197414 X-Mailman-Approved-At: Sat, 23 Mar 2013 11:17:52 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:89686 Archived-At: Thorsten Jolitz writes: > So if both, true lists and cons cells, are processed in a 'dolist' or proper-lists, not true lists. They all are true lists, ie. cons cells or nil. > 'mapc', how do you get that second string element without writing > something like > > ,---------------------------------- > | (defun tj/act-conditional (lst) > | (if (cdr (last lst)) > | (cdr lst) > | (cadr lst))) > | > | (tj/act-conditional '("a" . "1")) > | "1" > | > | (tj/act-conditional '("a" "1")) > | "1" > `---------------------------------- This is like asking how do you get the second digit of 123 and "123" without writing something like: (defun second-digit (thing) (etypecase thing (integer (mod (truncate thing (expt 10 (truncate (1- (log thing 10))))) 10)) (string (- (aref thing 1) ?0)))) (second-digit 12345) --> 2 (second-digit "1234") --> 2 The point is that lisp is a typed programming language. If a function expects data of type proper-list, then you don't pass it circular lists or dotted lists or strings or numbers, or whatever else. If you want to write a function that takes a or type argument, then you have to do the type casing yourself, or use other functions that take the same or type. (defun second-element (thing) (check-type thing (or null cons)) (typecase thing (null nil) (cons (typecase (cdr thing) (cons (cadr thing)) (t (cdr thing)))))) (second-element '()) --> nil (second-element '(1)) --> nil (second-element '(1 2)) --> 2 (second-element '(1 2 3)) --> 2 (second-element '(1 2 . 3)) --> 2 (second-element '(1 . 2)) --> 2 (second-element "123") error: (wrong-type-argument (or null cons) "123" thing) It is up to you to define what type of data you want to work with. -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}.