Page 1 of 1
					
				Math operator in a symbol
				Posted: Wed Apr 15, 2009 12:12 am
				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....
			 
			
					
				
				Posted: Wed Apr 15, 2009 12:53 am
				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))))
 
			 
			
					
				
				Posted: Wed Apr 15, 2009 1:16 am
				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. ;)
			 
			
					
				
				Posted: Wed Apr 15, 2009 7:52 pm
				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?
			 
			
					
				
				Posted: Wed Apr 15, 2009 9:02 pm
				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
 
			 
			
					
				
				Posted: Wed Apr 15, 2009 11:46 pm
				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 *.
			 
			
					
				
				Posted: Thu Apr 16, 2009 12:57 am
				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!
			 
			
					
				
				Posted: Thu Apr 16, 2009 7:27 pm
				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'.
 
			 
			
					
				
				Posted: Thu Apr 16, 2009 9:02 pm
				by dukester
				Thanks Lutz! The pieces are fitting together a bit better now. BTW, thanks for this great language.