(setq lst '((0.5 13) (0.6 11) (0.7 9) (0.8 5) (0.9 3) (1.0 2))
pal (list 0.55))
(Lookup pal lst)-->12.0
E.g.2(setq lst
'((0.5 (10 410) (15 390) (20 365))
(0.6 (10 310) (15 300) (20 280) (25 270))
(0.7 (10 250) (15 240) (20 225) (25 215) (30 205))
(0.8 (10 200) (15 190) (20 180) (25 170) (30 165))
(0.9 (10 160) (15 150) (20 145) (25 140) (30 130) (35 125))
(1.0 (10 130) (15 125) (20 120) (25 115) (30 110) (35 105) (40 100)))
pal (list 0.76 12.3)
)
(Lookup pal lst)-->215.4
E.g.3(setq
pal (list 0.65 1.5 12.3)
l '((0.5 (1 (10 400) (15 380) (20 360)) (2 (10 340) (15 320) ( 20 300)) (3 (10 280) (15 260) (20 240)))
(0.6 (1 (10 340) (15 320) ( 20 300)) (2 (10 280) (15 260) (20 240)) (3 (10 220) (15 200) (20 180)))
(0.7 (1 (10 280) (15 260) (20 240)) (2 (10 220) (15 200) (20 180)) (3 (10 160) (15 140) (20 120))))
)
(Lookup pal l)-->270.8
(defun Lookup (n l)
(if (<= (caar l) (car n) (caadr l))
(if (cdr n)
(cadr (inters (list (car n) 0.0)
(list (car n) 1.0)
(list (caar l) (Lookup (cdr n) (cdar l)))
(list (caadr l) (Lookup (cdr n) (cdadr l)))
nil
)
)
(cadr (inters (list (car n) 0.0) (list (car n) 1.0) (car l) (cadr l) nil))
)
(Lookup n (cdr l))
)
)
Regards(defun Lookup (n l)
(cond ((null (cdr l)) nil)
((<= (caar l) (car n) (caadr l))
(if (cdr n)
((lambda (a b)
(if (and a b)
(cadr (inters (list (car n) 0.0) (list (car n) 1.0) (list (caar l) a) (list (caadr l) b) nil))
)
)
(Lookup (cdr n) (cdar l))
(Lookup (cdr n) (cdadr l))
)
(cadr (inters (list (car n) 0.0) (list (car n) 1.0) (car l) (cadr l) nil))
)
)
((Lookup n (cdr l)))
)
)
RegardsFirst release.Oh very cool for using inters function , Stefan
I've made tests on your samples only.Code - Auto/Visual Lisp: [Select]
) nil ) ) ) )
Hello,Hi bruno_vdh , your's cool too.
A variation ...Code: [Select](defun Lookup (n l)
Regards
(if (<= (caar l) (car n) (caadr l))
(if (cdr n)
(cadr (inters (list (car n) 0.0)
(list (car n) 1.0)
(list (caar l) (Lookup (cdr n) (cdar l)))
(list (caadr l) (Lookup (cdr n) (cdadr l)))
nil
)
)
(cadr (inters (list (car n) 0.0) (list (car n) 1.0) (car l) (cadr l) nil))
)
(Lookup n (cdr l))
)
)
(setq
pal (list 0.4 1.5 12.3)
l '((0.5 (1 (10 400) (15 380) (20 360)) (2 (10 340) (15 320) ( 20 300)) (3 (10 280) (15 260) (20 240)))
(0.6 (1 (10 340) (15 320) ( 20 300)) (2 (10 280) (15 260) (20 240)) (3 (10 220) (15 200) (20 180)))
(0.7 (1 (10 280) (15 260) (20 240)) (2 (10 220) (15 200) (20 180)) (3 (10 160) (15 140) (20 120))))
)
(ph:lookup pal l)-->420.8 , first Arg 0.4 beyond the domain , so it must return nil .(setq
p1 (list 0.4 1.5 12.3)
p2 (list 0.62 3.5 12.3)
p3 (list 0.62 1.5 22.3)
p4 (list 0.62 1.5 12.3)
l '((0.5 (1 (10 400) (15 380) (20 360)) (2 (10 340) (15 320) ( 20 300)) (3 (10 280) (15 260) (20 240)))
(0.6 (1 (10 340) (15 320) ( 20 300)) (2 (10 280) (15 260) (20 240)) (3 (10 220) (15 200) (20 180)))
(0.7 (1 (10 280) (15 260) (20 240)) (2 (10 220) (15 200) (20 180)) (3 (10 160) (15 140) (20 120))))
)
_$ (ph:lookup1 p1 l)
nil
_$ (ph:lookup1 p2 l)
nil
_$ (ph:lookup1 p3 l)
nil
_$ (ph:lookup1 p4 l)
288.8
(setq
p (list 0.4 1.5 )
l '((0.5 (1 (10 400) (15 380) (20 360)) (2 (10 340) (15 320) ( 20 300)) (3 (10 280) (15 260) (20 240)))
(0.6 (1 (10 340) (15 320) ( 20 300)) (2 (10 280) (15 260) (20 240)) (3 (10 220) (15 200) (20 180)))
(0.7 (1 (10 280) (15 260) (20 240)) (2 (10 220) (15 200) (20 180)) (3 (10 160) (15 140) (20 120))))
)
(ph:lookup1 p l)(defun ss:lookup (n l / b c r)
;; learn from Stefan's function , rewrite my old function
;|(setq l (vl-sort l (function (lambda (a b) (< (car a) (car b))))));_here must be added , we don't know a dimension of the Nd-list is descending . |;
(if (and (numberp (car n))
(vl-consp l)
(cadar l)
(<= (caar l) (car n) (car (last l)))
(setq l (mapcar (function (lambda (i)
(nth i l))) (f2 (car n) (mapcar (function car) l))))
;_ if loops goon one by one , when the given list is superlarge will take long time to get the suit items .
;_because the given list is sort by first item of every terms , so here can use Dichotomy to reduce computation .
)
(progn
(setq b (car l)
c (cadr l))
(if (<= (car b) (car n) (car c))
(cond
((not (cadr b));_1st term is no 2nd item , quit routine
nil)
((= (car n) (car b));_1st term's 1st item = para
(if (numberp (cadr b))
(setq r (cadr b));_2nd item is number
(setq r (f (cdr n) (cdr b)));_inter next recursion
)
)
((and (cadr c) (= (car n) (car c)));_2nd term's 1st item = para
(if (numberp (cadr c))
(setq r (cadr c));_2nd item is number
(setq r (f (cdr n) (cdr c)));_inter next recursion
))
((and (cadr b) (cadr c) (< (car b) (car n) (car c)));_para between 1st_term's 1st item and 2nd_term's 1st item
(if (and (numberp (cadr b)) (numberp (cadr c)));_2nd item is number
(setq
r (f1 (car n) (car b) (cadr b) (car c) (cadr c)))
(if (and (vl-consp (cadr b)) (vl-consp (cadr c)));_the 2 terms's 2nd item is a list
(setq r (f1 (car n)
(car b)
(ss:lookup (cdr n) (cdr b))
(car c)
(ss:lookup (cdr n) (cdr c))))
)))
))
)
)
)
(defun creat_lookup_lst (N / a i al bl ml)
(setq a (* N N)
i N)
(repeat N
(setq al (cons i al)
i (1- i)))
(setq bl (mapcar (function (lambda (a)
(* 0.1 a)))
al))
(repeat (* N N)
(setq mL (cons (setq a (1- a)) ml)))
(mapcar (function (lambda (i a)
(cons i (mapcar (function (lambda (i b)
(list i b)))
al
a))))
bl
(list-comp ml n))
)
(defun list-comp (a b / mid rslt)
(repeat (/ (length a) b)
(setq mid nil)
(repeat b
(setq mid (cons (car a) mid)
a (cdr a)
)
)
(setq rslt (cons (reverse mid) rslt))
)
(if a (reverse (cons a rslt))
(reverse rslt))
)
(setq l (creat_lookup_lst 1000)
p (list 57.6 875.3))
(QuickBench (mapcar '(lambda (f) (list f 'p 'l )) (list ph:lookup1 ss:lookup bv:lookup)))
QuickBench function see here http://www.theswamp.org/index.php?action=dlattach;topic=42091.0;attach=22832