Pull up a seat, children, and prepare to be schooled in how to read
the C*R language. One thing to note before we get started, you'll be
reading the C*R sentence right to left and moving through the list
left to right.
Now let's get started...
(car '(1 2 3))
_R_ead: Begin the reading.
_A_ccept: Accept the first value, leaving 1 to consider.
_C_ull the search, returning 1.
(cdr '(1 2 3))
_R_ead: Begin the reading.
_D_eny: Deny the first value, leaving (2 3) to consider.
_C_ull: Cull the search, returning (2 3).
(cadar '(((1)(2)(3))))
_R_ead: Begin the reading.
_A_ccept: Accept the first value (a list), leaving ((1)(2)(3)) to
consider.
_D_eny: Deny the first value, leaving ((2)(3)) to consider.
_A_ccept: Accept the first value (a list), leaving (2) to consider.
_C_ull: Cull the search, returning (2).
(caaadr '(((1))((2))((3))))
_R_ead: Begin the reading.
_D_eny: Deny the first value, leaving (((2))((3))) to consider.
_A_ccept: Accept the first value (a list), leaving ((2)) to consider.
_A_ccept: Accept the first value (a list), leaving (2) to consider.
_A_ccept: Accept the first value, leaving 2 to consider.
_C_ull: Cull the search, returning 2.
(cadadr '(1 ((2) (((3))))))
_R_ead: Begin the reading.
_D_eny: Deny the first value, leaving (((2) (((3))))) to consider.
_A_ccept: Accept the first value (a list), leaving ((2) (((3)))) to
consider.
_D_eny: Deny the first value, leaving ((((3)))) to consider.
_A_ccept: Accept the first value (a list), leaving (((3))) to
consider.
_C_ull: Cull the search, returning (((3))).
Any questions?
localhost <··········@frontiernet.net> writes:
> Pull up a seat, children, and prepare to be schooled in how to read
> the C*R language. One thing to note before we get started, you'll be
> reading the C*R sentence right to left and moving through the list
> left to right.
>
> Now let's get started...
> (cadadr '(1 ((2) (((3))))))
> _R_ead: Begin the reading.
> _D_eny: Deny the first value, leaving (((2) (((3))))) to consider.
> _A_ccept: Accept the first value (a list), leaving ((2) (((3)))) to
> consider.
> _D_eny: Deny the first value, leaving ((((3)))) to consider.
> _A_ccept: Accept the first value (a list), leaving (((3))) to
> consider.
> _C_ull: Cull the search, returning (((3))).
>
> Any questions?
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__
On May 27, 5:22 am, ····@informatimago.com (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.
Andreas Davour <·······@updateLIKE.uu.HELLse> writes:
> I'm not sure if you're insane or a genius!
"Insane genius" would be good enough for me. Thank you.
:-)
Notice how the lisp function is shorter than the example typed (or
copied-pasted :-/) by localhost. I hope he wrote something similar,
for his poor fingers.
--
__Pascal Bourguignon__