Re: Push/Pop for list tail



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")
.



Relevant Pages

  • Re: CollabRx seeks brilliant engineers for an excellent e-science adventure
    ... belief that lisp programmers are smarter/better. ... Java or PHP programmers. ... a type of language that attracts a personality that meets my perceptions ...
    (comp.lang.lisp)
  • Re: Question concerning object-oriented programming
    ... programming language, not his. ... there has usually been a toString. ... I know it's "unrealistic" to expect actual programmers to do this, ... tell me what a dog is. ...
    (comp.programming)
  • Re: Why do some people hate java?
    ... programming language like Java to exist, or if Java must exists, there's ... The majority of programmers at the time were using C++, ... and other resources that are just as important to ...
    (comp.lang.java.programmer)
  • Re: FORTH levels
    ... Most working on a collaborative project do not choose the programming language they are using: it is thrust upon them by the needs of the collaboration. ... When Iverson and Hui came up with J-- in part to remove APL's special character set and make it more "user friendly" not much of a community formed around it. ... And people who are by not by any reasonable stretch of the term "programmers" seem to take to Perl. ... But RPN does not require a visible stack, any more than any language requires a visible stack to rebuild its semantic trees from its flat expression. ...
    (comp.lang.forth)
  • Re: Python syntax in Lisp and Scheme
    ... > As for non-professional programmers, the next question is whether a good ... > language for them will ever be anything more than a language for them. ... Python to grow in any way that would "destroy the base simplicity"; ...
    (comp.lang.python)