Re: Push/Pop for list tail
- From: William James <w_a_x_man@xxxxxxxxx>
- Date: Thu, 28 Feb 2008 21:54:19 -0800 (PST)
On Feb 28, 8:42 pm, vanekl <va...@xxxxxxx> wrote:
Gene wrote:
snip
I understand, but can't stop. Here is the revision, with some
attention to macro hygiene, also. I was off by one pointer in
previous post. Accessor is just (cdr ), not (cdar ). Here we go...
(defmacro deque-push (item fore back)
(let ((x (gensym "x"))
(ring (gensym "ring")))
`(let* ((,x ,item)
(,ring (cons (cons (cons (cons t ,x) nil) ,x) ,fore)))
(setf (caaaar ,ring) ,ring)
What kind of a debased language has an abomination like
"caaaar"? Let's make it even more powerful and add
"caaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar"!
(if ,fore
(setf (cdaar ,fore) (caar ,ring))
(setf ,back (caar ,ring)))
(setf ,fore ,ring))))
(defmacro deque-pop (fore back)
`(prog1 (cdar ,fore)
(pop ,fore)
(if ,fore
(setf (cdaar ,fore) nil)
(setf ,back nil))))
CL-USER> (setq x nil y nil)
(deque-push 'b x y)
(deque-push 'a y x)
(deque-push 'c x y)
(deque-push 'd x y)
#1=((#2=((#1# . D)) . D) .
#3=((#4=((#3# . C) . #2#) . C) . #5=((#6=(# . #4#) . B) . #7=((# .
A)))))
CL-USER> (dolist (p x) (print (cdr p)))
D
C
B
A
NIL
CL-USER> (dolist (p y) (print (cdr p)))
A
B
C
D
NIL
CL-USER> (deque-pop x y)
D
CL-USER> (deque-pop y x)
A
CL-USER>
works like a charm, and i managed not to do anything too
stupid this time and fall into an endless loop. thanks.
i learn more new things w/ cl than w/ any other language
but i cant think so clearly like i used 2
To attain knowledge, add things every day.
To attain wisdom, remove things every day.
--- Lao-tse
All this for push and pop? It's no wonder that Paul Graham
said that COMMON-LISP sucks.
Programmers are always surrounded by complexity; we cannot avoid
it.... If our basic tool, the language in which we design and
code our programs, is also complicated, the language itself
becomes part of the problem rather than part of its solution.
--- C. A. R. Hoare (1980 Turing Award Lecture)
NewLisp:
; inserting in front
(set 'pList '(b c)) → (b c)
(push 'a pList) → a
pList → (a b c)
; insert at index
(push "hello" pList 2) → "hello"
pList → (a b "hello" c)
; optimized appending at the end
(push 'z pList -1) → z
pList → (a b "hello" c z)
(set 'pList '((f g) a b c "hello" d e 10))
(pop pList) → (f g)
(pop pList) → a
plist → (b c "hello" d e 10)
(pop pList 3) → d
(pop pList 100) → 10
pList → (b c "hello" e)
(pop pList -1) → e
pList → (b c "hello")
(pop pList -2) → c
pList → (b "hello")
.
- Follow-Ups:
- Re: Push/Pop for list tail
- From: Raffael Cavallaro
- Re: Push/Pop for list tail
- From: Pascal J. Bourguignon
- Re: Push/Pop for list tail
- References:
- Push/Pop for list tail
- From: vanekl
- Re: Push/Pop for list tail
- From: Kaz Kylheku
- Re: Push/Pop for list tail
- From: vanekl
- Re: Push/Pop for list tail
- From: Kaz Kylheku
- Re: Push/Pop for list tail
- From: vanekl
- Re: Push/Pop for list tail
- From: Gene
- Re: Push/Pop for list tail
- From: vanekl
- Re: Push/Pop for list tail
- From: Gene
- Re: Push/Pop for list tail
- From: vanekl
- Re: Push/Pop for list tail
- From: Gene
- Re: Push/Pop for list tail
- From: vanekl
- Push/Pop for list tail
- Prev by Date: Re: Pocket Reference for Common Lisp
- Next by Date: Re: Why Lisp?
- Previous by thread: Re: Push/Pop for list tail
- Next by thread: Re: Push/Pop for list tail
- Index(es):
Relevant Pages
|
|