due: Friday, March 2, in class
length(new int[2](0)) + 2
a:int = 2 + (return 2)
if ("abc" < x) 3 else false
L & (("foo" + L) ==
"fool")
!f(s)[length s]
foreach
(id in
E) S foreach
statement
executes the statement S once for each element of the array, with the
variable id bound to the array element at index i-1 on
iteration i. For example, the program:uses io.print, conv.itos
main(args:array[string]) : int = (
i: int = 0;
a: array[int] = new array[int][2]((i = i + 1));
foreach (X in a)
print(itos(X));
0
)
&
). The expression &
E
returns a pointer to its operand E.*
). The expression *
E,
where E evaluates to a pointer, returns the value to which this
pointer points. An expression of this form can also appear as the
left-hand side of an assignment statement, in which case the assignment
overwrites the location pointed to. (Confusingly, the star token is used
in two different ways in C: as a unary operator on expressions (*
E),
and in the construction of pointer types (T*).)Suppose we were to extend Iota with pointer types in the manner of C. (We will ignore C's support for pointer arithmetic.) Here are some brief examples of how we might use these new features:
swap(x:int*, y:int*) = (temp:int = *x; *x =
*y; *y = temp)
a:int = 0; b:int = 1; swap(&a, &b); b==0
x:string = "foo"; z:string* = &x; *z = "bar";
x=="bar"