[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: 'length' function for lists and cons cells?
From: |
Pascal J. Bourguignon |
Subject: |
Re: 'length' function for lists and cons cells? |
Date: |
Sat, 23 Mar 2013 13:19:34 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.2 (gnu/linux) |
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 {}.
- RE: 'length' function for lists and cons cells?, (continued)
RE: 'length' function for lists and cons cells?, Drew Adams, 2013/03/21
Message not available
Re: 'length' function for lists and cons cells?, Pascal J. Bourguignon, 2013/03/21
- Re: 'length' function for lists and cons cells?, Thorsten Jolitz, 2013/03/21
- Re: 'length' function for lists and cons cells?, Mark Skilbeck, 2013/03/22
- Re: 'length' function for lists and cons cells?, Thorsten Jolitz, 2013/03/22
- Message not available
- Re: 'length' function for lists and cons cells?,
Pascal J. Bourguignon <=
- RE: 'length' function for lists and cons cells?, Drew Adams, 2013/03/23
- Message not available
- Re: 'length' function for lists and cons cells?, Pascal J. Bourguignon, 2013/03/23
- RE: 'length' function for lists and cons cells?, Drew Adams, 2013/03/24
Message not available
Message not available