anti-select elements of a list

Q&A's, tips, howto's

anti-select elements of a list

Postby lyl » Fri Dec 21, 2018 2:18 am

The function "select" can be used to select multi-elements of a list at one time, for example:
Code: Select all
(select '(0 1 2 3 4) '(1 2))
->(1 2)

Yet what I want is to get all elements other than those "selceted", that is to say, I want to obtain (0 3 4) from above example.
I tried with the function "filter" like this:
Code: Select all
(filter (not (find $idx '(1 2))) '(0 1 2 3 4))

but it fails because "filter" does not support $idx.

So, is there a better way to anti-select elements in a list at one time by another list which assign positions those elements to be deleted?
lyl
 
Posts: 10
Joined: Sun Mar 25, 2018 5:00 am

Re: anti-select elements of a list

Postby newBert » Fri Dec 21, 2018 10:37 am

Here is a solution (probably not the only one, nor the best) :
Code: Select all
newLISP v.10.7.5 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h

> (select '(0 1 2 3 4) (difference (index true? '(0 1 2 3 4)) '(1 2)))
(0 3 4)
> ;; maybe clearer:
>
(let (lst '(0 1 2 3 4))
  (select lst (difference (index true? lst) '(1 2))))

(0 3 4)
>
BertrandnewLISP v.10.7.5 64-bit on Linux (Bodhi Linux 4.5)
newBert
 
Posts: 134
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: anti-select elements of a list

Postby lyl » Fri Dec 21, 2018 2:45 pm

newBert wrote:Here is a solution (probably not the only one, nor the best) :
Code: Select all
newLISP v.10.7.5 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h

> (select '(0 1 2 3 4) (difference (index true? '(0 1 2 3 4)) '(1 2)))
(0 3 4)
> ;; maybe clearer:
>
(let (lst '(0 1 2 3 4))
  (select lst (difference (index true? lst) '(1 2))))

(0 3 4)
>


If the lst contains nil(0 1 2 3 nil), how to do?
lyl
 
Posts: 10
Joined: Sun Mar 25, 2018 5:00 am

Re: anti-select elements of a list

Postby fdb » Fri Dec 21, 2018 4:23 pm

Code: Select all
(define (unselect lst sel)
  (clean (fn(x) (member x sel)) lst))

(unselect '( 0 1 2 3 nil) '(1 2))

(0 3 nil)
fdb
 
Posts: 39
Joined: Sat Nov 09, 2013 8:49 pm

Re: anti-select elements of a list

Postby fdb » Fri Dec 21, 2018 4:39 pm

or just

Code: Select all
(difference '(0 1 2 3 nil) '(1 2))

(0 3 nil)


or am I misunderstanding something?
fdb
 
Posts: 39
Joined: Sat Nov 09, 2013 8:49 pm

Re: anti-select elements of a list

Postby lyl » Sat Dec 22, 2018 1:18 am

@fdb '(1 2) in my example means the position of elements in lst, not elements themselves. I think I give a poor example.
lyl
 
Posts: 10
Joined: Sun Mar 25, 2018 5:00 am

Re: anti-select elements of a list

Postby ralph.ronnquist » Sat Dec 22, 2018 4:21 am

So you might be looking for something like the following:
Code: Select all
(define (drop lst idx) (let (n -1) (clean (fn (x) (member (inc n) idx)) lst)))

> (drop '(5 4 3 2 1) '(1 2))
(5 2 1)
ralph.ronnquist
 
Posts: 181
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia

Re: anti-select elements of a list

Postby newBert » Sat Dec 22, 2018 12:36 pm

lyl wrote:
newBert wrote:Here is a solution (probably not the only one, nor the best) :
Code: Select all
newLISP v.10.7.5 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h

> (select '(0 1 2 3 4) (difference (index true? '(0 1 2 3 4)) '(1 2)))
(0 3 4)
> ;; maybe clearer:
>
(let (lst '(0 1 2 3 4))
  (select lst (difference (index true? lst) '(1 2))))

(0 3 4)
>


If the lst contains nil(0 1 2 3 nil), how to do?


I replaced 0 1 2 3 4... with a b c d e... for clarity.
Code: Select all
>
(let (lst '(a b c d nil f))
  (select lst (difference (sequence 0 (- (length lst) 1)) '(1 2))))

(a d nil f)
>
Last edited by newBert on Sat Dec 22, 2018 12:42 pm, edited 1 time in total.
BertrandnewLISP v.10.7.5 64-bit on Linux (Bodhi Linux 4.5)
newBert
 
Posts: 134
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Re: anti-select elements of a list

Postby fdb » Sat Dec 22, 2018 12:38 pm

or use select and the difference of the selected indexes:

Code: Select all
(define (unselect lst sel)
    (select lst (difference (sequence 0 (dec (length lst))) sel)))

> (unselect '(5 4 3 2 1) '(1 2))
(5 2 1)
fdb
 
Posts: 39
Joined: Sat Nov 09, 2013 8:49 pm

Re: anti-select elements of a list

Postby lyl » Sat Dec 22, 2018 12:43 pm

Many thanks all of you for these beautiful solutions!!
lyl
 
Posts: 10
Joined: Sun Mar 25, 2018 5:00 am


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 4 guests

cron