[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: drop it

William James

11/16/2015 8:05:00 PM

Chris Russell wrote:

> > > > Drop every N'th element from a list.
> > > > Example:
> > > > * (drop '(a b c d e f g h i k) 3)
> > > > (A B D E G H K)
> >
> > > I'm guilty responding to a spammer, who doesn't help Lisp or OCaml by
> > > posting wrong statements about it, so I'll solve your problem:
> >
> > > (defun drop (list n)
> > > (loop for item in list
> > > for i = 1 then (1+ i)
> > > when (/= (mod i n) 0) collect item))
> >
> > > Of course, in Haskell I can write it like this:
> >
> > > myDrop list n=[list!!x|x<-[0..length list-1], mod (1+x) n/=0]
> >
> > > and I'm sure this is not the shortest solution. But for me it is more
> > > difficult to develop pure functional algorithms than using Lisp.
> >
> > It is not that difficult.
> >
> > (defun drop (list n)
> > (labels ((drop-aux (list i)
> > (cond ((null list) nil)
> > ((= i 1) (drop-aux (rest list) n))
> > (t (cons (first list)
> > (drop-aux (rest list) (1- i)))))))
> > (drop-aux list n)))
> >
> > There are many extra points to get for a version using 'Series'.
> > A few extra points for a tail-recursive version.
> >
> > Or you can take this one:
> >
> > (defun every-nth (n)
> > (let ((i n))
> > (lambda ()
> > (prog1 (= i 1)
> > (decf i)
> > (when (zerop i)
> > (setf i n))))))
> >
> > Above returns a function that returns T for every n-th call.
> >
> > (defun drop (list n)
> > (remove-if (every-nth n) list))
> >
> > --http://lispm....
>
> Obligatory 1 line solution using map__:
>
> (defun remove-nth(n list)
> (mapcan (let ((tick 0))(lambda(x) (when (/= 0 (rem (incf tick) n))
> (list x)))list))


MatzLisp (Ruby):

def drop_nths(n,seq)
seq.each_with_index.grep(proc{|x,i| (i+1)%n>0}){|x,i| x}
end

drop_nths 3,('a'..'z')
==>["a", "b", "d", "e", "g", "h", "j", "k", "m", "n", "p", "q", "s",
"t", "v", "w", "y", "z"]

Another way:

def drop_nths(n,seq)
seq.each_with_index.flat_map{|x,i| (i+1)%n>0 ? x : []}
end

Another way:

def drop_nths(n,seq)
seq.each_slice(n).map{|x| x[0..-2]}.flatten
end

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... They've already said that everybody from
Syria can come to Sweden.... [T]hey are actually thinking of commandeering
people's vacation homes because they need more housing for immigrants.
--- Dr. Kevin MacDonald (http://lnrlive.com/tpc/tpc201...)
1 Answer

The Peeler

6/6/2013 10:04:00 PM

0

On Thu, 6 Jun 2013 23:33:47 +0200, The Peeler
<finishingoff@themoronicRevd.invalid> wrote:

>On Thu, 06 Jun 2013 04:40:03 -0700, The Rectum, the resident psychopath of
>sci and scj, FAKING his time zone again and IMPERSONATING his master, The
>Peeler, wrote:
>
>> On Thu, 6 Jun 2013 04:38:39 +0000 (UTC), NoSpamAtAll
>> <peeling@degenerate.Griks> wrote:
>>
>> Repetition, shiteating Jewish person!
>
>...says the

Repetition shiteating, Grik anus!