From: Davide
Subject: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <5tS28.24950$tl3.795884@twister2.libero.it>
Hi,  I am novice.
I want to build a program that ask me a phrase in natural language and respond.
I have created a function "string_to_list" to convert a string in a list of strings,
but I don't understand how to convert a string in a list of symbols, 
example with a string:

> ( setq phrase ( string_to_list "My car is red" ) )
> ( "My" "car" "is" "red" )
>

I would prefer to use symbols, but how to convert a string in a list of symbols?

> ( setq phrase ( list 'My 'car 'is 'red ) )
> ( my car is red )
>

Which is the right method to do this?
I don't understand which are differences from symbols and strings.

Thanks for any help.
Davide U.

------------------------------------------------------------------------------------
(defun string_to_list (stringx)
   (when t
      (setq listx nil)
      (setq stringx (strcat " " (string-trim " " stringx)))
      (setq luns (length stringx))
      (setq oldp luns)
      (dotimes (i luns)
        (when t
           (setq ip (- (- luns i) 1))
           (when (equal (elt stringx ip) #\Space)
               (when t
                  (setq b (subseq stringx (+ ip 1) oldp))
                  (setq listx (cons b listx))
                  (setq oldp ip)
      ))))
      listx
))
------------------------------------------------------------------------------------

From: Kenny Tilton
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <3C4C1930.127493A1@nyc.rr.com>
Davide wrote:
> I don't understand how to convert a string in a list of symbols,

check out: (INTERN "MY")

also: (read-from-string "my car is red" nil nil :start 3)

and: (read-from-string "(my car is red)")

> I don't understand which are differences from symbols and strings.

strings are lisp sequences of lisp characters:

> (type-of "red")
(SIMPLE-ARRAY CHARACTER (3))

you can access their elements (characters) at random, take their
lengths, etc. tocompare you have to use string= or string-equal.

symbols are atoms, much easier to work with. to get a symbol as a
string: (symbol-name 'red)


kenny
clinisys
From: Davide
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <gzW28.26059$tl3.820258@twister2.libero.it>
"Kenny Tilton" <·······@nyc.rr.com> ha scritto nel messaggio ······················@nyc.rr.com...
> 
> 
> Davide wrote:
> > I don't understand how to convert a string in a list of symbols,
> 
> check out: (INTERN "MY")
> 
> also: (read-from-string "my car is red" nil nil :start 3)
> 
> and: (read-from-string "(my car is red)")
> 
> > I don't understand which are differences from symbols and strings.
> 
> strings are lisp sequences of lisp characters:
> 
> > (type-of "red")
> (SIMPLE-ARRAY CHARACTER (3))
> 
> you can access their elements (characters) at random, take their
> lengths, etc. tocompare you have to use string= or string-equal.
> 
> symbols are atoms, much easier to work with. to get a symbol as a
> string: (symbol-name 'red)
> 
> 
> kenny
> clinisys

Thank you very much.
Your help has been very precious.

David U.
From: ab talebi
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <3c4c1c66.3787324@news.uio.no>
On Mon, 21 Jan 2002 10:56:01 GMT, "Davide" <········@tin.it> wrote:

>Hi,  I am novice.
>I want to build a program that ask me a phrase in natural language and respond.
>I have created a function "string_to_list" to convert a string in a list of strings,
>but I don't understand how to convert a string in a list of symbols, 
>example with a string:
>
>> ( setq phrase ( string_to_list "My car is red" ) )
>> ( "My" "car" "is" "red" )
>>
>
>I would prefer to use symbols, but how to convert a string in a list of symbols?
>
>> ( setq phrase ( list 'My 'car 'is 'red ) )
>> ( my car is red )
>>

you might find this function interesting:

(defun string-to-list (str &optional (pos 0) (list nil))
  (multiple-value-bind (obj newpos)
       (read-from-string str nil :end-of-string :start pos)
    (if (eql obj :end-of-string)
       (nreverse list)
       (string-to-list str newpos (cons obj list)))))

this functions does this:
(string-to-list "my car is red")
(MY CAR IS RED)
From: Janis Dzerins
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <a2h82k$cf6$1@milzis.latnet.lv>
············@yahoo.com (ab talebi) writes:

> On Mon, 21 Jan 2002 10:56:01 GMT, "Davide" <········@tin.it> wrote:
> 
> >Hi,  I am novice.
> >I want to build a program that ask me a phrase in natural language and respond.
> >I have created a function "string_to_list" to convert a string in a list of strings,
> >but I don't understand how to convert a string in a list of symbols, 
> >example with a string:
> >
> >> ( setq phrase ( string_to_list "My car is red" ) )
> >> ( "My" "car" "is" "red" )
> >>
> >
> >I would prefer to use symbols, but how to convert a string in a list of symbols?
> >
> >> ( setq phrase ( list 'My 'car 'is 'red ) )
> >> ( my car is red )
> >>
> 
> you might find this function interesting:
> 
> (defun string-to-list (str &optional (pos 0) (list nil))
>   (multiple-value-bind (obj newpos)
>        (read-from-string str nil :end-of-string :start pos)
>     (if (eql obj :end-of-string)
>        (nreverse list)
>        (string-to-list str newpos (cons obj list)))))
> 
> this functions does this:
> (string-to-list "my car is red")
> (MY CAR IS RED)

How nice -- two blind men in the night have stumbled on each other :)

-- 
Janis Dzerins

  Eat shit -- billions of flies can't be wrong.
From: Coby Beck
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <JW038.58952$_w.10503724@typhoon.tampabay.rr.com>
"Janis Dzerins" <·····@latnet.lv> wrote in message
·················@milzis.latnet.lv...
> ············@yahoo.com (ab talebi) writes:
>
> > On Mon, 21 Jan 2002 10:56:01 GMT, "Davide" <········@tin.it> wrote:
> >
> > (defun string-to-list (str &optional (pos 0) (list nil))
> >   (multiple-value-bind (obj newpos)
> >        (read-from-string str nil :end-of-string :start pos)
> >     (if (eql obj :end-of-string)
> >        (nreverse list)
> >        (string-to-list str newpos (cons obj list)))))
> >
> > this functions does this:
> > (string-to-list "my car is red")
> > (MY CAR IS RED)
>
> How nice -- two blind men in the night have stumbled on each other :)
>

Sure, and you were born knowing everything.  We've all been there...
From: Davide
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <zW138.29218$tl3.933703@twister2.libero.it>
"Janis Dzerins" <·····@latnet.lv> ha scritto nel messaggio news:a2h82k$cf6
> 
> How nice -- two blind men in the night have stumbled on each other :)
> 

