Declaring 2-Dimensional arrays in Java - javac

Following are the legal array declarations:
int i[][];
int []j[];
int [][]k;
int[][] l;
But if we declare the arrays in a single line we we are getting a SYNTAX error
int [] []i, []j[], k[], l[][];
ERROR: Syntax Error.
Why is this behavior being displayed?

problem is here
int [] []i, []j[], k[], l[][];
^^
In Java after , in declaration section you are allowed to declare new variable with new additional dimensions after it, not before it, so
int a, b[], c[][];
is possible and it is the same as
int a;
ing[] b;
int[][] c;
but
int a, []b;
is incorrect.
From jls-8.3
More than one field may be declared in a single field declaration by using more than one declarator; the FieldModifiers and Type apply to all the declarators in the declaration.
The declared type of a field is denoted by the Type that appears in the field declaration, followed by any bracket pairs that follow the Identifier in the declarator.
Additional informations are available in 10.2. Array Variables

I think the array symbols '[][]' are linked to the 'int', so
int[][] i, j, k, l
would work
What you did is like
Object a, Object b, Object c;
Which is incorrect
instead,
Object a; Object b; Object c;
or
Object a, b, c
is correct

Related

Using mutable varargs doesnt work as expected in Nim

trying to make a simple procedure to modify a variable number of arguments in nim (for example to initialize a number of a variables from input).
proc Read(outputVars: var varargs[int]) =
for v in outputVars.mitems:
v = 3
var
a, b, c : int
Read(a, b, c)
However the compiler doesn't like this and outputs:
Error: type mismatch: got <int, int, int>
but expected one of:
proc Read(outputVars: var varargs[int])
first type mismatch at position: 1
required type for outputVars: var varargs[int]
but expression 'a' is of type: int
expression: Read(a, b, c)
How can I make a procedure that accepts a variable number of mutable arguments?
The reason this doesn't work is because varargs is an array under the hood. Even if it could be mutable, the values would still have to be copied into the array, so updating them wouldn't update the original variables.
IMO the correct "safe" way to express your example would be something like varargs[var int], i.e. an array of mutable views to integers. But this also doesn't work because Nim's views feature is still experimental.
A less safe solution which does work, is using a coercing varargs of pointers to integers. For example:
proc varIntAddr(n: var int): ptr int =
addr n
proc read(outputVars: varargs[ptr int, varIntAddr]) =
for v in outputVars:
v[] = 3
var a, b, c: int
read(a, b, c)
echo (a, b, c)
Here if you pass in mutable integers, varIntAddr will be implicitly applied to them to take their addresses which get added to the array.
You can also generalise the proc like so:
proc varAddr[T](n: var T): ptr T =
addr n

How to do 'function pointers' in Rascal?

Does Rascal support function pointers or something like this to do this like Java Interfaces?
Essentially I want to extract specific (changing) logic from a common logic block as separate functions. The to be used function is passed to the common block, which then call this function. In C we can do this with function pointers or with Interfaces in Java.
First I want to know how this general concept is called in the language design world.
I checked the Rascal Function Helppage, but this provide no clarification on this aspect.
So e.g. I have:
int getValue(str input) {
.... }
int getValue2(str input){
... }
Now I want to say:
WhatDatatype? func = getValue2; // how to do this?
Now I can pass this to an another function and then:
int val = invoke_function(func,"Hello"); // how to invoke?, and pass parameters and get ret value
Tx,
Jos
This page in the tutor has an example of using higher-order functions, which are the Rascal feature closest to function pointers:
http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Concepts/Functions/Functions.html
You can define anonymous (unnamed) functions, called closures in Java; assign them to variables; pass them as arguments to functions (higher-order functions); etc. Here is an example:
rascal>myfun = int(int x) { return x + 1; };
int (int): int (int);
rascal>myfun;
int (int): int (int);
rascal>myfun(3);
int: 4
rascal>int applyIntFun(int(int) f, int x) { return f(x); }
int (int (int), int): int applyIntFun(int (int), int);
rascal>applyIntFun(myfun,10);
int: 11
The first command defines an increment function, int(int x) { return x + 1; }, and assigns this to variable myfun. The rest of the code would work the same if instead this was
int myfun(int x) { return x + 1; }
The second command just shows the type, which is a function that takes and returns int. The third command calls the function with value 3, returning 4. The fourth command then shows a function which takes a function as a parameter. This function parameter, f, will then be called with argument x. The final command just shows an example of using it.

