RepCad Posted March 29, 2020 Posted March 29, 2020 (edited) Hi all guys, I need a function to find the point that has minimum Y in a list, like this : ((10 15) (19 25) (26 10) (50 32)) >> (26 10) Can some one give me a function or best way to do this? Thanks in advanced. Edited March 29, 2020 by amir0914 Quote
dlanorh Posted March 29, 2020 Posted March 29, 2020 Try. I can't test as I'm away from AutoCAD (defun miny (lst) (car (vl-sort lst '(lambda (x y) (< (cadr x) (cadr y)))))) ;example call (setq ymin (miny '((10 15) (19 25) (26 10) (50 32)))) 1 Quote
Lee Mac Posted March 29, 2020 Posted March 29, 2020 When finding extrema, it is typically more efficient to iterate over the list once, rather than using a sorting function (such as vl-sort) which necessarily performs many more comparisons to order the entire list - this is one circumstance where shorter code is not necessarily more efficient. You may find this thread of interest in this regard. 1 Quote
Jonathan Handojo Posted March 30, 2020 Posted March 30, 2020 2 hours ago, amir0914 said: Quote Hi all guys, I need a function to find the point that has minimum Y in a list, like this : ((10 15) (19 25) (26 10) (50 32)) >> (26 10) Can some one give me a function or best way to do this? Thanks in advanced. I won't probably iterate. I'll do the below, though I'm not sure if it's more efficient: (defun findmin (lst / m) (setq m (apply 'min (mapcar 'cadr lst))) (car (vl-member-if '(lambda (x) (eq (cadr x) m)) lst)) ) 1 Quote
RepCad Posted March 30, 2020 Author Posted March 30, 2020 9 hours ago, Lee Mac said: When finding extrema, it is typically more efficient to iterate over the list once, rather than using a sorting function (such as vl-sort) which necessarily performs many more comparisons to order the entire list - this is one circumstance where shorter code is not necessarily more efficient. You may find this thread of interest in this regard. You are right lee mac, I looked at the post that you implied. it's the exact post that i need. Quote
marko_ribar Posted March 30, 2020 Posted March 30, 2020 (defun car-sort ( l f / removenth r k ) (defun removenth ( l n / k ) (setq k -1) (vl-remove-if '(lambda ( x ) (= (setq k (1+ k)) n)) l) ) (setq k -1) (vl-some '(lambda ( a ) (setq k (1+ k)) (if (vl-every '(lambda ( x ) (apply f (list a x))) (removenth l k)) (setq r a))) l) r ) ;;; (car-sort '(2 4 1 3 5 1) '<) => nil ;;; (car-sort '(2 4 1 3 5 1) '<=) => 1 (setq yminpt (car-sort ptlst '(lambda ( a b ) (<= (cadr a) (cadr b))))) Lee is right as usual... Consider using my (car-sort) function - should be fine with large lists, but be aware that when specifying sorting function '(lambda ( a b ) ... ) you don't use arguments the same as in (car-sort)... For ex. '(lambda ( x y ) ... ) will fail... Also, note that for comparisons you must use <= or >= instead like normal (vl-sort) < or > as like you see in example (car-sort) can't determine (< 1 1) as T and therefore return will be nil... (car-sort) will iterate just once and won't make further sorting of other elements - it will return just (car) element... Regards, M.R. 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.