Author Topic: How to compare number  (Read 3210 times)

0 Members and 1 Guest are viewing this topic.

Adesu

  • Guest
How to compare number
« on: December 14, 2006, 02:59:35 AM »
Hi Alls,
I have a problem to check number in file name,my file name have specific number,i.e
Code: [Select]
(setq file_folder "D:/YBI/Program/AutoLisp/Lisp program/My Alls Program")
 (setq lst (cddr (vl-directory-files file_folder)))
("0001-Array Circle.LSP" "0002-View Bar M10 to Down.LSP" "0003-Symbol of Yuasa.LSP" "0004-Border and Title.LSP" "0005-ASBAK.LSP" "0006-Registration Of Drawing.LSP"....etc.
Code: [Select]
(setq len (length lst))                    ; 490
(setq file_last (car (reverse lst)))     ; "0491-Edit Text Location File.LSP"
I mean total number of file is 491 pcs,but the fact only 490,it mean minus 1 file,but I don't know where or what number not yet write.
Sorry if confuse,I give solution like this
(setq lst1 '(1 2 3 4 5 6 7 8 9 10))
(setq lst2 '(1 2 3 4 5 6 8 9 10))
Now you can imagine,how to know number 7 it need for lst2,how to compare it.



gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How to compare number
« Reply #1 on: December 14, 2006, 03:58:47 AM »
Hi,

Something like this ?

Code: [Select]
(defun test (lst)
  (while (and lst (= (1+ (car lst)) (cadr lst)))
    (setq lst (cdr lst))
    )
  (if (cdr lst)
  (1+ (car lst))
    )
  )

(test lst1) -> nil
(test lst2) -> 7

Edit :

Or a recursive way :

Code: [Select]
(defun test (lst)
  (if (and lst (= (1+ (car lst)) (cadr lst)))
    (test (cdr lst))
    (if (cdr lst)
      (1+ (car lst))
    )
  )
)
« Last Edit: December 14, 2006, 04:14:32 AM by gile »
Speaking English as a French Frog

Adesu

  • Guest
Re: How to compare number
« Reply #2 on: December 14, 2006, 04:25:19 AM »
Hi gile,
I just tested,it's great !!,thanks very much.

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to compare number
« Reply #3 on: December 14, 2006, 06:39:14 AM »

If it is necessary to check up only:

Code: [Select]
(defun test (lst)
  (vl-every (function (lambda (a b) (= (1+ a) b))) lst (cdr lst))
) ;_ defun

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to compare number
« Reply #4 on: December 14, 2006, 06:53:05 AM »


If you have many breaks in the list or breaks big...

Code: [Select]

(defun test (lst)
  (vl-remove-if
    (function
      (lambda (x)
        (= (1+ (car x)) (cdr x))
      ) ;_ lambda
    ) ;_ function
    (mapcar
      (function cons)
      lst
      (cdr lst)
    ) ;_ mapcar
  ) ;_ vl-remove-if
) ;_ defun


Code: [Select]
(test '(1 2 3 4 5 6  8 9 10 13))
; => ((6 . 8) (10 . 13))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to compare number
« Reply #5 on: December 14, 2006, 07:05:40 AM »
recursive function, for many breaks in the list or breaks big:

Code: [Select]
(defun test (lst)
  (if (cdr lst)
    (if (= (1+ (car lst)) (cadr lst))
      (test (cdr lst))
      (cons (cons (car lst) (cadr lst)) (test (cdr lst)))
    ) ;_ if
  ) ;_ if
) ;_ defun

Code: [Select]
(test '(1 2 3 4 5 6 8 9 10 13))
; => ((6 . 8) (10 . 13))

Adesu

  • Guest
Re: How to compare number
« Reply #6 on: December 15, 2006, 01:42:15 AM »
It's good ,I like it.
Thanks.



If you have many breaks in the list or breaks big...

Code: [Select]

(defun test (lst)
  (vl-remove-if
    (function
      (lambda (x)
        (= (1+ (car x)) (cdr x))
      ) ;_ lambda
    ) ;_ function
    (mapcar
      (function cons)
      lst
      (cdr lst)
    ) ;_ mapcar
  ) ;_ vl-remove-if
) ;_ defun


Code: [Select]
(test '(1 2 3 4 5 6  8 9 10 13))
; => ((6 . 8) (10 . 13))

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: How to compare number
« Reply #7 on: December 15, 2006, 01:57:41 AM »
> Adesu

Recursion works more quickly... :)