Making Rascal structures looking better

To keep structures clear is it possible to name them. So essentially I asking for a 'struct' in Rascal. So eg:
list[tupple[map[str,int],int]]
to:
treeLabel :: str
occurences :: int
treeData :: map[treeLabel,int]
treeNode :: tupple[treeData,int]
tree :: list[treeNode]
tree x=[];
Tx
Jos
How about using Abstract Data Types?
See Rascal Tutor. The above could then look like this:
data MyStruct = ms(str treeLabel,
int occurrence,
map[treeLabel, int] treeData,
tuple[TreeData td, int n] treeNode,
list[TreeNode] tree);
given some variable m with a myStruct value you can access elements with the usual dot notation:
m.treeLabel;
m.treeLabel = "xyz";
etc.

F# Mutable Variables inside closures - IDictionary.Item

I am trying to iterate through an IDictionary (reasons explained later...) in F#, and round each value to a specified precision. Essentially this is what I'm trying to do:
List.iter (fun(x) -> a.Item(x) <- Math.Round(a.Item(x), input.precision)) (ICollectionToDoubleList a.Keys)
(where ICollectionToDoubleList takes the ICollection a.Keys and casts it to a double list).
However since you can't alter mutable variables inside closures, this doesn't compile.
My first attempt at a solution was this:
List.iter (fun(x) -> let p = Math.Round(a.Item(x), input.precision)
a.Item(x) := p
) (ICollectionToDoubleList a.Keys)
However I'm getting the error:
This expression was expected to have type
'a ref
but here has type
double
on a.Item(x)
I could convert the IDictionary into two lists (or a list of tuples), perform the rounding, and re-cast into an IDictionary, but this seems a bit messy and convoluted.
Any guidance greatly appreciated.
EDIT:
I forgot to mention a was defined as:
let mutable (a : IDictionary<double,double>) = ...
I think you want
a.Item(x) <- p
In F# you use <- to assign to mutable values, whilst := assign to ref values.
You could even use
a.[x] <- p
for a slightly simpler version.
Explaination of what mutable means (it behaves like the opposite of const in C)
let mutable m = [|1|]
let t = [|1|]
m.[0] <- 0
t.[0] <- 0 //neither of these change m or t - only elements so they are fine
m <- [|1;2;3;|] //fine as m is mutable
t <- [|1;2;3;|] //not allowed as t is not mutable
If you are used to const in C, the above are roughly equivalent to
int* m = {1};
const int* t = {1}
note, neither is equivalent to
const int* q const = {1}
which is I think what you thought not mutable meant.
Ok so I've discovered the answer...
I have defined a as:
let mutable (a : IDictionary<double,double>) = ...
If I change this to
let (a : IDictionary<double,double>) = ...
then this compiles. It seems a little counter-intuative to me that a non-mutable value can be mutated, but a mutatable variable cannot!!

Why does this f# function expects an integer[] instead of byte array

Could someone please show me why the function below expects integer[] instead of byte[]
type Node =
| InternalNode of int*Node*Node
| LeafNode of int * byte
let weight node =
match node with
|InternalNode(w,_,_) -> w
|LeafNode(w,_)-> w
let createNodes inputValues =
let getCounts (leafNodes:(int*byte)[])=
inputValues |>Array.iter
(fun b-> let (w,v) =leafNodes.[(int)b]
leafNodes.[(int)b]<-(w+1,v))
leafNodes
[|for b in 0uy..255uy -> (0 ,b)|] |>getCounts
|>List.ofArray
|>List.map LeafNode
The only place that tells the F# compiler something about the type of the parameter is inside the lambda function given to Array.iter (from the use of this higher-order function, the compiler infers that you're working with arrays). Inside the lambda function you have:
leafNodes.[(int)b]
As a side-note, int in this code is just a normal F# function (not a special type cast construct), so the usual way to write it would be just:
leafNodes.[int b]
Now, the compiler knows that b (that is, values of the array given as the argument) can be converted to integer, however the int function works with other types (you can write for example int 3.13f. In ambiguous cases like this, the compiler uses int as the default type, so that's the reason why you're seeing a type int[].
You can add type annotations to the declaration like this (and it will work without any other changes, because byte can be converted to integer using the int function):
let createNodes (inputValues:byte[]) =
// ...

Resources