Math operator in a symbol
Math operator in a symbol
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....
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
-
- Posts: 388
- Joined: Thu May 08, 2008 1:24 am
- Location: Croatia
- Contact:
Hi. You need to convert string into operation, something like:
or
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)
Code: Select all
(set 'op (eval (sym (read-line))))
[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. ;)
[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
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?
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
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
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:
Code: Select all
(set 'expr '(+ 1 2))
(eval expr)
;-> 3
Code: Select all
(set 'expr "(+ 1 2)")
(eval-string expr)
;-> 3
-
- Posts: 388
- Joined: Thu May 08, 2008 1:24 am
- Location: Croatia
- Contact:
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 *.
(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 *.
newLISP has not one but two 'eval' functions. The first, eval, accepts an expression and evaluates it ...
and there is a third one worth to mention: 'read-expr' takes a string and translates it to an expression:The second, eval-string, accepts a string and evaluates it ...
Code: Select all
> (read-expr "(+ 3 4)")
(+ 3 4)
> (eval (read-expr "(+ 3 4)"))
7
>