Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: chaining functions ?

27 views
Skip to first unread message

WJ

unread,
Mar 29, 2015, 11:38:33 PM3/29/15
to
Pascal Bourguignon wrote:

> Finally if you want to do a lot of "unnatural" operations on lists
> (the "natural" list operations in lisp work on the head (car) and the
> rest (cdr) of a list, with the common idiom of push / nreverse, etc),
> then you need to introduce a new abstract data type for which you'll
> be able to write optimized operations.
>
> For example, with:
>
> (defstruct htlist head tail)
>
> (defun htelements (htlist)
> (htlist-head htlist))
>
> (defun htpush (item htlist)
> (push item (htlist-head htlist))
> (when (null (htlist-tail htlist))
> (setf (htlist-tail htlist) (htlist-head htlist)))
> htlist)
>
> (defun htpush-at-end (item htlist)
> (setf (cdr (htlist-tail htlist)) (cons item nil))
> (when (null (htlist-head htlist))
> (setf (htlist-head htlist) (htlist-tail htlist)))
> (setf (htlist-tail htlist) (cdr (htlist-tail htlist)))
> htlist)
>
> you can easily (in O(1)) add elements at both ends:
>
> [60]> (defparameter *mylist* (make-htlist))
> *MYLIST*
> [61]> (loop :for i :from 1 :to 10
> :do (if (oddp i) (htpush i *mylist*) (htpush-at-end i *mylist*))
> :finally (print (htelements *mylist*)))
>
> (9 7 5 3 1 2 4 6 8 10)


Gauche Scheme:

(use util.queue)

(define myq (make-queue))
(dotimes (i 10)
((if (odd? i) queue-push! enqueue!) myq i))
(dequeue-all! myq)

===>
(9 7 5 3 1 0 2 4 6 8)

WJ

unread,
Jan 14, 2016, 6:11:24 AM1/14/16
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
WJ wrote:

> Pascal Bourguignon wrote:
>
> > Finally if you want to do a lot of "unnatural" operations on lists
> > (the "natural" list operations in lisp work on the head (car) and the
> > rest (cdr) of a list, with the common idiom of push / nreverse, etc),
> > then you need to introduce a new abstract data type for which you'll
> > be able to write optimized operations.
> >
> > For example, with:
> >
> > (defstruct htlist head tail)
> >
> > (defun htelements (htlist)
> > (htlist-head htlist))
> >
> > (defun htpush (item htlist)
> > (push item (htlist-head htlist))
> > (when (null (htlist-tail htlist))
> > (setf (htlist-tail htlist) (htlist-head htlist)))
> > htlist)
> >
> > (defun htpush-at-end (item htlist)
> > (setf (cdr (htlist-tail htlist)) (cons item nil))
> > (when (null (htlist-head htlist))
> > (setf (htlist-head htlist) (htlist-tail htlist)))
> > (setf (htlist-tail htlist) (cdr (htlist-tail htlist)))
> > htlist)
> >
> > you can easily (in O(1)) add elements at both ends:
> >
> > [60]> (defparameter mylist (make-htlist))
> > MYLIST
> > [61]> (loop :for i :from 1 :to 10
> > :do (if (oddp i) (htpush i mylist) (htpush-at-end i mylist))
> > :finally (print (htelements mylist)))
> >
> > (9 7 5 3 1 2 4 6 8 10)
>
>
> Gauche Scheme:
>
> (use util.queue)
>
> (define myq (make-queue))
> (dotimes (i 10)
> ((if (odd? i) queue-push! enqueue!) myq i))
> (dequeue-all! myq)
>
> ===>
> (9 7 5 3 1 0 2 4 6 8)

MatzLisp (Ruby):

result = []
1.upto(10){|i| result.send(i.odd? ? :unshift : :push, i)}
result

[9, 7, 5, 3, 1, 2, 4, 6, 8, 10]

--
The first step in liquidating a people is to erase its memory. Destroy
its books, its culture, its history. Then you have somebody write new
books, manufacture a new culture, invent a new history. Before long
the nation will begin to forget what it is and what it was.
--- Milan Kundera, _The Book of Laughter and Forgetting_
0 new messages