Math operator in a symbol

Q&A's, tips, howto's
Locked
dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Math operator in a symbol

Post by dukester »

hi...

it's been awhile...

Need help with this code:

(print "Enter the 1st number: ")
(set 'num1 (int (read-line)))
(print "Enter the 2nd number: ")
(set 'num2 (int (read-line)))
(print "Enter an operator [+ - * /]: ")
(set 'op (read-line))
;(print op)
(set 'result (op num1 num2))
result

I get:

Enter the 1st number: "Enter the 1st number: "
5
5
Enter the 2nd number: "Enter the 2nd number: "
6
6
Enter an operator [+ - * /]: "Enter an operator [+ - * /]: "
*
"*"

ERR: string index out of bounds in function set
>

Some clues, please. TIA....
duke

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

Hi. You need to convert string into operation, something like:

Code: Select all

(print "Enter the 1st number: ")
(set 'num1 (int (read-line)))
(print "Enter the 2nd number: ")
(set 'num2 (int (read-line)))
(print "Enter an operator [+ - * /]: ")
(set 'op (eval-string (read-line)))
(set 'result (op num1 num2)) 
(print result)
(exit)
or

Code: Select all

(set 'op (eval (sym (read-line))))

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Post by dukester »

[quote="Kazimir Majorinc"]Hi. You need to convert string into operation, something like:

[code](print "Enter the 1st number: ")
(set 'num1 (int (read-line)))
(print "Enter the 2nd number: ")
(set 'num2 (int (read-line)))
(print "Enter an operator [+ - * /]: ")
(set 'op (eval-string (read-line)))
(set 'result (op num1 num2))
(print result)
(exit)[/code]

or

[code](set 'op (eval (sym (read-line))))[/code][/quote]

Thank you my friend! It's hell being a rookie. ;)
duke

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Post by dukester »

Hi...

Kazimir, would you explain your code. Here is the homework I have done. From the newLISP manual:

syntax: (sym string [sym-context nil-flag] )
Translates the first argument in string, ... into a symbol and returns it.

This one I don't understand. I have a string (char) in the
read-line buffer. `sym' will translate it into a symbol? i.e. a
variable. Then `eval' evaluates the symbol, i.e. it....???

******************************************

syntax: (eval-string str [expr] [sym-context])

Before being evaluated, the result of str is compiled into
newLISP's internal format, and the result of the evaluation
is returned.

This one I understand -> it says (in the case of my example)
"evaluate the character in the read-line buffer and compile
it into a newLISP function. So the string "*" e.g., becomes
function *. Is that correct?
duke

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

newLISP has not one but two 'eval' functions. The first, eval, accepts an expression and evaluates it:

Code: Select all

(set 'expr '(+ 1 2))
(eval expr)
;-> 3
The second, eval-string, accepts a string and evaluates it:

Code: Select all

(set 'expr "(+ 1 2)")
(eval-string expr)
;-> 3

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

Like Cormullion said. Maybe bit more on this symbol. "Executive part" of your code is

(op num1 num2)

op is obviously symbol. You have to ensure that this symbol evaluates to function, in this case built in primitive function for multiplying.

* itself is not built in primitive, it is the symbol that evaluates to primitive (similarly like after (set 'f (lambda(x)x)), f is symbol that evaluates to lambda-list.)

You need to define op on the way it evaluates to that same primitive as * does. Not to * symbol. You can do it by this, simplest way

(set 'op *)


which is equivalent to

(set 'op (eval '*)) ;


which is equivalent to

(set 'op (eval (sym "*")));


which is equivalent to

(set 'op (eval-string "*"))



(set 'op '*) will not work, it will set the value of op to be SYMBOL *.

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Post by dukester »

Hey Kaz...

Thanks for the explanation! Just what I needed. I enjoy your "retro"-looking sites. ;) Reminds me of the 1980's Valdocs software I was using on an Epson computer -- written in Forth of all things.

Thanks again!
duke

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

newLISP has not one but two 'eval' functions. The first, eval, accepts an expression and evaluates it ...
The second, eval-string, accepts a string and evaluates it ...
and there is a third one worth to mention: 'read-expr' takes a string and translates it to an expression:

Code: Select all

> (read-expr "(+ 3 4)")
(+ 3 4)
> (eval (read-expr "(+ 3 4)"))
7
> 
'eval-string' is a combination of 'read-expr' and 'eval'.

dukester
Posts: 115
Joined: Tue May 08, 2007 1:06 pm
Location: Alberta, Canada

Post by dukester »

Thanks Lutz! The pieces are fitting together a bit better now. BTW, thanks for this great language.
duke

Locked