From: Konst Sushenko
Subject: scanning a string?
Date: 
Message-ID: <7Vhx5.19643$6f1.958577@bgtnsc05-news.ops.worldnet.att.net>
hello,

i am not a student, but i still have a lot to learn, for example, what is
the best and most efficient way in lisp to scan through a string of
characters?

say i want to write a function that given a regular expression string
converts it into a symbolic expression:

(regexp->sym "a|b*) => '(OR A (LOOP* B))

currently i do it using with-input-from-string. my implementation is split
into several sub-functions, and it is very convenient to pass the string
stream into these functions and then pick up whatever left in the stream
after the call returns.

what confuses me as a lisp beginner is that much lisp code that i have seen
does not deal with streams, instead people seem to prefer converting strings
into lists of chars, and working with car/cdr, etc to scan the string.

but if i convert my regexp string into a list before processing it, then i
would have to return multiple values from my sub-functions: the result and
the remaining input list...

any word from experts?

thanks
konst

From: Rob Warnock
Subject: Re: scanning a string?
Date: 
Message-ID: <8q4ihc$tskjg$1@fido.engr.sgi.com>
Konst Sushenko <·····@worldnet.att.net> wrote:
+---------------
| but if i convert my regexp string into a list before processing it, then i
| would have to return multiple values from my sub-functions: the result and
| the remaining input list...
+---------------

There's no nead to avoid multiple values -- Common Lisp has them
conveniently built into the language. In fact, when writing tiny
parsers of various kinds, I often do exactly as you suggest: Call
a routine to parse some construct, and have it return the parsed
phrase/object/structure/whatever and the list of unused characters:

	(multiple-value-bind (result-thing remaining-chars)
	    (parse-a-thing some-chars)
	  ;; do something with the parsed "result-thing",
	  ;; handing the "remaining-chars" to the next parser
	  ... )

[Also see "multiple-value-setq".]


-Rob

p.s. But before taking me literally and heading off to code a regexp in
that form, first look in your Lisp implementation's documentation -- you
may find there's a perfectly usable regexp library there already!

-----
Rob Warnock, 41L-955		····@sgi.com
Network Engineering		http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.		Phone: 650-933-1673
1600 Amphitheatre Pkwy.		PP-ASEL-IA
Mountain View, CA  94043
From: Stig Hemmer
Subject: Re: scanning a string?
Date: 
Message-ID: <ekvu2bc9uen.fsf@lizard.pvv.ntnu.no>
"Konst Sushenko" <·····@worldnet.att.net> writes:
> what confuses me as a lisp beginner is that much lisp code that i
> have seen does not deal with streams, instead people seem to prefer
> converting strings into lists of chars, and working with car/cdr,
> etc to scan the string.

The problem is that most introductory Lisp courses doesn't say much
about streams.  They are considered too difficult for the poor little
students to handle.

As a result, a lot of Lisp programmers don't use streams a lot.

Also, Lisp attracts "functional fanatics", and they do _NOT_ like
streams as they represent global state.  Fie!

I would say you are on the right track, or at least _a_ right track,
don't let the people going in other directions confuse you.

Not an expert, but I play one on Usenet,

Stig Hemmer,
Jack of a Few Trades.