• Re: drop it

    From B. Pym@21:1/5 to Chris Russell on Thu Aug 8 16:42:46 2024
    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.dyndns.org

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

    newLISP

    (define (remove-nth n lst , i)
    (clean (fn() (= 0 (mod (++ i) n))) lst))

    (remove-nth 3 '(a b c d e f g h i j k))

    (a b d e g h j k)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to All on Wed Jun 18 22:23:24 2025
    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)))

    Gauche Scheme

    (define (drop seq n)
    (append-map
    (lambda (x i) (if (zero? (mod i n)) () (list x)))
    seq
    (lrange 1)))

    (drop '(#f #f X #f #f Y and so on) 3)
    ===>
    (#f #f #f #f and so)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Wed Jul 30 04:06:13 2025
    B. Pym 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)))

    Gauche Scheme

    (define (drop seq n)
    (append-map
    (lambda (x i) (if (zero? (mod i n)) () (list x)))
    seq
    (lrange 1)))

    (drop '(#f #f X #f #f Y and so on) 3)
    ===>
    (#f #f #f #f and so)

    Gauche Scheme

    (use srfi-1) ;; circular-list

    (define (remove-nth n lst)
    (let1 bools (apply circular-list `(,@(make-list (- n 1) #t) #f))
    (append-map
    (lambda(b x) (if b (list x) ()))
    bools
    lst)))

    (remove-nth 3 '(a b c #f e f g h i j k))
    ===>
    (a b #f e g h j k)


    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)