[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: different LOOP results

William James

2/2/2016 1:49:00 AM

Pierre R. Mai wrote:

> Kent M. Pitman <pitman@world.std.com> writes:
>
> > edi@agharta.de (Dr. Edmund Weitz) writes:
> >
> > > PS: For the sake of completeness, here's my take on LDIFF
> > >
> > > (defun subdivide (list &key (incr 1))
> > > (loop for sublist on list by #'(lambda (list)
> > > (nthcdr incr list))
> > > collect (ldiff sublist (nthcdr incr sublist))))
> > >
> > > As far as I can see this version is also in the O(2n) ballpark which I
> > > think you anticipated.
> >
> > I was thinking something more like the following. Doing two identical
> > nthcdrs should make you suspicious...
> >
> > (defun subdivide (list &optional (increment 1))
> > (loop with sublist = list
> > while sublist
> > for next = (nthcdr increment sublist)
> > collect (ldiff sublist next)
> > do (setq sublist next)))
>
> Actually I think you are not allowed to do it this way: LOOP requires
> that variable binding clauses appear before main-clauses like while.
> The MIT loop code does allow it, and deals with it correctly, and most
> independent implementations seem to also allow it, though some warn
> about this. But sadly the standard doesn't require this (I think
> there were reservations about the exact semantics of mixing and
> matching such clauses). I say sadly, because this forces portable
> code to sometimes be much more convoluted than would otherwise have
> been necessary. Interleaving stepping and checking clauses is a very
> common idiom, which could have been supported by LOOP.

MatzLisp (Ruby):

The hard way:

def subdivide(array, stride=1)
(0 ... array.size).step(stride).map{|i| array[i,stride]}
end

subdivide [0,1,2,3,4], 2
==>[[0, 1], [2, 3], [4]]
subdivide [0,1,2,3,4], 3
==>[[0, 1, 2], [3, 4]]
subdivide [0,1,2,3,4]
==>[[0], [1], [2], [3], [4]]

The easy way:

[0,1,2,3,4].each_slice(2).to_a
==>[[0, 1], [2, 3], [4]]

--
Amazon bans book. After nearly a month on the site, all traces of the book and
its 80 reviews have been removed.
http://jamesfetzer.blogspot.com/2015/11/debunking-sandy-hook-debunk...
https://www.youtube.com/watch?v=E...