using linked list in another linkedlist in fortran - linked-list

I have a List(A) which consists of three node A1, A2, A3. Every node in list A has own node B1, B2, B3. In other words node A1 has a list of nodes(B1,B2,B3), node A2 has own list(B1,B2,B3).....
How can I implement it in Fortran.
I write two modules for it.
MODULE B
TYPE NODE_B
INTEGER :: i
DOUBLE PRECISION, DIMENSION(:) :: INPUT
TYPE(NODE_B) , POINTER :: NEXT_B
END TYPE NODE_B
......
......
END MODULE B
The second Module
MODULE A
TYPE NODE_A
INTEGER ::j
TYPE(NODE_B) :: INPUT_A
TYPE(NODE_A), POINTER :: NEXT_A
END NODE_A
.....
.....
END MODULE A
I am not sure about TYPE NODE_A.

Related

What does "local variable" mean in the Forth programming language?

In C, local variables exist inside of a function and contain the values like this:
void main(){
int a = 5;
int b = 9;
}
In the Gforth manual, they describe the local variables like this:
: swap { a b -- b a }
b a ;
1 2 swap .s 2drop
but it seems like a function which is taking two arguments, a and b.
Another tutorial on the Forth language shows a variable like this:
variable a
3 a ! ( ! to store the value )
So, which one is correct?
In Forth, local variables are described by the following syntax (see also 13.6.2.2550 {:):
{: args [ | vals ] [ –– outs ] :}
where each of args, vals and outs represents space-delimited names (the parts in square brackets are optional). These names are interpreted as follows:
args names are for locals that are initialized from the data stack, with the top of the stack being assigned to the rightmost name in args;
vals names are for locals that are uninitialized;
outs names are ignored (they are for documentation purposes only, if any).
Gforth uses { ... } notation for locals as an alternative to the standard one.
So, swap can be defined as:
: swap {: a b :} b a ;
It takes two values from the stack into a and b local variables, and then puts them back on the stack in the reversed order.
An example of use an uninitialized local variable:
: exch ( x2 addr -- x1 ) {: a | x1 :}
a # to x1 a ! x1
;
The optional -- ... part is allowed to mimic a stack diagram, i.e., to unite the declaration of locals and the stack diagram for a word. For example:
: umin {: u2 u1 -- u2|u1 :} u2 u1 u< if u2 else u1 then ;
Without special optimizations, performance of local variables is slightly worse than of a little stack juggling.

How do you write an n-ary tree in postfix notation?

I am trying to understand this paper, Tree template matching in ranked ordered trees by pushdown automata. The first step is having the tree in postfix notation.
How do I take a tree such as this:
foo
bar
abc
def
bar
abc
a
b
a
b
c
d
e
def
abc
baz
bar
abc
a
b
c
abc
def
And write that in postfix notation?
It doesn't make a lot of sense. However, you can either use parentheses:
...(abc a b c)bar abc def)baz)foo
Or specify the number of operands with each operator:
... abc a b c bar4 abc def baz3 foo3
or even:
... abc0 a0 b0 c0 bar4 abc0 def0 baz3 foo3
In the terms of that paper, the tree you are asking about is impossible because you have nodes with the same "symbol" (name) with different numbers of children. The paper, however, is assuming that every symbol in the alphabet has a specified "arity" (the number of children for a node labelled with that symbol). Leaf symbols have arity 0, by the way.
This is (very briefly) mentioned in the Basic Definitions section at the beginning:
A ranked alphabet is a couple 𝒜 = (Σ, φ), where Σ is an alphabet and φ is a mapping . The arity (rank) of a symbol x ∈ Σ is φ(x).
In other words, there is a mathematical function which tells you how many children a labelled node will have, which you can use in the postfix notation to know how many subtrees precede that symbol. (Note also that 𝒜, which includes the arity function, is part of their definition of a PDA.)

Ord: No type class instance was found for Data.Eq.Eq (Extended a0). PureScript by Example book, Chapter 6

