Combining characters in a common lisp

I want to insert char into a list. However, I want to combine this char with the last character in the list. When added and flawed, the result always consists of two different characters. Well, I want one combined character to be my result.

Example:

(XXXX 'a '5) ====> (a5) 

What I would like to have, not:

  (XXXX 'a '5) ====> (a 5) 
+3
source share
3 answers

You cannot "combine characters" in Lisp.

First of all, 5 not a symbol, but a number. If you need a character named "5" , you must enter it as |5| (eg).

If the function accepts the character A and the character |5| and produces an A5 character; it does not combine characters. He created a new character whose name is the name of the names of these input characters.

Properly designed Lisp programs rarely depend on the symbol name. They depend on characters that are unique objects.

If you use symbols to identify things, and both 5 and A identify an entity, the best answer is not necessarily to create a new symbol, which, at least by name, is a mashup of these two symbols. For example, a better design might be to recognize that names are multifaceted or complex in some way. Perhaps the list (A 5) may serve as a name.

Common Lisp functions may have compound names. For example, (setf foo) is the name of the function. Aggregates like lists can be names.

If you just need a machine to create unique characters at runtime, consider using the gensym function. You can pass your own prefix:

 (gensym "FOO") -> #:FOO0042 

Of course, the prefix may be the name of some existing symbol pulled through the symbol-name . Symbol #:FOO0042 not unique because of 0042 , but because it is just the selected object in the address space. #: means it is not interned in any package. Symbol Name FOO0042 .

If you still really want a simple way to take a printed representation of a bunch of input features and turn it into a character:

 (defun mashup-symbol (&rest objects) (intern (format nil "~{~a~}" objects))) 

Examples:

 (mashup-symbol 1 2 3) -> |123| (mashup-symbol '(ab) 'c 3) -> |(AB)C3| 
+10
source

The answer to your question

 (defun concatenate-objects-to-symbol (&rest objects) (intern (apply #'concatenate 'string (mapcar #'princ-to-string objects)))) (concatenate-objects 'a 'b) ==> ab 

Oh, if you need a list as a result:

 (defun xxxx (s1 s2) (list (concatenate-objects-to-symbol s1 s2))) 

However, I am sure that this is not a question that you really want to ask.

Creating new characters programmatically is not something that beginners should do ...

+2
source

Define this:

 (defun symbol-append (&rest symbols) (intern (apply #'concatenate 'string (mapcar #'symbol-name symbols)))) 

and then use it like:

 * (symbol-append 'a 'b 'c) ABC * (apply #'symbol-append '(abc)) ABC 

If you expect your arguments to contain material other than characters, replace symbol-name with:

  (lambda (x) (typecase x ...)) 

or a pre-existing CL function (which I forgot :() that builds anything.

+2
source

Source: https://habr.com/ru/post/987799/


All Articles