How to convert list element to string type
How to convert list element to string type
Hello!
Can anyone help me with this lisp quesition?
I passed a list (only one item) to a function and inside function I want to set value of this list to a text widget.
I have:
(defun display-value (mylist)
(set-value text1 "value" mylist) ; which is not working because mylist is not string
)
How can I convert mylist to string type to set text field value.
Because the interpreter is customized, please show me all the possible way to do it.
Thanks!
Can anyone help me with this lisp quesition?
I passed a list (only one item) to a function and inside function I want to set value of this list to a text widget.
I have:
(defun display-value (mylist)
(set-value text1 "value" mylist) ; which is not working because mylist is not string
)
How can I convert mylist to string type to set text field value.
Because the interpreter is customized, please show me all the possible way to do it.
Thanks!
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: How to convert list element to string type
You will have to tell us:
- What kind of Lisp are you using (Common Lisp, Scheme or some other dialect), including the implementation (or interpreter)
- What is the function set-value supposed to do
- What kind of element mylist is supposed to have (integer, character, string...)
- What representation as a string do you want for such element
Re: How to convert list element to string type
Because the lisp interpreter is customized, I don't exactly know what lisp is used. I guess it's commom lisp.
Anyway, I find a way to make it work. I use symbol-name to convert it to string.
Thanks for the help!
Anyway, I find a way to make it work. I use symbol-name to convert it to string.
Thanks for the help!
-
- Posts: 406
- Joined: Sat Mar 07, 2009 6:17 pm
- Location: Brazil
- Contact:
Re: How to convert list element to string type
Well, then it's not a list, it's a symbol.
Re: How to convert list element to string type
The following may be relevant.
Set stream to nil, and this should return a string without printing elsewhere.
Code: Select all
(defun tk-princ (stream arg colon at)
"Like princ (format ~a), but convert a lisp list to a Tk list."
(declare (ignore colon at))
(cond ((or (null arg)
(and (stringp arg)
(string= arg "")))
(format stream "{}"))
((listp arg)
(format stream "{~{~/tk-princ/~^ ~}}" arg))
(t
(format stream "~a" arg))))
Re: How to convert list element to string type
Thanks for the help. I remember I tried format and failed. Maybe I used wrong syntax.
Right now I have another question regarding list and string. When I read from a file using read-line, I think it's string type, right? How can I convert it into list? For example the line is "abc efg", I want to assign abc to var1 and efg to var2. I thought it would be easy to do if the line is list. But I don't know how to do it. Or maybe I should parse string?
Right now I have another question regarding list and string. When I read from a file using read-line, I think it's string type, right? How can I convert it into list? For example the line is "abc efg", I want to assign abc to var1 and efg to var2. I thought it would be easy to do if the line is list. But I don't know how to do it. Or maybe I should parse string?

Re: How to convert list element to string type
Best way to figure it out is to try it in this case
You can coerce a string to a list with (coerce string 'list), but that'd be a list of characters, of course.
I think you might want to use READ which reads one s-expression with the same rule as code is read.Streams can be very useful even if you're not working with files! For instance if you want to compose a string, you can similarly use with-output-to-string; otherwise you'd be effectively reimplementing streams.
Note that it also reads numbers and sublists and such which can be undesirable. CL does seem a little annoying in this, it feels a way too inconvenient working with readtables/variables and such to try make READ work, and CL is lacking for instance in having parse-integer, but not parse-number.([here are two](http://www.reddit.com/r/lisp/comments/c ... ns/c0swsih), mine doesn't do fractions atm, btw) Similarly, it doesn't seem to have a parse-symbol/parse-word. Easy to make though.

Code: Select all
CL->USER> (read-line)
yup it is a string
"yup it is a string"
nil
I think you might want to use READ which reads one s-expression with the same rule as code is read.
Code: Select all
(defun read-until-empty (stream)
"Does what you want, but from a stream."
(let ((result (read stream nil :eof)))
(if (eql result :eof) nil ;End of stream, done.
(cons result (read-until-empty stream))))) ;Keep listing results.
(defun read-until-empty-from-string (string)
"Makes it work from a stream for you."
(with-input-from-string (stream string)
(read-until-empty stream)))
(read-until-empty-from-string "reads symbols (but also lists) and numbers 1 2 3")
Note that it also reads numbers and sublists and such which can be undesirable. CL does seem a little annoying in this, it feels a way too inconvenient working with readtables/variables and such to try make READ work, and CL is lacking for instance in having parse-integer, but not parse-number.([here are two](http://www.reddit.com/r/lisp/comments/c ... ns/c0swsih), mine doesn't do fractions atm, btw) Similarly, it doesn't seem to have a parse-symbol/parse-word. Easy to make though.
Code: Select all
(defun parse-word (stream)
(with-output-to-string (out)
(flet ((rch ()
(read-char stream nil :eof)))
(do ((ch (rch) (rch)))
((case ch ((:eof #\Space #\Newline #\Tab) t)) nil)
(write-char ch out)))))