Author Topic: vl-some function  (Read 8364 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
vl-some function
« on: January 26, 2014, 03:40:05 AM »
Hello guys .

I hope someone of you experts can explain this function ( vl-some ) to me because I saw this function used a lot and I read about it in the Help document but can not understand it correctly .

Can you give examples please ?

Thanks in advance .  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vl-some function
« Reply #1 on: January 26, 2014, 05:52:27 AM »
The vl-some function will evaluate a predicate function on every item in a given list (or lists) with a syntax similar to mapcar, however, vl-some will cease evaluation when the predicate function returns a non-nil value - at which point vl-some will return this value, e.g.:

Iterate over a list until an item is equal to 3:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-some '(lambda ( x ) (= x 3)) '(0 1 2 3 4 5))
  2. T

Adding a print expression to demonstrate the function evaluation:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-some '(lambda ( x ) (print x) (= x 3)) '(0 1 2 3 4 5))
  2.  
  3. 0
  4. 1
  5. 2
  6. 3 T

Iterating over two lists until the items in the same position in both lists are equal to 5:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-some '(lambda ( a b ) (print a) (princ b) (= 5 a b)) '(0 1 2 5 2 6) '(2 1 4 5 2 1))
  2.  
  3. 0 2
  4. 1 1
  5. 2 4
  6. 5 5T

Or, as a simplified example, testing whether two lists contain equal items at the same index:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-some '= '(0 1 4 3 6) '(7 2 5 3 2))
  2. T

Returning the item itself to demonstrate that vl-some returns the value of the predicate function:
Code - Auto/Visual Lisp: [Select]
  1. _$ (vl-some '(lambda ( x ) (if (= (cdr x) "c") x)) '((1 . "a") (2 . "b") (3 . "c") (4 . "d")))
  2. (3 . "c")

Finally, here is the formal documentation for vl-some:

http://exchange.autodesk.com/autocad/enu/online-help/browse#WS1a9193826455f5ff1a32d8d10ebc6b7ccc-6882.htm

Coder

  • Swamp Rat
  • Posts: 827
Re: vl-some function
« Reply #2 on: January 26, 2014, 06:32:17 AM »
Waw too much examples  :-) thank you Lee .

Can I say that it's a Boolean function somehow ? there must be at least one non-nil value to return T otherwise it will return nil .

Many thanks

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vl-some function
« Reply #3 on: January 26, 2014, 06:49:24 AM »
Waw too much examples  :-) thank you Lee .

You're welcome Coder  :-)

Can I say that it's a Boolean function somehow ? there must be at least one non-nil value to return T otherwise it will return nil .

Not really - since vl-some will return the first non-nil value returned by the supplied function, which will not necessarily always be T
(as shown in my last example).

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vl-some function
« Reply #4 on: January 26, 2014, 06:53:10 AM »
To aid your understanding of the evaluation of vl-some, here is an equivalent version of vl-some written as a recursive function:

Code - Auto/Visual Lisp: [Select]
  1. (defun _vl-some ( prd lst )
  2.     (cond
  3.         ((null lst) nil)
  4.         (((eval prd) (car lst)))
  5.         ((_vl-some prd (cdr lst)))
  6.     )
  7. )

Or, iteratively using while:
Code - Auto/Visual Lisp: [Select]
  1. (defun _vl-some ( prd lst / itm rtn )
  2.     (setq prd (eval prd))
  3.     (while
  4.         (and
  5.             (setq itm (car lst))
  6.             (not (setq rtn (prd itm)))
  7.         )
  8.         (setq lst (cdr lst))
  9.     )
  10.     rtn
  11. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (_vl-some '(lambda ( x ) (if (= (cdr x) "c") x)) '((1 . "a") (2 . "b") (3 . "c") (4 . "d")))
  2. (3 . "c")
« Last Edit: January 27, 2014, 06:48:26 PM by Lee Mac »

Coder

  • Swamp Rat
  • Posts: 827
Re: vl-some function
« Reply #5 on: January 26, 2014, 06:53:32 AM »
non-nil value does not equal to T ? :-o

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vl-some function
« Reply #6 on: January 26, 2014, 06:59:10 AM »
non-nil value does not equal to T ? :-o

Not necessarily, a non-nil value could be any value which is not equal to nil, be it a non-empty list, string, integer, real...

Coder

  • Swamp Rat
  • Posts: 827
Re: vl-some function
« Reply #7 on: January 26, 2014, 07:09:25 AM »
You did a great job Lee . thank you so much . 

The last two routines explained it very well .  :-)


CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: vl-some function
« Reply #8 on: January 26, 2014, 08:39:20 AM »
Surprised vl-every did not come up.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Coder

  • Swamp Rat
  • Posts: 827
Re: vl-some function
« Reply #9 on: January 26, 2014, 10:47:48 AM »
Surprised vl-every did not come up.

Hi CAB .

I didn't get what you wanted to tell  :-(

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vl-some function
« Reply #10 on: January 26, 2014, 11:19:21 AM »
You did a great job Lee . thank you so much . 

The last two routines explained it very well .  :-)

Excellent - I'm glad my explanations were clear  :-)

Surprised vl-every did not come up.
I didn't get what you wanted to tell  :-(

CAB's post refers to the fact that vl-every is the AND to vl-some's OR
They are complementary functions  :wink:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: vl-some function
« Reply #11 on: January 26, 2014, 06:59:10 PM »
I also found an older explanation of mine here:-)

Coder

  • Swamp Rat
  • Posts: 827
Re: vl-some function
« Reply #12 on: January 26, 2014, 11:36:36 PM »
I also found an older explanation of mine here:-)
That is helpful too  :wink:

vl-every is a function that checks every element in the list and it returns T if all elements matched the criteria , is that correct ?

Thank you so much for your time and beautiful explanations  :-)

kruuger

  • Swamp Rat
  • Posts: 637
Re: vl-some function
« Reply #13 on: January 27, 2014, 05:03:32 AM »
vl-every is a function that checks every element in the list and it returns T if all elements matched the criteria , is that correct ?
yep,
Code: [Select]
(vl-every
 '(lambda (%)
   (= (type %) 'STR)
  )
  (list "a" "b" "c" 1)
)
return nil
Code: [Select]
(vl-every
 '(lambda (%)
   (= (type %) 'STR)
  )
  (list "a" "b" "c")
)
return T. similar to:
Code: [Select]
(apply 'and
  (mapcar
   '(lambda (%)
      (= (type %) 'STR)
    )
    (list "a" "b" "c")
  )
)
k.

Coder

  • Swamp Rat
  • Posts: 827
Re: vl-some function
« Reply #14 on: January 27, 2014, 06:07:33 AM »
Thank you so much kruuger for that nice work  :-)