Hi
Can anyone tell me how to convert e.g. a list into a string like the
print function in lisp does?
Is there a way to add characters/strings to a string?
Thank you
Kristof
Hello!
Kristof <··············@hotmail.com> wrote:
>Can anyone tell me how to convert e.g. a list into a string like the
>print function in lisp does?
There are functions print-to-string, prin1-to-string and
princ-to-string. Or you can use string output streams. In this case,
you can format to the string stream or build the output with multiple
output function calls.
(with-output-to-string (stream)
(format stream "Hello world! ")
(princ '(1 2 3) stream))
=>
"Hello world! (1 2 3)"
>Is there a way to add characters/strings to a string?
It depends. In principle strings are one-dimensional arrays
containing characters. Arrays can be adjustable or not
(i.e. you can change the size of the dimensions), and
have a fill-pointer (a concept that only a part of a one-dimensional
array is really "valid").
So if the string is adjustable, you can destructively append to it
(change the original string).
You can, of course, also do it in a functional way, creating a
new string which is the concatenation of two or more old ones,
using (concatenate 'string a-string another-string)
>Thank you
>Kristof
Kind regards,
Hannah.
Kristof wrote:
> Hi
>
> Can anyone tell me how to convert e.g. a list into a string like the
> print function in lisp does?
write-to-string, princ-to-string, prin1-to-string
btw, (apropos 'to-string) reveals the above. apropos is one of your best
friends.
> Is there a way to add characters/strings to a string?
Many and varied depending on what you are up to.
(concatenate 'string "one" ",")
or the sneaky: (format nil "~a," "one")
if you created the string as an array:
(let ((s (make-array 3 :element-type 'character
:adjustable t :fill-pointer 3
:initial-contents "one")))
(vector-push-extend #\, s)
s)
Since you are asking about printing lists at the same time:
(format nil "~{ ~a,~}" '(one two three)) => " ONE, TWO, THREE,"
KT
--
http://tilton-technology.com
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application