How to convert list element to string type

Whatever is on your mind, whether Lisp related or not.

How to convert list element to string type

Postby joybee » Fri Jul 02, 2010 7:43 am

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!
joybee
 
Posts: 21
Joined: Wed Jan 07, 2009 1:49 pm

Re: How to convert list element to string type

Postby gugamilare » Fri Jul 02, 2010 9:22 am

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
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: How to convert list element to string type

Postby joybee » Fri Jul 02, 2010 9:58 am

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!
joybee
 
Posts: 21
Joined: Wed Jan 07, 2009 1:49 pm

Re: How to convert list element to string type

Postby gugamilare » Fri Jul 02, 2010 11:40 am

Well, then it's not a list, it's a symbol.
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: How to convert list element to string type

Postby nuntius » Fri Jul 02, 2010 8:18 pm

The following may be relevant.

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))))


Set stream to nil, and this should return a string without printing elsewhere.
User avatar
nuntius
 
Posts: 497
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: How to convert list element to string type

Postby joybee » Wed Jul 07, 2010 12:12 pm

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? :-(
joybee
 
Posts: 21
Joined: Wed Jan 07, 2009 1:49 pm

Re: How to convert list element to string type

Postby Jasper » Fri Jul 09, 2010 11:01 am

Best way to figure it out is to try it in this case :)
Code: Select all
CL->USER> (read-line)
yup it is a string
"yup it is a string"
nil
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.
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")
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
(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)))))
Jasper
 
Posts: 209
Joined: Fri Oct 10, 2008 8:22 am
Location: Eindhoven, The Netherlands


Return to The Lounge

Who is online

Users browsing this forum: No registered users and 1 guest

cron