Returning an element from a list
I'm trying to learn lisp and as i'm making my first steps i got stuck.
How can i get c element form following list: (a b (c.d))
I've tried: (caar (last '(a b (c.d))))
but it returns c.d and not only c
(caar (last '(a b (c . d))))
The problem i'm trying to resolves has the list specified without spaces. Can that be done or it's a typo in the exercise?
Thanks.
LE: Uisng GNU Clisp http://clisp.cons.org/ Is it possible that the issue it's caused by the implementation?
Your code is right, it's a typo (or maybe a really bad font?) in the exercise.
In Lisp (Common Lisp and Scheme are the two I tested just now, I don't know about Clojure), [nearly] the only divisions between symbols are spaces and parentheses. Even though . is used as literal syntax for cons
, if you type '(c.d)
, you get one symbol in a list, not two symbols in a cons cell.
For example,
'c.d ; is one symbol
'(c . d) ; is two symbols in a single cons cell
'((c)d) ; is two symbols, the first in a nested list
Edit: Since you are using Common Lisp, here is the list of relevant characters and the reader algorithm. To summarise, (
and )
are terminating characters, while .
is not. CLisp is performing according to the Common Lisp spec.
I looks like you have a typo in the exercise.
The (c . d) is a cons cell shown with dotted pair notation.
Here is a link that has more on this http://c2.com/cgi/wiki?DottedPairNotation
精彩评论