From: Amarnath Arsikere
Subject: Converting a string to a list
Date: 
Message-ID: <39039A1C.61FAE12C@ttd.teradyne.com>
Hi,

You would think that this would be a simple affair.  But I do not
seem to find the appropriate routines to convert a string to a list in
lisp.

For example, if I try to use read-from-string recursively it doesn't
seem
to work - coz it seems to ignore :start keyword (I am using Allegro cl
5.0.1)

If I try (read-from-string "who is jill")  and
              who
               4
              (read-from-string "who is jill" :start 4)
               who
                4

All I want to do is convert
"who is jill" to '(who is jill)

My only last resort is to write a parser using (read-char)  and
recognize words
in a sentence to place them into list - the benefits of using lisp is
then shattered


Thanks in advance
Amar


-------------------------------------------------

From: Dennis Marti
Subject: Re: Converting a string to a list
Date: 
Message-ID: <dennis_marti-022DC5.23524423042000@news.starpower.net>
In article <·················@ttd.teradyne.com>, Amarnath Arsikere 
<····@ttd.teradyne.com> wrote:

> For example, if I try to use read-from-string recursively it doesn't
> seem
> to work - coz it seems to ignore :start keyword (I am using Allegro cl
> 5.0.1)

It doesn't ignore them, it uses them as EOF-ERROR-P and EOF-VALUE; the 
two optional arguments to READ-FROM-STRING.

> If I try (read-from-string "who is jill")  and
>               who
>                4
>               (read-from-string "who is jill" :start 4)

(read-from-string "who is jill" nil nil :start 4)

Dennis
From: Jonathan BAILLEUL
Subject: Re: Converting a string to a list
Date: 
Message-ID: <39044305.B34758B4@emi.u-bordeaux.fr>
Amarnath Arsikere wrote:
> 
> Hi,
> 
> You would think that this would be a simple affair.  But I do not
> seem to find the appropriate routines to convert a string to a list in
> lisp.
> 
> For example, if I try to use read-from-string recursively it doesn't
> seem
> to work - coz it seems to ignore :start keyword (I am using Allegro cl
> 5.0.1)
> 
> If I try (read-from-string "who is jill")  and
>               who
>                4
>               (read-from-string "who is jill" :start 4)
>                who
>                 4
> 
> All I want to do is convert
> "who is jill" to '(who is jill)
> 
> My only last resort is to write a parser using (read-char)  and
> recognize words
> in a sentence to place them into list - the benefits of using lisp is
> then shattered
> 
> Thanks in advance
> Amar
> 
> -------------------------------------------------
I have a little solution for your problem.
(str-tokenize "ti,ti ta\"ta tu")
-->("ti" "ti" "ta" "ta" "tu")

I think I've already posted it for another purpose... 
One obvious problem is the consing of a list: you should consider it if
you intend to use it repeatedly. 
Maybe the use of a memory pool should solve it... 

Let me know if you can improve this solution.

;;;-----------------------------------------------------------------------------
;;; Tool functions from Paul Graham
;;;-----------------------------------------------------------------------------


;;Paul Graham's 'Ansi Common Lisp'
(defun tokens (str test start)
  (let ((p1 (position-if test str :start start)))
    (if p1
	(let ((p2 (position-if #'(lambda (c) 
				   (not (funcall test c)))
			       str :start p1)))
	  (cons (subseq str p1 p2)
		(if p2 
		    (tokens str test p2) 
		  nil)))
      nil)))


;;;-----------------------------------------------------------------------------
;;; String Functions
;;;-----------------------------------------------------------------------------


(defun str-exclude (x)
  "return nil if the argument is a character which should not appear in
the
final string."
  (or (eq #\SPACE x)
      (eq #\" x)
      (eq #\, x)))


(defun str-tokenize (string)
  "return a list of tokens, ie #\SPACE-separated elements, from given
string.
Notice that encountered strings are included without double-quotes."
(let ((nospc (lambda (x) (not (str-exclude x)))))
  (tokens string nospc 0)))



-- 
----------------------------------------------
Jonathan BAILLEUL (········@emi.u-bordeaux.fr)
Maitrise Informatique, Universite Bordeaux I
From: David Hanley
Subject: Re: Converting a string to a list
Date: 
Message-ID: <3904648E.7C40F8CF@ncgr.org>
Your post seemed to indicate you want the program to emit symbols...

Probably the most efficient solution is the string tokenizer
already posted.  If you want quick-and-dirty, you can
use the following:

(defun tokenize( str )(read-from-string (concatenate 'string "(" str
")")))

You could, by special effort, be even less efficient by using (format ...)
instead
of concatenate.   :)

I'm joking of course.  The above is probably fully sufficient for what you
will
be doing.

dave
From: Courageous
Subject: Re: Converting a string to a list
Date: 
Message-ID: <39039CBF.9F0C2FDA@san.rr.com>
> My only last resort is to write a parser using (read-char)  and
> recognize words
> in a sentence to place them into list - the benefits of using lisp is
> then shattered

No. I know you don't have to do this. I don't have a lisp
interpreter sitting in front of me, and I'm fairly new at
this, so hopefully someone who knows the *right* way to do
this can step in.

In a recent project, I am through a socket interface given
strings of the following form:

"(asd asd asd a sd asd \"asd asd asd asd\")"

(Which is to say, the string contains values, parens, and
embedded double quotes which signify strings).

You do something like (read-from-string) to tell lisp to
take a string and turn it into a bona-fide lisp object
(in this case a list). The obvious -- albeit hackish and
probably wrong -- solution might include you doing something
like concatenating parens on either end of your string
and then calling (read-from-string) on that (btw, I'm
not sure about the function name).

I'm guessing, however, that you ought to find something
more direct than that.

I've found that I can figure out most anything I need to
do in Lisp from Steele's book, which appears to be a very
thorough reference. In all the various documentation on
the read-from* forms, read-macros, and so forth, you
should be able to find the clues that you are looking
for.

Hopefullly someone can give you a more exact answer....



C/
From: Erik Naggum
Subject: Re: Converting a string to a list
Date: 
Message-ID: <3165559641278849@naggum.no>
* Courageous <········@san.rr.com>
| I don't have a lisp interpreter sitting in front of me, and I'm fairly
| new at this, so hopefully someone who knows the *right* way to do this
| can step in.

  patience is a virtue.  if you don't know the answer, somebody else will
  most probably post it in a very short time after a question has been
  posted.  (as indeed happened here.  Dennis Marti saw the mistake right
  away and responded.)  if you post garbage, it needs to be cleaned up.
  save us all the trouble and don't post garbage that needs cleaning up.

#:Erik