i have only just started into lisp and programming, and i am trying to
discover a way to write a programme that allows interaction between
faulty human and a perfect program. A simple way to say what i am
looking at is :-
(format nil "~r" RANDOM NUMBERS )
is there a way to programe so that an individual can have the ability
to write there own numbers to get there own answers and not the one
that i have programmed in????
scraggy wrote:
> i have only just started into lisp and programming, and i am trying to
> discover a way to write a programme that allows interaction between
> faulty human and a perfect program. A simple way to say what i am
> looking at is :-
>
> (format nil "~r" RANDOM NUMBERS )
>
> is there a way to programe so that an individual can have the ability
> to write there own numbers to get there own answers and not the one
> that i have programmed in????
(format t "~R" (read))
On Mon, 18 Sep 2006 17:25:33 -0700, John Stoneham wrote:
>
> scraggy wrote:
>> i have only just started into lisp and programming, and i am trying to
>> discover a way to write a programme that allows interaction between
>> faulty human and a perfect program. A simple way to say what i am
>> looking at is :-
>>
>> (format nil "~r" RANDOM NUMBERS )
>>
>> is there a way to programe so that an individual can have the ability to
>> write there own numbers to get there own answers and not the one that i
>> have programmed in????
>
> (format t "~R" (read))
Just so you do not wonder where your REPL suddenly went:
cl-user> (defun number-from-user ()
(princ "Please type a number and press enter: ")
(read))
number-from-user
cl-user> (+ 2 (number-from-user) 3)
Please type a number and press enter: 5
10
cl-user> (+ 2 5 3)
10
cl-user> (= (+ 2 (number-from-user) 3)
(+ 2 5 3))
Please type a number and press enter: 5
t
cl-user>
--
Lars Rune Nøstdal
http://lars.nostdal.org/
Lars Rune N�stdal <···········@gmail.com> writes:
> On Mon, 18 Sep 2006 17:25:33 -0700, John Stoneham wrote:
>
>>
>> scraggy wrote:
>>> i have only just started into lisp and programming, and i am trying to
>>> discover a way to write a programme that allows interaction between
>>> faulty human and a perfect program. A simple way to say what i am
>>> looking at is :-
>>>
>>> (format nil "~r" RANDOM NUMBERS )
>>>
>>> is there a way to programe so that an individual can have the ability to
>>> write there own numbers to get there own answers and not the one that i
>>> have programmed in????
>>
>> (format t "~R" (read))
>
> Just so you do not wonder where your REPL suddenly went:
>
> cl-user> (defun number-from-user ()
> (princ "Please type a number and press enter: ")
> (read))
> number-from-user
> cl-user> (+ 2 (number-from-user) 3)
> Please type a number and press enter: 5
>
> 10
> cl-user> (+ 2 5 3)
> 10
> cl-user> (= (+ 2 (number-from-user) 3)
> (+ 2 5 3))
> Please type a number and press enter: 5
>
> t
> cl-user>
Well, if you consider user interaction, you should probably use *query-io*,
and add some validation:
;; Actually, ~R expects an INTEGERN not just a NUMBER, so:
(defun INTEGER-from-user ()
(format *query-io* "~&Please type an INTEGER and press enter: ")
(let* ((eof (gensym)) ; let be explicit for the newbie's sake.
(data (read *query-io* nil eof)))
(cond
((eql data eof) (format t "~&End of user input.~%") nil)
((integerp data) data)
(t (format *query-io* "~%~S is not an INTEGER." data)
(real-from-user)))))
(let ((data (INTEGER-from-user)))
(unless (null data)
(format *query-io* "~R" data)))
Please type an INTEGER and press enter: #c(1 2)
#C(1 2) is not an INTEGER.
Please type a real and press enter: abc
ABC is not a real.
Please type a real and press enter: pi
PI is not a real.
Please type a real and press enter: 42/3
fourteen
NIL
--
__Pascal Bourguignon__ http://www.informatimago.com/
In a World without Walls and Fences,
who needs Windows and Gates?
As long as we're burdening the new user with technicalities, let's not
forget that READ will only return the first expression when multiple
expressions are entered, leaving the remaining unread expressions in
the input buffer. So, when placing the above INTEGER-from-user in a
loop form to process input multiple times, the following will happen
when the user inputs more than one number at the prompt:
Please type an INTEGER and press enter: 4 5
four
Please type an INTEGER and press enter: five
So it's probably better to use READ-FROM-STRING on READ-LINE instead of
the one call to READ.
"John Stoneham" <··············@gmail.com> writes:
> As long as we're burdening the new user with technicalities, let's not
> forget that READ will only return the first expression when multiple
> expressions are entered, leaving the remaining unread expressions in
> the input buffer. So, when placing the above INTEGER-from-user in a
> loop form to process input multiple times, the following will happen
> when the user inputs more than one number at the prompt:
>
> Please type an INTEGER and press enter: 4 5
> four
> Please type an INTEGER and press enter: five
>
> So it's probably better to use READ-FROM-STRING on READ-LINE instead of
> the one call to READ.
Indeed. We may even want to parse user input according to locale,
such as accepting:
1,234,567
or:
1.234.567
for one million two hundred thirty four thousand five hundred and sixty seven.
Or, if we stay with the lisp READer, at least, insert a
(clear-input *query-io*)
at the start of integer-from-user to just ignore the remaining input,
and not forgetting (let ((*read-eval* nil)) ...) like I did.
(defun INTEGER-from-user ()
(let ((*read-eval* nil)
(*read-base* 10.))
(clear-input *query-io*)
(format *query-io*
"~&Please type an INTEGER in base Ten and press enter: ")
(let* ((eof (gensym)) ; let be explicit for the newbie's sake.
(data (read *query-io* nil eof)))
(cond
((eql data eof) (format t "~&End of user input.~%") nil)
((integerp data) data)
(t (format *query-io* "~%~S is not an INTEGER." data)
(real-from-user))))))
--
__Pascal Bourguignon__ http://www.informatimago.com/
This is a signature virus. Add me to your signature and help me to live.
"scraggy" <············@aol.com> writes:
> i have only just started into lisp and programming, and i am trying to
> discover a way to write a programme that allows interaction between
> faulty human and a perfect program. A simple way to say what i am
> looking at is :-
>
> (format nil "~r" RANDOM NUMBERS )
>
> is there a way to programe so that an individual can have the ability
> to write there own numbers to get there own answers and not the one
> that i have programmed in????
You seem to be asking for something like this:
(progn
(print "Enter first number: ")
(let ((first-number (read)))
(check-type first-number number)
(print "Enter second number: ")
(let ((second-number (read)))
(check-type second-number number)
(format t "~&the total is ~A"
(+ first-number
second-number))
(format t "~&The production is ~A"
(* first-number
second-number)))))
I don't find much play value in this. I have more fun using
the Lisp REPL as a programmable calculator, writing my own
little functions, and then playing with them at the REPL.
For example:
CL-USER> (defun my-sqrt (x)
(do ((sqrt 1 (/ (+ sqrt
(/ x sqrt))
2)))
((< (abs (- x (expt sqrt 2)))
*tolerance*) sqrt)))
MY-SQRT
CL-USER> (defparameter *tolerance* 1/1000000)
*TOLERANCE*
CL-USER> (my-sqrt 2)
665857/470832
CL-USER> (my-sqrt 2.0)
1.4142135
CL-USER> (float **) <-- double asterisk is the result two before
1.4142135
CL-USER> (my-sqrt 2)
665857/470832
CL-USER> (* * *) <-- initial asterisk is multiply, next two
asterisks both refer to previous result
443365544449/221682772224
CL-USER> (float *)
2.0
CL-USER> (- 2 (* (my-sqrt 2)
(my-sqrt 2)))
-1/221682772224
Alan Crowe
Edinburgh
Scotland