Вопрос

I'm having trouble combining two items in a list together into one item.

For example:

'(Ben Hofferber) => '(Ben_Hofferber) or '(Ben-Hofferber)

Any ideas on how this can be achieved?

I've been messing around with print functions trying to get it to work that way with no success.I need to combine and separate these items so that I can use them as keys for an a-list.

Это было полезно?

Решение

You can use lists as alist keys too. E.g.,

(assoc '(1 2) '(((1 1) a) ((1 2) b) ((1 3) c)) :test #'equal)
==> ((1 2) B)

So, if you have a list of people

(defparameter *people* '((Ben Hofferber) (Adam Young)))

you can set their ages like this:

(defparameter *ages* (mapcar #'list *people* '(20 11)))

and find them like this:

(assoc '(Ben Hofferber) *ages* :test #'equal)
==> ((BEN HOFFERBER) 20)

If, however, you insist on combining symbols into one, you will have to explain how you want to do that.

If both symbols are in the same package, the combined symbols can be placed there too:

(defun combine-symbols (s1 s2)
  (intern (concatenate 'string (symbol-name s1) "-" (symbol-name s2))
          (symbol-package s1)))

but if they are in different packages (e.g., cl:car and cl-user::ben), you would have to make a tough choice which package to go with.

Alternatively, you can go with strings and use

(defun combine-objects (o1 o2)
  (format nil "~A-~A" o1 o2))

Don't forget to pass :test #'equal or :test #'string= to assoc.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top