Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:43:30 |
Calls: | 583 |
Files: | 1,139 |
D/L today: |
179 files (27,921K bytes) |
Messages: | 111,617 |
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)