Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplist way to ensure that 2 lists in lisp are the same length?

Tags:

lisp

Given 2 lists, I want to ensure that they are the same size, I'm having a tough time with this code. Should I be using variables to do this?

(defun samesize (list1 list2)
  (cond (;logic here) T))
like image 333
Firoso Avatar asked Oct 27 '10 02:10

Firoso


2 Answers

Both Common Lisp and elisp have length:

(defun samesize (list1 list2)
  (= (length list1) (length list2)))
like image 110
Jack Kelly Avatar answered Nov 07 '22 18:11

Jack Kelly


You can use recursion if you want to implemet this yourself.

2 lists are the same size if they are both empty. They are different size if one is empty and the other is not. And if none of these is true, they are of the same size-comparison as those lists sans one element (i.e. their cdr-s)

like image 45
DVK Avatar answered Nov 07 '22 18:11

DVK