I am quite new to Haskell/Purescript and currently learning by studying the PureScript by Example book.
In chapter 6 about type classes, exercise 4 has following task:
(Medium) Given any type a with an instance of Ord, we can add a new "infinite" value which is greater than any other value:
data Extended a = Finite a | Infinite
Write an Ord instance for Extended a which reuses the Ord instance for a.
Here is my attempt:
instance ordExtended :: Ord a => Ord (Extended a) where
compare Infinite Infinite = EQ
compare Infinite _ = GT
compare _ Infinite = LT
compare (Finite f1) (Finite f2) = compare f1 f2
Unfortunately, the code triggers an error:
No type class instance was found for
Data.Eq.Eq (Extended a0)
while checking that expression #dict Eq
has type { eq :: Extended a0 -> Extended a0 -> Boolean
}
in value declaration ordExtended
where a0 is a rigid type variable
bound at (line 0, column 0 - line 0, column 0)
PureScript(NoInstanceFound)
I cannot quite understand the error message:
What does expression #dict Eq mean? There is no dict in my code.
What is a rigid type variable?
The error seems to use different identifiers like a0 (why? I assume, that is a)
In my book, Eq type class instance should be covered by implementing Ord, as Ord extends Eq.
The key part of the error is at the start:
No type class instance was found for
Data.Eq.Eq (Extended a0)
Here is the definition of Ord:
class Eq a <= Ord a where
compare :: a -> a -> Ordering
This is actually the superclass syntax, saying that you need an Eq instance to have an Ord instance. So, you can fix the error by making an Eq instance:
instance eqExtended :: Eq a => Eq (Extended a) where
eq Infinite Infinite = true
eq (Finite f1) (Finite f2) = eq f1 f2
eq _ _ = false
instance ordExtended :: Ord a => Ord (Extended a) where
compare Infinite Infinite = EQ
compare Infinite _ = GT
compare _ Infinite = LT
compare (Finite f1) (Finite f2) = compare f1 f2
As to why a0 is used, it seems the purescript compiler just likes adding numbers after type variables, possibly to reduce vagueness or to allow for scoped type variables. You can read about rigid type variables here (they're basically variables that can't be changed to fit constraints).

Polish to infix notation

Lets say we have an expression in prefix notation or(1) and A B or(2) or(3) C D E (where A, B, C, D, E are boolean values and or numbered for convenience) that we want to convert to an infix notation. In principle I have two ways to evaluate it:
(1) start at or(3) C D, then or(2), then and, then or(1)
(2) start at and A B then check or(3), or(2). Lastly check or(1)
(1) Evaluate starting from right most operator
(2) Evaluate starting from left most operator having all operands as it's direct neighbors.
Both evaluations yield (A and B) or C or D or E.
Which evaluation sequence is correct?
Will these two evaluations ever give different result for the same prefix record?
http://www.cs.man.ac.uk/~pjj/cs212/fix.html recommends the first method.
You will get the same result regarding of the order, so it is up to you.

Can extended Backus Naur Form (EBNF) describe an unordered set of values?

I'd like to define an unordered set of values using an Extended Backus-Naur Form (EBNF) context free grammar. It's easy to define an unordered list of values in EBNF, for example:
value = 'A' | 'B' | 'C';
list = value, {',', value};
However, I'm having doubts it can be done for an unordered set.
Below are examples of valid unordered sets of values:
A, B, C, D
A, B, D, C
A, D, C, B
...
D, C, B, A
Whilst invalid lists would be:
A, A, C, D
B, C, C, B
A, A, A, A
...
or lists of arbitrary length.
A, A, B, C, D, A
A, B, C, D, A, B, C, D
...
You can define unordered sets in EBNF, but only by listing all possible enumerations. That's impractical for sets larger than about two elements.
The standard -- to the extent that EBNF is a standardized notation -- allows you to use English (or any other language you feel comfortable with) to describe a sequence which is not otherwise describable. For example, the EBNF for EBNF includes this production:
syntactic exception
= ? a syntactic-factor that could be replaced
by a syntactic-factor containing no
meta-identifiers
? ;
Similarly, you could write something like:
value = 'A' | 'B' | 'C';
list = value, {',', value};
set = ? a "list" where all "value"s are unique ? ;
or perhaps
set = ? a "list" with exactly three values
where all "value"s are unique
? ;
That's not much use for building a parser generator, but it might be helpful for a human reader who understands the same language as you do.
For real parser generators, a common strategy is to allow any list, which might include repeated elements, and then reject invalid lists during semantic analysis. (Or even earlier. It's not a difficult analysis.)

Resources