From: Dan Bensen
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <evtr7o$i0q$1@wildfire.prairienet.org>
···········@gmail.com wrote:
  > 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.

Use read-line to read input, and parse-integer to convert the string.

> help regarding a loop so that it only exits when the user enters 5

sbcl has a quit function, but it's not part of the language.  You can 
check your implementation's docs for an equivalent.  Or you can just 
return from your main function to exit to the repl.

-- 
Dan
www.prairienet.org/~dsb/

From: ···········@gmail.com
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <1176664030.532988.295350@w1g2000hsg.googlegroups.com>
On Apr 15, 2:39 pm, Dan Bensen <··········@cyberspace.net> wrote:
> ···········@gmail.com wrote:
>
>   > 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.
>
> Use read-line to read input, and parse-integer to convert the string.
>
> > help regarding a loop so that it only exits when the user enters 5
>
> sbcl has a quit function, but it's not part of the language.  You can
> check your implementation's docs for an equivalent.  Or you can just
> return from your main function to exit to the repl.
>
> --
> Danwww.prairienet.org/~dsb/

ok i tried the read input and parse integer but i dunno if I am doing
it correctly, I looked up some stuff and the tutorials on this stuff
arn't easy for me to understand what everything is doing.

(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-line ANS)
(parse-integer "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))

These are the errors i got :
;;; An error of type UNBOUND-VARIABLE was detected in function #<
COMPILED-FUNCTION: #xCF96D0 >:
;;; Error: The variable ANS is unbound
;;; Entering Corman Lisp debug loop.
;;; Use :C followed by an option to exit. Type :HELP for help.
;;; Restart options:
;;; 1   Abort to top level.

So still and unbound variable
From: ···········@gmail.com
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <1176664157.822553.157650@y80g2000hsf.googlegroups.com>
On Apr 15, 3:07 pm, ···········@gmail.com wrote:
> On Apr 15, 2:39 pm, Dan Bensen <··········@cyberspace.net> wrote:
>
>
>
> > ···········@gmail.com wrote:
>
> >   > 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.
>
> > Use read-line to read input, and parse-integer to convert the string.
>
> > > help regarding a loop so that it only exits when the user enters 5
>
> > sbcl has a quit function, but it's not part of the language.  You can
> > check your implementation's docs for an equivalent.  Or you can just
> > return from your main function to exit to the repl.
>
> > --
> > Danwww.prairienet.org/~dsb/
>
> ok i tried the read input and parse integer but i dunno if I am doing
> it correctly, I looked up some stuff and the tutorials on this stuff
> arn't easy for me to understand what everything is doing.
>
> (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-line ANS)
> (parse-integer "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))
>
> These are the errors i got :
> ;;; An error of type UNBOUND-VARIABLE was detected in function #<
> COMPILED-FUNCTION: #xCF96D0 >:
> ;;; Error: The variable ANS is unbound
> ;;; Entering Corman Lisp debug loop.
> ;;; Use :C followed by an option to exit. Type :HELP for help.
> ;;; Restart options:
> ;;; 1   Abort to top level.
>
> So still and unbound variable

I AM DUMB I am beginning to realize this LOL
From: Dan Bensen
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <evu1p9$k2m$1@wildfire.prairienet.org>
···········@gmail.com wrote:
> (read-line ANS)
read-line doesn't take a variable name as an argument,
it produces the input as its return value.
So you write something like this:

(let ((ans (read-line)))
   ...

ans is now a string containing the input.
The man page is here:
http://www.lisp.org/HyperSpec/Body/fun_read-line.html#read-line

> (parse-integer "ANS")

parse-integer takes the value to be parsed,
not the name of the variable as a string:

(let* ((ans (read-line))
        (val (parse-integer ans)))
   ...

val is now the integer you're looking for,
if the input was an integer.
The man page can be found from here:
http://www.lisp.org/HyperSpec/FrontMatter/Symbol-Index.html

> (COND (1 (ADDITION (NUM1 NUM2)

This should be (case val (1 ...
I'll let you find this one yourself:
http://www.lisp.org/HyperSpec/FrontMatter/index.html

-- 
Dan
www.prairienet.org/~dsb/
From: ···········@gmail.com
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <1176687854.968055.182660@n59g2000hsh.googlegroups.com>
On Apr 15, 4:31 pm, Dan Bensen <··········@cyberspace.net> wrote:
> ···········@gmail.com wrote:
> > (read-line ANS)
>
> read-line doesn't take a variable name as an argument,
> it produces the input as its return value.
> So you write something like this:
>
> (let ((ans (read-line)))
>    ...
>
> ans is now a string containing the input.
> The man page is here:http://www.lisp.org/HyperSpec/Body/fun_read-line.html#read-line
>
> > (parse-integer "ANS")
>
> parse-integer takes the value to be parsed,
> not the name of the variable as a string:
>
> (let* ((ans (read-line))
>         (val (parse-integer ans)))
>    ...
>
> val is now the integer you're looking for,
> if the input was an integer.
> The man page can be found from here:http://www.lisp.org/HyperSpec/FrontMatter/Symbol-Index.html
>
> > (COND (1 (ADDITION (NUM1 NUM2)
>
> This should be (case val (1 ...
> I'll let you find this one yourself:http://www.lisp.org/HyperSpec/FrontMatter/index.html
>
> --
> Danwww.prairienet.org/~dsb/

OK i dumbed my code down to help me out and in my thinking assumed
that it would help me to figure out my problem.

(loop
    (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")
    (print 'Enter>)
    (terpri)
    (let* ((ans (read-line)))
        (val (parse-integer ans)))
        (COND (1 (ADDITION (NUM1 NUM2)))))

Ok so the output to the screen works, can someone please, I don't
think i could bang my head harder on a brick wall than I have today
trying to code something this simple in thought.

Im doing this barebones, I want to try and enter the number 1.  From
there I want my LISP code to take me to the addition function so that
I can add two numbers.  Somewhere in this code I have to ask the user
to enter in two numbers they want to add.  I can't get past it
displaying the menu cuz after that it craps out on me.  Does anyone
have any suggestions that would help, and please be very descriptive
cuz im dying here and iv only done tutorials on LISP and I don't
completly understand it all yet but I am trying.

If anyone could help me it would be greatly appreciated.
From: Dan Bensen
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <evuna8$qsv$1@wildfire.prairienet.org>
···········@gmail.com wrote:
> On Apr 15, 4:31 pm, Dan Bensen <··········@cyberspace.net> wrote:
>>> (COND (1 (ADDITION (NUM1 NUM2)
>> This should be (case val (1 ...

>     (let* ((ans (read-line)))
>         (val (parse-integer ans)))
>         (COND (1 (ADDITION (NUM1 NUM2)))))

You still haven't fixed the cond.  If you want to use cond,
you have to say something like

(cond
   ((= val 1) (addition ...))
   ((= val 2) ...

> Im doing this barebones, I want to try and enter the number 1.  From
> there I want my LISP code to take me to the addition function so that
> I can add two numbers.  Somewhere in this code I have to ask the user
> to enter in two numbers they want to add.  I can't get past it
> displaying the menu cuz after that it craps out on me. 

Forget about multiplying, etc., for now.  Just do addition,
then add more code later.  You need to get the two numbers and
print their sum.  Here's an example/starting point:

(write-string "Enter first number: ")
(finish-output)
(let ((x1 (parse-integer (read-line))))
   (write-string "Enter second number: ")
   (finish-output)
   (let ((x2 (parse-integer (read-line))))
     (format t "The sum is ~A~%" (+ x1 x2))))

You can wrap the whole thing in a function, use different printing
functions, etc., especially wrap the code that asks for input in
a function, and eventually start adding more functionality.
Each number is going to need a separate prompt, read-line, and
variable binding (the stuff in the LET form), unless you want to
start parsing more complex input, which at this point you don't.

When you have addition done, you'll need to figure out how you
want to support the input with several operations.  That's a
design issue that you should work out before trying to code it.
The simplest way might be to get the operation number and the
other two numbers from the user before you even enter the cond.
Just write three chunks of code to get the three numbers,
and then put the cond after them to handle the logic.

-- 
Dan
www.prairienet.org/~dsb/
From: Rob St. Amant
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <evum9c$39d$1@blackhelicopter.databasix.com>
···········@gmail.com writes:

> On Apr 15, 4:31 pm, Dan Bensen <··········@cyberspace.net> wrote:
>> ···········@gmail.com wrote:
>> > (read-line ANS)
>>
>> read-line doesn't take a variable name as an argument,
>> it produces the input as its return value.
>> So you write something like this:
>>
>> (let ((ans (read-line)))
>>    ...
>>
>> ans is now a string containing the input.
>> The man page is here:http://www.lisp.org/HyperSpec/Body/fun_read-line.html#read-line
>>
>> > (parse-integer "ANS")
>>
>> parse-integer takes the value to be parsed,
>> not the name of the variable as a string:
>>
>> (let* ((ans (read-line))
>>         (val (parse-integer ans)))
>>    ...
>>
>> val is now the integer you're looking for,
>> if the input was an integer.
>> The man page can be found from here:http://www.lisp.org/HyperSpec/FrontMatter/Symbol-Index.html
>>
>> > (COND (1 (ADDITION (NUM1 NUM2)
>>
>> This should be (case val (1 ...
>> I'll let you find this one yourself:http://www.lisp.org/HyperSpec/FrontMatter/index.html
>>
>> --
>> Danwww.prairienet.org/~dsb/
>
> OK i dumbed my code down to help me out and in my thinking assumed
> that it would help me to figure out my problem.
>
> (loop
>     (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")
>     (print 'Enter>)
>     (terpri)
>     (let* ((ans (read-line)))
>         (val (parse-integer ans)))
>         (COND (1 (ADDITION (NUM1 NUM2)))))
>
> Ok so the output to the screen works, can someone please, I don't
> think i could bang my head harder on a brick wall than I have today
> trying to code something this simple in thought.
>
> Im doing this barebones, I want to try and enter the number 1.  From
> there I want my LISP code to take me to the addition function so that
> I can add two numbers.  Somewhere in this code I have to ask the user
> to enter in two numbers they want to add.  I can't get past it
> displaying the menu cuz after that it craps out on me.  Does anyone
> have any suggestions that would help, and please be very descriptive
> cuz im dying here and iv only done tutorials on LISP and I don't
> completly understand it all yet but I am trying.

I think you'd be best off going back to basics.  Here's how to
interpret this fragment as written:

>     (let* ((ans (read-line)))
>         (val (parse-integer ans)))
>         (COND (1 (ADDITION (NUM1 NUM2)))))

Let the variable ans be a line of text.  Now call the function val on
the result of extracting an integer from ans.  (It seems clear that
val is a variable, which means that the parentheses are wrong.)  Next
we have a test: See if 1 is non-nil.  Okay, that's true, so call the
function addition with one argument, the result of calling the
function num1 with an argument num2.  Again the parentheses are wrong,
but there's also the problem that num1 and num2 wouldn't have values
if they were passed to the function addition.  Hope this helps.
From: Frank Buss
Subject: Re: Making a Calculator program in LISP
Date: 
Message-ID: <mhr0eus9xnqj.1e3lz8zibntto.dlg@40tude.net>
Try reading a book, first, e.g. this one: http://www.gigamonkeys.com/book/

···········@gmail.com wrote:

> (read-line ANS)
> (parse-integer "ANS")

This doesn't make sense, because READ-LINE returns an object, which you
don't use. Here is a simpler example:

(+ 1 2)

This returns 3, but if you don't use it, it's lost (ignoring tricks like
"*" for now). One solution is to nest it. E.g. you can write this:

(format t "number: ~a ~%" 2)

This prints "number: 2", followed by a newline (~%). Now you can nest it:

(format t "something: ~a ~%" (read))

First the inner expression is evaluated, which prompts you for entering an
object, which eventually is printed.

If you manage to implement the loop, you should read the description of
COND again :-)

-- 
Frank Buss, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de