From: Pascal Bourguignon
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <878xcthrci.fsf@voyager.informatimago.com>
···········@gmail.com writes:

> Ok I am a newbie at LISP and I am doing a project for class and im
> trying to make a simple (very simple) calculator that just does + - /
> * functions.

I would advise you to read some good book (or study more your class
materials, if they're good enough).

Try:
    Common Lisp: A Gentle Introduction to Symbolic Computation
    David S. Touretzky
    http://www-cgi.cs.cmu.edu/afs/cs.cmu.edu/user/dst/www/LispBook/index.html
    http://www.cs.cmu.edu/~dst/LispBook/



> THis is what i have:
>
> (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))
>
> My not so great knowledge of LISP...i am writing this currently in
> Common Lisp on the windows version of Corman LISP, evaluation copy.
>
> My main problem right now is how to read in the answer (ANS) from the
> menu so it evaluates what i have written and does what the program
> says.
>
> Any help on improving this program a little would be great and also
> help regarding a loop so that it only exits when the user enters 5
> would be great but currently I just want to get it working so I can
> enter a value, have that value understood and then have it complete a
> function (addition, subtractiong etc.)

Programming is like playing lego.  You have little bricks (the
operators of the programming language) and you can combine them in
certain ways, depending on the form of the brick, to do nice things.
The important part in that is to know your little bricks.  In Common
Lisp, there are 972 different little bricks, and while you don't need
to know all of them (you can start building interesting stuff with
half a dozen of them), you still have to know how they work and how
you can combine them.  You can do that by reading reference material
(usually some reference is cited in your courses, or in the tutorial
or book you study with, or you can read the original: CLHS
http://www.lispworks.com/documentation/HyperSpec/Front/Contents.htm

You don't need to read it cover to cover, but when you want to use a
little brick you don't know, like READ or PRINT, you'd better read the
corresponding CLHS page, and see how it's used.

In the case of READ for example, the first argument it takes is not
the variable where to store what is read, but it's the stream it reads
from.  The value read is returned as result of the function READ.

So you'd have rather (let ((ans (read))) ...)


Finally, I don't know the specifications of your class project, but in
lisp you can read something else than just numbers, you could just
read the operators:

(defun calc ()
 (loop
   (terpri) (terpri)
   (princ "Enter operator, follwed by two numbers, or type QUIT: ")
   (let ((op (read)))
     (cond
       ((member op '(+ - * /))
           (let ((arg1 (read))
                 (arg2 (read)))
             (terpri)
             (princ "The result is: ")
             (princ (funcall op arg1 arg2))))
       ((eq op 'quit)
         (return-from calc (values)))
       (t (terpri)
          (princ "Unknown operator: ") (princ op))))))



[115]> (calc)


Enter operator, follwed by two numbers, or type QUIT: + 3 4

The result is: 7

Enter operator, follwed by two numbers, or type QUIT: * 4 5

The result is: 20

Enter operator, follwed by two numbers, or type QUIT: / 1 0

The result is: 
*** - division by zero
Break 1 [116]> :rt

Values: 'infinity
INFINITY

Enter operator, follwed by two numbers, or type QUIT: sin pi

Unknown operator: SIN

Enter operator, follwed by two numbers, or type QUIT: 
Unknown operator: PI

Enter operator, follwed by two numbers, or type QUIT: quit

[117]> 

-- 
__Pascal Bourguignon__