chavlji Posted October 10, 2009 Posted October 10, 2009 I have some basic question. I have a list of elements. Now I would like to check each item of list with every other item in the list. Except with himself and item items that stands before it in list (with that items it has already been compared in previous cycles of loop). In Delphi i wold do it like this: for i:=0 to length( List ) do for j:=i to length( List ) do if List.point = List[j].point then DoSomething end; end; How would you translate the above statement into LISP ? Quote
gile Posted October 10, 2009 Posted October 10, 2009 Hi, Something like this ? (setq copy lst) (foreach i lst (setq copy (cdr copy)) (foreach j copy (if (equal i j) (do_something) ) ) ) Quote
chavlji Posted October 10, 2009 Author Posted October 10, 2009 Thanks! It seems ok... although it's very resource consuming, but this is LISP I guess. To copy entire record list and then rewriting whole list each step in the loop just to compare items (( Quote
gile Posted October 10, 2009 Posted October 10, 2009 (setq copy (cdr copy)) isn't "rewriting whole list each step in the loop". It only bounds copy to the "Contents of the Decrement part of Register number"), IOW the list but first item (you can have a look here "There is no list" for some explanations about how "lists" are built in LISP languages). If you prefer a syntax closer to the "for i = ..." it can be done this way : (setq i 0 l (length lst)) (while (< i (1- l)) (setq j i) (while (< j l) (setq j (1+ J)) (if (equal (nth i lst) (nth j lst)) (do_something) ) ) (setq i (1+ i)) ) But it uses the nth function which is probably implemented using cdr process which is, with car, the way LISP languages access to list elements. (nth 3 lst) is equivalent to (car (cdr (cdr (cdr lst)))). I'm not sure it's less ressource expansive... You can avoid copying the list if you don't need it anymore after this process (destructive method) (while (and (setq i (car lst)) (setq lst (cdr lst)) ) (foreach j lst (if (equal i j) (do_something) ) ) ) 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.