We can extract an element of the Vector as follows:
The question is, why is the notation ``(Ball)'' needed in this
assignment statement?
Your function should use only basic Scheme operations like if,
cond,
car, cdr, and appropriate recursion.
For each of the following parameter passing mechanisms, execute the program
and give the final values of b[1] and b[2].
using the normal static scoping rules of Scheme?
using dynamic scoping?
The most important distinction between primitive functions and
special forms is that primitive functions are called by first evaluating
all of the arguments to the function then invoking the function on the
evaluated arguments. When a special form is invoked, on the other hand,
none of the arguments are evaluated. The special form can pick and choose
which of the arguments it wants to evaluate as it runs.
Ball b1 = new Ball();
Vector balls = new Vector();
balls.addElement(b1);
Ball b2 = (Ball) balls.elementAt(0);
The Java compiler does static type checking. This means that the left hand
side of an and the right hand side of the must have the same
static type. Vector.elementAt(int) is statically typed to return an
Object; however, it may return any subclass of Object. In this
case, the cast adds a runtime type check to make sure that the Object
returned by elementAt() is in fact a Ball and it tells the
compiler to treat the object being cast (in this case, the right hand side
of the equation) is dynamically an object of type Ball. Thus, the
static type of the right hand side and the left hand side of the are
the same and the code fragment passes the Java type checker.
(define (scale stuff by)
(if (null? stuff)
'()
(cons (* (car stuff) by)
(scale (cdr stuff) by)
)
)
)
No. It is not tail recursive because the final return value is the result
of cons not the direct result of a recursive call to scale.
In C++, data is data. In a typical implementation, integers and pointers
are the same size. There is no way to tell if a particular pointer sized
piece of data is a pointer or some other data (like an integer). Thus,
there is no way for a garbage collector to go through known data and
determine which pieces of the data are really pointers to other data and
which are just numbers. C++ also allows arbitrary pointer operations like
pointer arithmetic which mean that a garbage collector cannot make
assumptions about pointers even if it could tell which values were pointers.
(last '(a b c)) => c
(last '(a)) => a
(define (last l)
(if (null? (cdr l))
(car l)
(last (cdr l))
)
)
begin
integer i;
integer array b[3]; // b has elements b[0], b[1], and b[2]
procedure q(int x)
begin
x = x + 2;
b[i] = 10;
i = 2;
x = x + 2;
end;
b[0] = 1; b[1] = 1; b[2] = 1;
i = 1;
q(b[i]);
end.
(define x 10)
(define (squid y) (+ x y))
(define (clam x) (squid (* 2 x)))
(clam 3)
16
(clam 3)
9
You should define tiny classes and/or functions as needed in your
examples..
public class A
{
int foo()
{
return 0;
}
int foo(int i)
{
return i;
}
}
public class B
{
void bar()
{
System.out.println("called B.bar()");
}
}
public class C extends B
{
void bar()
{
System.out.println("called C.bar()");
}
}