Reputation: 3209
I want to use the dot (.) as a symbol, like a
or b
.
I found that I can do so by quoting and escaping the dot. However, when the dot is displayed on the screen, it is surrounded by vertical bars:
'\.
=> |.|
How can I get the dot displayed without the vertical bars?
Update: Thank you jkiiski, using format
works great. Here's why I am doing this: For my own education, I wrote a function to convert a list in list notation to the equivalent list in dot notation. Thanks to your help, now it works great:
(defun list-notation-to-dot-notation (lst)
(cond ((atom lst) lst)
((null (cdr lst)) (list (list-notation-to-dot-notation (car lst)) '\. 'NIL))
(t (list (list-notation-to-dot-notation (car lst)) '\. (list-notation-to-dot-notation (cdr lst))))))
(defun list-2-dot (lst)
(format t "~a" (list-notation-to-dot-notation lst)))
(list-2-dot '(a))
=> (A . NIL)
(list-2-dot '(a b))
=> (A . (B . NIL))
(list-2-dot '((a) b))
=> ((A . NIL) . (B . NIL))
(list-2-dot '(a (b) c))
=> (A . ((B . NIL) . (C . NIL)))
(list-2-dot '(a b (c)))
=> (A . (B . ((C . NIL) . NIL)))
(list-2-dot '((a) (b) (c)))
=> ((A . NIL) . ((B . NIL) . ((C . NIL) . NIL)))
Upvotes: 3
Views: 1622
Reputation: 11
I find this to be the simplest solution to the case:
(defun list-notation-to-dot-notation (list)
(cond ((atom list) (format t "~S" list))
(t (format t "(")
(list-notation-to-dot-notation (car list))
(format t " . ")
(list-notation-to-dot-notation (cdr list))
(format t ")"))))
as found in Common Lisp: A Gentle Introduction to Symbolic Computation by professor David S. Touretzky.
Assume you have a function, list-notation-to-dot-notation
, that you know it converts lists from list notation to dot notation, i.e.:
car
of the list (car list)
.
cdr
of the list (cdr list)
Then, all you have to do is to recursively call that function on the car
and on the cdr
to convert them to dot notation, and putting a dot in between.
Personally, I find the base condition to be a bit tricky.
As you can't simply write (cond ((atom list)
list
)) ...
as it wouldn't print atoms correctly (at all!), perhaps due to the fact the return value will be passed to format
.
Upvotes: 1
Reputation: 29
Just FYI, the lisp printer will always print the dot (.) symbol as |.|
because the dot is associated with special, lisp reader defined rules. Unless it is part of the name like foo.bar
, the reader will always assume you meant a dotted list of some kind.
Upvotes: -1
Reputation: 8411
This would be a bit cleaner way of achieving the same result:
(defun print-dot-notation (list &optional (stream *standard-output*))
(if (atom list)
(format stream "~s" list)
(format stream "(~a . ~a)"
(print-dot-notation (car list) nil)
(print-dot-notation (cdr list) nil))))
(print-dot-notation '(a (b) c))
; (A . ((B . NIL) . (C . NIL)))
No need to create extra lists or use a symbol for the dot.
Upvotes: 5