all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: 'length' function for lists and cons cells?
Date: Sat, 23 Mar 2013 13:19:34 +0100	[thread overview]
Message-ID: <877gkypauh.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.22632.1363946271.855.help-gnu-emacs@gnu.org

Thorsten Jolitz <tjolitz@gmail.com> 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 {}.


  parent reply	other threads:[~2013-03-23 12:19 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.22586.1363890109.855.help-gnu-emacs@gnu.org>
2013-03-21 20:49 ` 'length' function for lists and cons cells? Pascal J. Bourguignon
2013-03-22  2:38   ` Thorsten Jolitz
2013-03-22  7:27     ` Mark Skilbeck
2013-03-22  9:57       ` Thorsten Jolitz
     [not found]       ` <mailman.22632.1363946271.855.help-gnu-emacs@gnu.org>
2013-03-23 12:19         ` Pascal J. Bourguignon [this message]
2013-03-23 15:35           ` Drew Adams
     [not found]           ` <mailman.22706.1364052941.855.help-gnu-emacs@gnu.org>
2013-03-23 15:52             ` Pascal J. Bourguignon
2013-03-24  5:20               ` Drew Adams
     [not found]   ` <mailman.22618.1363919920.855.help-gnu-emacs@gnu.org>
2013-03-22  7:45     ` Barry Margolin
2013-03-23 11:31     ` Pascal J. Bourguignon
2013-03-21 18:21 Thorsten Jolitz
2013-03-21 20:02 ` Stephen Berman
2013-03-21 21:58   ` Thorsten Jolitz
2013-03-21 22:05     ` Drew Adams
     [not found]   ` <mailman.22591.1363903134.855.help-gnu-emacs@gnu.org>
2013-03-23 11:24     ` Pascal J. Bourguignon
2013-03-21 20:10 ` Drew Adams
2013-03-21 22:09   ` Thorsten Jolitz
     [not found] ` <mailman.22588.1363896620.855.help-gnu-emacs@gnu.org>
2013-03-25 15:24   ` duthen.cnv
2013-03-25 20:02     ` Pascal J. Bourguignon

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=877gkypauh.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.