list - What is the core difference between strings and numbers in Common Lisp? -
being new cl, play lot simple algorithms. instance, tried implement function removing unique elements in list.
(1 2 2 3 3 4 5 3) -> (2 2 3 3 3)
first attempt lead code:
(defun remove-unique (items) (let ((duplicates (set-difference items (remove-duplicates items :test #'equal)))) (append duplicates (remove-duplicates duplicates :test #'equal))))
this works ok strings return nil
numbers. reading bit more set-difference
i've learned isn't suppose work duplicate populated lists @ all, works somehow in case, abandoned approach unportable , moved along.
another attempt is:
(defun remove-unique (items) (loop item in items when (member item (cdr (member item items))) collect item))
and works ok numbers, returns nil
strings.
apparently there core difference between strings , numbers don't understand. how come list processing functions such member
, set-difference
work differently on them?
strings more related lists numbers since both lists , strings sequences.
"hello"
sequence (compund data type) starting primitive character value #\h
, ending #\o
.
'(1 2 3)
sequence (compond data type) starting primitive numeric value 1 , ending 3.
characters similar numbers in primitive values. primitive values can compared using eql
while sequences, not same object, can compared using equal
(setq list1 (list 1 2 3)) (setq list2 (list 1 2 3)) (eql list1 list2) ;==> nil (equal list1 list2) ;==> t ;; comparing first element of both lists using eql (eql (car list1) (car list2)) ;==> t (setq string1 "hello") (setq string2 "hello") (eql string1 string2) ;==> nil (equal string1 string2) ;==> t ;; comparing first character of both strings using eql (eql (elt string1 0) (elt string2 0)) ;==> t
most (if not all) functions in common lisp compares has optional named argument :test
can supply how elements compare. default eql
. make them behave corretly sequences need supply #'equal
:test
.
Comments
Post a Comment