Is only for expert this forum?
I have posted my message only for a suggestion.

best regards
Davide U.
From: Brian P Templeton
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <87wuy8y0ca.fsf@tunes.org>
"Davide" <········@tin.it> writes:

> "Janis Dzerins" <·····@latnet.lv> ha scritto nel messaggio news:a2h82k$cf6
>> 
>> How nice -- two blind men in the night have stumbled on each other :)
>> 
> 
> Is only for expert this forum?
> I have posted my message only for a suggestion.
> 
No, this forum is for all Lisp programmers, from novices to experts.

> best regards
> Davide U.
> 
> 

-- 
BPT <···@tunes.org>	    		/"\ ASCII Ribbon Campaign
backronym for Linux:			\ / No HTML or RTF in mail
	Linux Is Not Unix			 X  No MS-Word in mail
Meme plague ;)   --------->		/ \ Respect Open Standards
From: Davide
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <hzW28.26060$tl3.820213@twister2.libero.it>
"ab talebi" <············@yahoo.com> ha scritto nel messaggio 
> 
> you might find this function interesting:
> 
> (defun string-to-list (str &optional (pos 0) (list nil))
>   (multiple-value-bind (obj newpos)
>        (read-from-string str nil :end-of-string :start pos)
>     (if (eql obj :end-of-string)
>        (nreverse list)
>        (string-to-list str newpos (cons obj list)))))
> this functions does this:
> (string-to-list "my car is red")
> (MY CAR IS RED)

WOW!  this function is wonderful!
I have noticed that every symbol has all the capitals letters or all the lower case letter.
If I want to preserve the letters in the original state, like could I do?

Example:   
(string-to-list "My car is red")  --> (My car is red)

Is it possible to do this with symbols or must I use the strings?
How would you do to resolve this problem?

Thank you for your help.
David U.
From: Kenny Tilton
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <3C4C3C5D.4C3B4F79@nyc.rr.com>
Davide wrote:

> Example:
> (string-to-list "My car is red")  --> (My car is red)

Consider this:

> (intern "My")
|My|
NIL
> (symbol-name *)
"My"

kenny
clinisys
From: Barry Margolin
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <gS138.25$hI5.299568@burlma1-snr2>
In article <······················@twister2.libero.it>,
Davide <········@tin.it> wrote:
>I have noticed that every symbol has all the capitals letters or all the
>lower case letter.
>If I want to preserve the letters in the original state, like could I do?
>
>Example:   
>(string-to-list "My car is red")  --> (My car is red)
>
>Is it possible to do this with symbols or must I use the strings?
>How would you do to resolve this problem?

