Re: Reading CAR, CDR, and other C*R Sentances



On May 27, 5:22 am, p...@xxxxxxxxxxxxxxxxx (Pascal J. Bourguignon)
wrote:
Like this?

(defun read-cxr (form)
(destructuring-bind (op arg) form
(let ((sop (string-upcase op)))
(assert (and (char= #\C (aref sop 0))
(char= #\R (aref sop (1- (length sop))))
(<= 3 (length sop))))
(loop
:with result = (eval arg)
:for i :from (1- (length sop)) :downto 1
:initially (format t "~S~%_R_ead: Begin the reading.~%" form)
:do (ecase (aref sop i)
((#\A)
(setf result (car result))
(format t "_A_ccept: Acept the first value (a ~(~A~)), leaving: ~S to consider.~%"
(type-of result) result))
((#\D)
(setf result (cdr result))
(format t "_D_eny: Deny the first value, leaving: ~S to consider.~%" result)))
:finally (format t "_C_ull: Cull the search, returning ~S.~%" result)))))

(read-cxr '(cadadr '(1 ((2) (((3)))))))

(CADADR '(1 ((2) (((3))))))
_R_ead: begin the reading.
_D_eny: Deny the first value, leaving: (((2) (((3))))) to consider.
_A_ccept: Acept the first value (a cons), leaving: ((2) (((3)))) to consider.
_D_eny: Deny the first value, leaving: ((((3)))) to consider.
_A_ccept: Acept the first value (a cons), leaving: (((3))) to consider.
_C_ull: Cull the search, returning (((3))).
NIL

--
__Pascal Bourguignon__

Perfect! I'm always amazed at the things that can be done with lisp.

By the way, I like the use of keywords for loop's parameters as it's
much easier to read. I'll invoke the greatest form of flattery and
imitate this from here forward.
.