Math operator in a symbol

Started by dukester, April 14, 2009, 05:12:46 PM

Previous topic - Next topic

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

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


(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


(set 'op (eval (sym (read-line))))
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

dukester

#2
Quote from: "Kazimir Majorinc"Hi. You need to convert string into operation, something like:



(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



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



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

dukester

#3
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

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


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


The second, eval-string, accepts a string and evaluates it:


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

Kazimir Majorinc

#5
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.



[size=200]*[/size] 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 *.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

dukester

#6
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

#7
QuotenewLISP has not one but two 'eval' functions. The first, eval, accepts an expression and evaluates it ...


QuoteThe 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:


> (read-expr "(+ 3 4)")
(+ 3 4)
> (eval (read-expr "(+ 3 4)"))
7
>


'eval-string' is a combination of 'read-expr' and 'eval'.

dukester

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