Hi,
I am a beginner to lisp and I use CLISP to learn.
I have recently found this function:
;Reads user input and turns the string into a list.
;From Winston-Horn, doesn't work with xlisp (1989)
(defun read-question ()
(with-input-from-string
(input (string-trim "!.?" (read-line)))
(do ((word (read input nil)
(read input nil))
(sentence nil))
((not word) (return (reverse sentence)))
(push word sentence))))
Is there another smaller function for read a question?
Can anybody help me?
Thanks
Dave
From: Barry Margolin
Subject: Re: [newbie] I need a function for read user input.
Date:
Message-ID: <VuFPa.52$0z4.20@news.level3.com>
In article <························@twister2.libero.it>,
Dave <·········@iol.it> wrote:
>Hi,
>I am a beginner to lisp and I use CLISP to learn.
>I have recently found this function:
>
>;Reads user input and turns the string into a list.
>;From Winston-Horn, doesn't work with xlisp (1989)
>(defun read-question ()
> (with-input-from-string
> (input (string-trim "!.?" (read-line)))
> (do ((word (read input nil)
> (read input nil))
> (sentence nil))
> ((not word) (return (reverse sentence)))
> (push word sentence))))
>
>
>Is there another smaller function for read a question?
>Can anybody help me?
YES-OR-NO-P and Y-OR-N-P can be used to ask yes/no questions. Otherwise,
you can just use READ-LINE and return the whole string, rather than
splitting it up into words like READ-QUESTION does.
--
Barry Margolin, ··············@level3.com
Level(3), 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.
Dave wrote:
> Hi,
> I am a beginner to lisp and I use CLISP to learn.
> I have recently found this function:
>
> ;Reads user input and turns the string into a list.
> ;From Winston-Horn, doesn't work with xlisp (1989)
> (defun read-question ()
> (with-input-from-string
> (input (string-trim "!.?" (read-line)))
> (do ((word (read input nil)
> (read input nil))
> (sentence nil))
> ((not word) (return (reverse sentence)))
> (push word sentence))))
>
>
> Is there another smaller function for read a question?
> Can anybody help me?
>
> Thanks
> Dave
>
>
Hi Dave --
Can you be more specific as to what you want to do with the user's input?
Regs,
Henry
"synthespian" <···········@uol.com.br> ha scritto nel messaggio
···················@ID-78052.news.uni-berlin.de...
> Dave wrote:
> > Hi,
> > I am a beginner to lisp and I use CLISP to learn.
> > I have recently found this function:
> >
> > ;Reads user input and turns the string into a list.
> > ;From Winston-Horn, doesn't work with xlisp (1989)
> > (defun read-question ()
> > (with-input-from-string
> > (input (string-trim "!.?" (read-line)))
> > (do ((word (read input nil)
> > (read input nil))
> > (sentence nil))
> > ((not word) (return (reverse sentence)))
> > (push word sentence))))
> >
> >
> > Is there another smaller function for read a question?
> > Can anybody help me?
> >
> > Thanks
> > Dave
> >
> >
> Hi Dave --
>
> Can you be more specific as to what you want to do with the user's
input?
>
> Regs,
>
> Henry
>
I want to do a question/answer program and I need a function for user's
input (sentence).
The function (read-question()) is too complex for me because there are
some keywords that I never used.
Could you explain to me this function?? :-)
thanks in advance.
bye
Dave
Dave wrote:
> "synthespian" <···········@uol.com.br> ha scritto nel messaggio
> ···················@ID-78052.news.uni-berlin.de...
>
>>Dave wrote:
>>
>>>Hi,
>>>I am a beginner to lisp and I use CLISP to learn.
>>>I have recently found this function:
>>>
>>>;Reads user input and turns the string into a list.
>>>;From Winston-Horn, doesn't work with xlisp (1989)
>>>(defun read-question ()
>>> (with-input-from-string
>>> (input (string-trim "!.?" (read-line)))
>>> (do ((word (read input nil)
>>> (read input nil))
>>> (sentence nil))
>>> ((not word) (return (reverse sentence)))
>>> (push word sentence))))
>>>
>>>
>>>Is there another smaller function for read a question?
>>>Can anybody help me?
>>>
>>>Thanks
>>>Dave
>>>
>>>
>>
>>Hi Dave --
>>
>> Can you be more specific as to what you want to do with the user's
>
> input?
>
>> Regs,
>>
>> Henry
>>
>
>
> I want to do a question/answer program and I need a function for user's
> input (sentence).
> The function (read-question()) is too complex for me because there are
> some keywords that I never used.
>
> Could you explain to me this function?? :-)
>
> thanks in advance.
> bye
> Dave
>
Dave --
If it's a "yes or no" question, there's the (y-or-n-p [format strings
[args]]) or the (yes-or-no-p [format strings [args]]), as somebody's
already answered (the difference is one will take "n" for ans answer,
and the other will take "no").
The function above is not so complex. I suspect your problem is related
to the DO macro. Am I right? DO /is/ kind of complex. I can post a
diagram with the DO "algorithm", if you want.
Please be more specific as to what you wish to do (write some
pseudocode). I'm a newbie too, but I'll try to help if I can.
Henry
"synthespian" <···········@uol.com.br> ha scritto nel messaggio
···················@ID-
>
> If it's a "yes or no" question, there's the (y-or-n-p [format strings
> [args]]) or the (yes-or-no-p [format strings [args]]), as somebody's
> already answered (the difference is one will take "n" for ans answer,
> and the other will take "no").
It's not a "yes or no" question but a complete sentence.
[NOUN GROUP]+[VERB GROUP]+[.....]
> The function above is not so complex. I suspect your problem is
related
> to the DO macro. Am I right? DO /is/ kind of complex. I can post a
> diagram with the DO "algorithm", if you want.
Thank you but It's not necessary.
For example, why (read input nil) is used for two times?
> Please be more specific as to what you wish to do (write some
> pseudocode). I'm a newbie too, but I'll try to help if I can.
>
> Henry
>
Gareth McCaughan has explained sufficiently well..
Thanks again.
:-)
bye
Dave
"Dave" <·········@iol.it> ha scritto nel messaggio news:VocQa.170534
>
> Thank you but It's not necessary.
> For example, why (read input nil) is used for two times?
>
...but Gareth already explained this code.
Excuse me.
:)
>> This form occurs twice: as the initial value for WORD
>> and as its update form. So, each time around the loop
>> WORD will get set to the result of reading a single
>> object from the INPUT stream; eventually it will be
>> set to NIL, when we have reached the end of the string.
bye
Dave
Dave Someoneorother wrote:
>>> I am a beginner to lisp and I use CLISP to learn.
>>> I have recently found this function:
>>>
>>> ;Reads user input and turns the string into a list.
>>> ;From Winston-Horn, doesn't work with xlisp (1989)
>>> (defun read-question ()
>>> (with-input-from-string
>>> (input (string-trim "!.?" (read-line)))
>>> (do ((word (read input nil)
>>> (read input nil))
>>> (sentence nil))
>>> ((not word) (return (reverse sentence)))
>>> (push word sentence))))
...
> The function (read-question()) is too complex for me because there are
> some keywords that I never used.
>
> Could you explain to me this function?? :-)
Yeah, OK. :-)
Working from the outside in:
(defun read-question () ...)
We're defining a function that takes no arguments,
called READ-QUESTION.
(with-input-from-string (input ...) ...)
Take a string. Turn it into an input stream --
a thing you can treat in the same way as an open
file. Bind that to the name INPUT, and do some
stuff with that binding in effect.
(string-trim "!.?" (read-line))
Read a line of input, producing a string. Then
strip off the end any instances of the characters
#\!, #\. and '\? .
The string that results from evaluating this form
is what gets turned into a stream and called INPUT.
(do (...) (...) ...)
Set up some variables. Then, repeatedly,
(1) check for an end condition and, if it's met,
return a specified value.
(2) do some other stuff.
(3) update the variables.
(do ((word ... ...) (sentence nil)) (...) ...)
The variables we're setting up are called WORD
and SENTENCE. WORD has two expressions attached
to it; the first is its initial value, the second
is what gets used to update it. SENTENCE has only
one, so it gets an initial value and then isn't
updated.
(read input nil)
Read an object from the input stream called INPUT.
If we reach end-of-file -- which, here, means the
end of the string, don't signal an error. (The value
NIL will be returned instead, but note that the NIL
there means "no, don't signal an error"; if you
want to specify a different value to use on EOF
you have to do so separately.)
This form occurs twice: as the initial value for WORD
and as its update form. So, each time around the loop
WORD will get set to the result of reading a single
object from the INPUT stream; eventually it will be
set to NIL, when we have reached the end of the string.
(do (...) ((not word) ...) ...)
The termination condition for the loop is (NOT WORD).
In other words, after the initial setup of the variables
and after each update we check whether (NOT WORD) is
true. If it is, we're done.
(return (reverse sentence))
Look at the value of SENTENCE, which should be a list.
Return the list that results from reversing it.
Two comments on that RETURN. Firstly, it doesn't
return from the *function* but from the *loop*.
More precisely, it returns from a block called NIL
that's implicitly set up by the DO form. In this
case, the value of the function is the value of the
WITH-INPUT-FROM-STRING form, which is the value of
the DO form, so returning from the loop gives the
same result as returning from the function. But
that isn't true in general.
Secondly, the RETURN is completely unnecessary here.
The expression in a DO loop's termination clause is
always used as the return value of the loop. So the
termination clause could just have said
((not word) (reverse sentence))
and it would have worked just as well.
(push word sentence)
Take the value of WORD and stick it onto the front
of the list that's the value of SENTENCE. Make the
resulting list be the value of SENTENCE.
Doing this repeatedly has the result that the first
thing pushed is at the end of the list, and the last
thing pushed is at the beginning. That's why we reverse
the list at the end.
Putting the whole thing together, it means:
Read a line of input. Repeatedly grab things
from it using READ. Put them in a list. The
value of the function is that list.
The code feels rather old-fashioned to me. The DO loop
could more perspicuously be replaced with
(loop for word = (read input nil) while word collect word)
which even the strictest LOOP-hater would have to agree
is clearer.
--
Gareth McCaughan
"Gareth McCaughan" <·····@g.local> ha scritto nel messaggio
···················@g.mccaughan.ntlworld.com...
> >
> > Dave Someoneorother wrote:
> >
> > Could you explain to me this function?? :-)
>
> Yeah, OK. :-)
>
Thank you very much!
I shall use your explanation as a part of manual. :-)
best regards
Dave