What is the simplist way to ensure that 2 lists in lisp are the same length?
Given 2 lists, I want to ensure that they are the same size, I'm having a tough time with this code. Should I 开发者_JAVA百科be using variables to do this?
(defun samesize (list1 list2)
(cond (;logic here) T))
Both Common Lisp and elisp have length
:
(defun samesize (list1 list2)
(= (length list1) (length list2)))
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)
No need to explicitly use length twice.
(defun samesize (l1 l2)
(apply #'= (mapcar #'length (list l1 l2)))
This may seem like wasted effort, but with longer function names this sort of thing is useful.
I was also hoping to demonstrate a new concept.
DRY (don't repeat yourself)
精彩评论