From: ···········@gmail.com
Subject: Making a Calculator program in LISP
Date: 
Message-ID: <1176660244.326161.296090@b75g2000hsg.googlegroups.com>
I am writing in Common Lisp...i am creating a very very simple version
of a calculator for one of my classes.  I am having a problem reading
in the ANS and then having it go to the appropriate code and
complete.  I know this code is not pretty and that is because I have
never written a LISP program before I am merely writing this for class
and this is our famaliarize yourself with LISP program sort of thing.

I appreciate any help that anyone gives and any suggestions but
remember i am a newbie so If you would clearly explain any of the help
you give it would help me understand and learn this language even
better.

Also if i can get the ANS to read eventually, hopefully with someone's
help here then I would also like to make this program exit only when
the user tells it to by entering in "5" other than that I would love
to learn how to implement a Loop to keep the menu ontop after it
completes each function.

Any help is appreciated, iv been scratching me head over this for a
long time now and i just can't understand why it won't read in ANS.

(print "ENTER 1 for ADDITION:")
(print "ENTER 2 for SUBTRACTION:")
(print "ENTER 3 for DIVISION:")
(print "ENTER 4 for MULTIPLICATION")
(print "ENTER 5 to EXIT")
(read ANS)
(DEFUN MENU (ANS)
  (COND (1 (ADDITION (NUM1 NUM2)
   (2 (SUBTRACTION (NUM1 NUM2)
  (3 (DIVISION (NUM1 NUM2)
    (4 (MULTIPLICATION (NUM1 NUM2)
    (5 (print "DONE")))
(DEFUN ADDITION (NUM1 NUM2) (+ NUM1 NUM2))
(DEFUN SUBTRACTION (NUM1 NUM2) (- NUM1 NUM2))
(DEFUN DIVISION (NUM1 NUM2) (/ NUM1 NUM2))
(DEFUN MULTIPLICATION (NUM1 NUM2) (* NUM1 NUM2))

From: ·······@googlemail.com
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <1176667710.249775.176290@n59g2000hsh.googlegroups.com>
Your cond seems false to me.  Numbers are always evaluate to true.
So always the first clause is of the cond is evaluated.  Your
implementation should warn you about that.

try:

(defun menu (op v1 v2)
  (cond ((equal ans '+) (+ v1 v2))
        ((equal ans '-) (- v1 v2))
  ;; etc
  ))


Thomas
From: Frank Buss
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <1igdviibqkkzm.1qdp8mrzzayds$.dlg@40tude.net>
···········@gmail.com wrote:

> Any help is appreciated, iv been scratching me head over this for a
> long time now and i just can't understand why it won't read in ANS.

A good idea is to read the documentation:

http://www.lisp.org/HyperSpec/Body/fun_readcm_re_g-whitespace.html

Your "ANS" is used as an input-stream, which doesn't work, because you
don't have defined it before. But you don't need it, because READ uses the
standard input stream, if it is not specified. One example in the
documentation shows you how to use it like this. It returns the object,
which was read.

To exit the program only, when someone enters "5", you should use a loop.
One example for the LOOP macro shows this, but with parse-integer and it
stops, if it is not an integer:

http://www.lisp.org/HyperSpec/Body/mac_loop.html

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: ···········@gmail.com
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <1176665068.895710.107290@p77g2000hsh.googlegroups.com>
On Apr 15, 2:37 pm, Frank Buss <····@frank-buss.de> wrote:
> ···········@gmail.com wrote:
> > Any help is appreciated, iv been scratching me head over this for a
> > long time now and i just can't understand why it won't read in ANS.
>
> A good idea is to read the documentation:
>
> http://www.lisp.org/HyperSpec/Body/fun_readcm_re_g-whitespace.html
>
> Your "ANS" is used as an input-stream, which doesn't work, because you
> don't have defined it before. But you don't need it, because READ uses the
> standard input stream, if it is not specified. One example in the
> documentation shows you how to use it like this. It returns the object,
> which was read.
>
> To exit the program only, when someone enters "5", you should use a loop.
> One example for the LOOP macro shows this, but with parse-integer and it
> stops, if it is not an integer:
>
> http://www.lisp.org/HyperSpec/Body/mac_loop.html
>
> --
> Frank Buss, ····@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de

> standard input stream, if it is not specified. One example in the
> documentation shows you how to use it like this. It returns the object,
> which was read.
>
> To exit the program only, when someone enters "5", you should use a loop.
> One example for the LOOP macro shows this, but with parse-integer and it
> stops, if it is not an integer:
>
> http://www.lisp.org/HyperSpec/Body/mac_loop.html
>
> --
> Frank Buss, ····@frank-buss.dehttp://www.frank-buss.de,http://www.it4-systems.de

I don't really understand how to implement what they are showing me.
I am really new to this and I posted on here for mainly a clearer
explanation of how to go about completing this project and I'm sure
that reading in the value from the menu isnt going to be my only
probelm but I hope that it will be the only one I cannot figure out on
my own.

Can anyone explain this read function stuff any clearer or explain how
i may implement it
From: Kent M Pitman
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <ulkgsrhmq.fsf@nhplace.com>
···········@gmail.com writes:

> I don't really understand how to implement what they are showing me.
> I am really new to this and I posted on here for mainly a clearer
> explanation of how to go about completing this project and I'm sure
> that reading in the value from the menu isnt going to be my only
> probelm but I hope that it will be the only one I cannot figure out on
> my own.

READ is a function that when called will take characters off of the
input stream (given as an argument or inferred from the dynamic context
if no argument given) and will return a parsed token.

e.g., if you define:

 (defun add-two-numbers ()
   (+ (read) (read)))

and then you call this function, it will read two numbers from the 
current input stream and add them.  For example:

 (add-two-numbers)
 3
 4
 => 7

If you call the same function in a loop, you have to arrange to save
and/or print the result, since the only reason it gets printed in the
above example is that when add-two-numbers is called from the 
read-eval-print loop, the result is printed by the read-eval-print loop.
So you can do:

 (loop (print (add-two-numbers)))

and it will let you see the following, where "<" denotes an input line
and ">" denotes an output line, even though no such characters will
appear on your console:

 < 3
 < 4
 > 7
 < 2 2
 > 4
 < 2
 <
 < 3
 > 5

You'll have to use some interrupt character to stop such a program since
it has no provision to stop.  If you instead do:

 (defun adder ()
   (loop (let ((n1 (read)) (n2 (read)))
           (if (or (equal n1 'stop) (equal n2 'stop))
               (return 'done)
               (print (list 'sum (+ n1 n2)))))))
 (adder)
 
You will get something that feels like this (where again the < and > 
just show you the directionality but won't show up on your console):

 < 3
 < 4
 > (SUM 7)
 < 5 5
 > (SUM 10)
 < 2 STOP
 > DONE

and then it will be stopped gracefully.

You could also write some function that prompted for a number giving you
a named prompt of some kind:

 (defun prompt-for-value (what)
   (print (list 'enter what))
   (read))

Then you could type this to Lisp:

 (list 'sum 'is (+ (prompt-for-value 'n1) (prompt-for-value 'n2)))

and see:

 (ENTER N1) 37
 (ENTER N2) 4
 (SUM IS 41)

Is the project actually to write some sort of menu choice facility?
Or is it to write a calculator?  Calculators normally don't have menus.
That doesn't make them simple, it makes them complicated.

> Can anyone explain this read function stuff any clearer
> or explain how i may implement it

Hopefully the above will help.  I have to sleep now.  Someone else
will take over for me from here.
From: Don Geddis
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <878xcsi3gs.fsf@geddis.org>
···········@gmail.com wrote on 15 Apr 2007 11:0:
> I know this code is not pretty and that is because I have never written a
> LISP program before I am merely writing this for class and this is our
> famaliarize yourself with LISP program sort of thing.

A lot of people here are trying to help you, but I have to say it really
looks like you understand almost nothing about the language.  You really
ought to start with a simple tutorial or some kind of instruction, rather
than trying to write a new program by scratch using basically trial and
error.

> I am having a problem reading in the ANS and then having it go to the
> appropriate code and complete.
> Any help is appreciated, iv been scratching me head over this for a
> long time now and i just can't understand why it won't read in ANS.

Well, I can help with this one part.  And maybe it'll get you on the right
track.  But really, you've got a LOT of misconceptions about how any of Lisp
works.  You need to look at some illustrated, explained examples before you
try just typing new code.

But in any case:

> (read ANS)

Others have tried to explain why this is wrong.  At the least, you can try
this instead:
        (setq ans (read))
        (print "You typed: ")
        (print ans)

Maybe that'll get you a little bit farther.

        -- Don
_______________________________________________________________________________
Don Geddis                  http://don.geddis.org/               ···@geddis.org
As Larry watched, the big hammerhead shark circled closer and closer.  His eyes
were empty and death-like, and so were the shark's.  He thought of all the
things he should have said to Linda, like "Help!  A shark is attacking me!"
	-- Deep Thoughts, by Jack Handey [1999]