(let ((*readtable* (copy-readtable)))
  (setf (readtable-case *readtable*) :preserve)
  (string-to-list "My car is red"))
--> (|My| |car| |is| |red|)

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Barry Margolin
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <tT138.26$hI5.299786@burlma1-snr2>
In article <················@news.uio.no>,
ab talebi <············@yahoo.com> wrote:
>you might find this function interesting:
>
>(defun string-to-list (str &optional (pos 0) (list nil))
>  (multiple-value-bind (obj newpos)
>       (read-from-string str nil :end-of-string :start pos)
>    (if (eql obj :end-of-string)
>       (nreverse list)
>       (string-to-list str newpos (cons obj list)))))
>
>this functions does this:
>(string-to-list "my car is red")
>(MY CAR IS RED)

(string-to-list "my car is :end-of-string red")
(MY CAR IS)

:)

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Geoffrey Summerhayes
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <9Z438.4262$JM6.1236997@news20.bellglobal.com>
"Barry Margolin" <······@genuity.net> wrote in message
························@burlma1-snr2...
> In article <················@news.uio.no>,
> ab talebi <············@yahoo.com> wrote:
> >
> >this functions does this:
> >(string-to-list "my car is red")
> >(MY CAR IS RED)
>
> (string-to-list "my car is :end-of-string red")
> (MY CAR IS)
>
> :)

(defun read-list (stream)
  (let ((end (gensym))
        (list nil))
    (do ((obj (read stream nil end)
              (read stream nil end)))
        ((eq obj end)(nreverse list))
      (push obj list))))

(defun string-to-list (string)
  (with-input-from-string (stream string)
    (read-list stream)))

> (string-to-list "my car is end red")
(MY CAR IS END RED)

:P

----------------
Geoff
From: Wade Humeniuk
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <a2imbu$lbf$1@news3.cadvision.com>
"Geoffrey Summerhayes" <·············@hotmail.com> wrote in message
···························@news20.bellglobal.com...
>
> (defun read-list (stream)
>   (let ((end (gensym))
>         (list nil))
>     (do ((obj (read stream nil end)
>               (read stream nil end)))
>         ((eq obj end)(nreverse list))
>       (push obj list))))
>
> (defun string-to-list (string)
>   (with-input-from-string (stream string)
>     (read-list stream)))
>
> > (string-to-list "my car is end red")
> (MY CAR IS END RED)
>
> :P
>
> ----------------
> Geoff
>
>
>

Better if, (still can cause problems)

(defun read-list (stream)
   (let ((end "eof")
         (list nil))
     (do ((obj (read stream nil end)
               (read stream nil end)))
         ((eq obj end)(nreverse list))
       (push obj list))))

Wade
From: Geoff Summerhayes
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <WSf38.15364$XG4.645783@news2.calgary.shaw.ca>
"Wade Humeniuk" <········@cadvision.com> wrote in message
·················@news3.cadvision.com...
>
> Better if, (still can cause problems)
>
> (defun read-list (stream)
>    (let ((end "eof")
>          (list nil))
>      (do ((obj (read stream nil end)
>                (read stream nil end)))
>          ((eq obj end)(nreverse list))
>        (push obj list))))
>

Hmmph, error-handling is an area I haven't gotten
enough reading done or had practice with. Or did
you have something else in mind? How about this...
------------------------------
;;; lifted directly from HS
(defun read-new-value ()
   (format t "Enter a new value: ")
   (multiple-value-list (eval (read))))

(defun read-list (stream)
  (let ((ans nil)
        (end "end"))
    ;; First time using this kind of construct
    ;; How reliable is the chapter on conditions
    ;; in CLTL2? Is there a better way to do this?
    (restart-case
        (do ((x (read stream nil end)
                (read stream nil end)))
            ((eq x end)(nreverse ans))
          (push x ans))
      (use-collected () :report "Use list collected before error."
                     (return-from read-list (nreverse ans)))
      (use-new-answer (new-answer) :report "Enter return value."
                      :interactive read-new-value
                      (return-from read-list new-answer)))))

-------------------------------
CL-USER 22 > (string-to-list "my car ) is blue")

Error: Close parenthesis at top level.
  1 (continue) Use list collected before error.
  2 Enter return value.
  3 (abort) Return to level 0.
  4 Return to top loop level 0.

Type :b for backtrace, :c <option number> to proceed,  or :? for other options

CL-USER 23 : 1 > :c 1
(MY CAR)
----

Geoff
From: Delaregue
Subject: Re: Newbie: How to convert a string in symbol?
Date: 
Message-ID: <6b4aa54a.0201220720.586cfc53@posting.google.com>
I've found that reading a string char by char is the best solution is
most case as you can deal with punctuation like quote, comma etc...