TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder 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 . :-)
-
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:
Adding a print expression to demonstrate the function evaluation:
Iterating over two lists until the items in the same position in both lists are equal to 5:
Or, as a simplified example, testing whether two lists contain equal items at the same index:
_$
(vl-some '
= '
(0 1 4 3 6) '
(7 2 5 3 2))T
Returning the item itself to demonstrate that vl-some returns the value of the predicate function:
_$
(vl-some '
(lambda ( x
) (if (= (cdr x
) "c") x
)) '
((1 .
"a") (2 .
"b") (3 .
"c") (4 .
"d")))(3 . "c")
Finally, here is the formal documentation for vl-some:
http://exchange.autodesk.com/autocad/enu/online-help/browse#WS1a9193826455f5ff1a32d8d10ebc6b7ccc-6882.htm
-
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
-
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).
-
To aid your understanding of the evaluation of vl-some, here is an equivalent version of vl-some written as a recursive function:
(defun _vl
-some
( prd lst
) ((_vl
-some prd
(cdr lst
))) )
)
Or, iteratively using while:
(defun _vl
-some
( prd lst
/ itm rtn
) )
)
rtn
)
_$
(_vl
-some '
(lambda ( x
) (if (= (cdr x
) "c") x
)) '
((1 .
"a") (2 .
"b") (3 .
"c") (4 .
"d")))(3 . "c")
-
non-nil value does not equal to T ? :-o
-
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...
-
You did a great job Lee . thank you so much .
The last two routines explained it very well . :-)
-
Surprised vl-every did not come up.
-
Surprised vl-every did not come up.
Hi CAB .
I didn't get what you wanted to tell :-(
-
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:
-
I also found an older explanation of mine here (http://bit.ly/vlsome). :-)
-
I also found an older explanation of mine here (http://bit.ly/vlsome). :-)
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 :-)
-
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,
(vl-every
'(lambda (%)
(= (type %) 'STR)
)
(list "a" "b" "c" 1)
)
return nil
(vl-every
'(lambda (%)
(= (type %) 'STR)
)
(list "a" "b" "c")
)
return T. similar to:
(apply 'and
(mapcar
'(lambda (%)
(= (type %) 'STR)
)
(list "a" "b" "c")
)
)
k.
-
Thank you so much kruuger for that nice work :-)
-
Excellent thread. Brings back memories of this (http://www.theswamp.org/index.php?topic=7806.0;all) one. What a great vault the swamp has become.
Edit: Close to half a million posts, wow.
-
What a great vault the swamp has become.
Couldn't agree more :-)
-
< .. >
Edit: Close to half a million posts, wow.
about 114 days away by my guestimation :)