newLISP Fan Club

Forum => newLISP in the real world => Topic started by: lyl on December 20, 2018, 06:18:41 PM

Title: anti-select elements of a list
Post by: lyl on December 20, 2018, 06:18:41 PM
The function "select" can be used to select multi-elements of a list at one time, for example:
(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:
(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?
Title: Re: anti-select elements of a list
Post by: newBert on December 21, 2018, 02:37:09 AM
Here is a solution (probably not the only one, nor the best) :
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)
>
Title: Re: anti-select elements of a list
Post by: lyl on December 21, 2018, 06:45:05 AM
Quote from: "newBert"Here is a solution (probably not the only one, nor the best) :
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?
Title: Re: anti-select elements of a list
Post by: fdb on December 21, 2018, 08:23:22 AM

(define (unselect lst sel)
  (clean (fn(x) (member x sel)) lst))

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

(0 3 nil)
Title: Re: anti-select elements of a list
Post by: fdb on December 21, 2018, 08:39:58 AM
or just



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

(0 3 nil)


or am I misunderstanding something?
Title: Re: anti-select elements of a list
Post by: lyl on December 21, 2018, 05:18:53 PM
@fdb '(1 2) in my example means the position of elements in lst, not elements themselves. I think I give a poor example.
Title: Re: anti-select elements of a list
Post by: rrq on December 21, 2018, 08:21:25 PM
So you might be looking for something like the following:
(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)
Title: Re: anti-select elements of a list
Post by: newBert on December 22, 2018, 04:36:28 AM
Quote from: "lyl"
Quote from: "newBert"Here is a solution (probably not the only one, nor the best) :
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.
>
(let (lst '(a b c d nil f))
  (select lst (difference (sequence 0 (- (length lst) 1)) '(1 2))))

(a d nil f)
>
Title: Re: anti-select elements of a list
Post by: fdb on December 22, 2018, 04:38:52 AM
or use select and the difference of the selected indexes:



(define (unselect lst sel)
(select lst (difference (sequence 0 (dec (length lst))) sel)))

> (unselect '(5 4 3 2 1) '(1 2))
(5 2 1)
Title: Re: anti-select elements of a list
Post by: lyl on December 22, 2018, 04:43:29 AM
Many thanks all of you for these beautiful solutions!!