From: ············@hotmail.com
Subject: Creating a read-from-string utility
Date: 
Message-ID: <1120181529.833211.272260@g43g2000cwa.googlegroups.com>
Hi. I'm trying to create a macro which functions similarly to
with-open-file.
Instead of reading from a file character-by-character, it would read
from string.

usage example:

(let ((mystr "hi"))
	(read-from-string mystr (str-read-char mystr))

lisp: #\h


I've tried a couple of different approaches to creating this function
to little avail. Here are

strike 1:

(defmacro read-from-string (string &body body)
    `(let ( (,string ',(copy-list `(,(eval `,string) ,(length `,(eval
`,string)) -1))) )
          ,@body))

It tries to call eval on the string's symbol before runtime, before
which ithas been bound. I thought this seemed fishy when I first coded
it, but since it worked when I was testing it in the console, I went
through with it.

strike 2:

(defmacro read-from-string (string &body body)
  (let ( (temp (gensym)) )
       `(let* ( (,temp ,string) (,string ',(copy-list `(,temp 0) )))
             ,@body))))

the idea was that the gensym would be bound to whatever string
originally was bound to. it (the gensym) would be paired up with the
index of the next character to be read.The length of our string would
be calculated at runtime using  (length (eval (second string)))

to give you an idea of how it would work, here is a related function:
(defun str-read-char (string)
  (if (>= (second string) (length (eval (first string))))
      nil
      (progn (setf (second string)
		   (1+ (second string)))
	     (elt (first string) (1- (second string))))))

I thought that let would somehow bind the gensym to the string's value
so that (eval gensym) would give me the string, but apparently it just
replaces all occurences inside the form. with the string. Right?

So it ended up not working.


Another approach would be to convert the string into a stream
structure so that "hello" would become (h (e (l (l (o))))),

It SHOULD be fairly straight forward to code, but I imagine the code
would be much less elegant.

I would like to read any available pointers/suggestions for better
solutions.

Also, is there a functional approach to this? I imagine it would be a
lot different from anything I've tried up until now, but I'd be
interested to read about (and possibly implement) it.

From: Joe Marshall
Subject: Re: Creating a read-from-string utility
Date: 
Message-ID: <k6kbnvze.fsf@comcast.net>
············@hotmail.com writes:

> Hi. I'm trying to create a macro which functions similarly to
> with-open-file.
> Instead of reading from a file character-by-character, it would read
> from string.

Why not use make-string-input-stream ?

-- 
~jrm
From: Barry Margolin
Subject: Re: Creating a read-from-string utility
Date: 
Message-ID: <barmar-CEE255.22034830062005@comcast.dca.giganews.com>
In article <············@comcast.net>,
 Joe Marshall <·············@comcast.net> wrote:

> ············@hotmail.com writes:
> 
> > Hi. I'm trying to create a macro which functions similarly to
> > with-open-file.
> > Instead of reading from a file character-by-character, it would read
> > from string.
> 
> Why not use make-string-input-stream ?

Or how about WITH-INPUT-FROM-STRING, which seems to do exactly what his 
macro does.

BTW, Common Lisp already has a function named READ-FROM-STRING, so you 
shouldn't try to use this as the name of your own macro or function.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Kent M Pitman
Subject: Re: Creating a read-from-string utility
Date: 
Message-ID: <uvf3vw871.fsf@nhplace.com>
Joe Marshall <·············@comcast.net> writes:

> ············@hotmail.com writes:
> 
> > Hi. I'm trying to create a macro which functions similarly to
> > with-open-file.
> > Instead of reading from a file character-by-character, it would read
> > from string.
> 
> Why not use make-string-input-stream ?

Or if you prefer a macro, maybe WITH-INPUT-FROM-STRING.
From: Wade Humeniuk
Subject: Re: Creating a read-from-string utility
Date: 
Message-ID: <Yo1xe.69365$wr.5912@clgrps12>
············@hotmail.com wrote:
> Hi. I'm trying to create a macro which functions similarly to
> with-open-file.

That would be with-input-from-string

http://www.lispworks.com/documentation/HyperSpec/Body/m_w_in_f.htm


Wade