I have a doubt in one of the Scheme definitions explained in class.The
(make-quantity value unit) definition.We have the definitions as
;;Defining the units used in the program.
(define units (list 'm 'm3 'RS 's 'k ))
;;checking for valid units.
(define (units? obj)
(if (member obj units)
#t
#f))
;;finally defining the make-quantity function
(define (make-quantity value unit)
(if (and (number? value)(units? unit))
(list value unit)
"Invalid input"))
In the make-quantity function if anything other than a number is given
as argument for 'value' we will get an error
'reference to undefined identifier:',
without being handled by the if construct given.Can we make the
program
more robust by handling this error through the program with suitable
steps
say by giving a suitable error message "Invalid input".
The interpreter won't give the error message for anything other than a
number.
For example :
> (make-quantity 5 "something")
"Invalid input"
> (make-quantity 4 #f)
"Invalid input"
It gets evaluated because "something" is a string and #f is a boolean.
But when we call :
>(make-quantity 5 d )
The interpreter looks for the value of identifier d .But d is never
defined anywere so before even trying to evaluate the function make-
quantity it prints the error saying "reference to undefined
identifier: d"
This happens if just try
>(+ 3 d)
>reference to undefined identifier: d
Here too before even trying to evaluate the operation (primitive
procedure) + it looks for the value d .And prints the error message.
I really don't know how we can catch even undefined identifier and
prints our own defined error message....
making it simpler
How to make an expression to be evaluated even if there is an
undefined identifier...????
Regards,
Girish N Gopal