- Index
- » Developer
- » OpenModelica development
- » Error concerning Recursive methods
Error concerning Recursive methods
Error concerning Recursive methods
Hi every one,
First, thank you for the attention and help you'll give to this post.
My API openModelica function fails when I introduce this following
computation :
I try to calculate from a list of Component Ref or String
defined such as :
list1 = { a.b, b.d, c.f, c.d, c.h, g.h }
a integer list equal to
List2 = { 1 , 1, 3, -2, -1, 1 }
because there is three terms with the same prefix c.
I try to write recursive program but I a beginner and I
don't understand where I make the mistake(s) ...
My master function is :
list<list<String>> connectorCref2;
list<Integer> subNodeCref;
subNodeCref = countStrCref(connectorCref2,{});
With these slave functions :
//-------------------------------------
public function countStrCref "Specific count about StrCref."
input list<list<String>> inList;
input list<Integer> indexList;
output list<Integer> outList;
algorithm
outList := matchcontinue(inList,indexList)
local
list<String> elem;
list<list<String>> tail;
String strFirst;
Integer nbCref,nbCrefPrevious;
list<list<String>> inList2;
case({},_)
then {};
case(elem :: tail,_)
equation
strFirst = listGet(elem,1);
inList2 = List.deleteMember(tail,elem);
nbCrefPrevious = Util.if_(List.isEmpty(indexList),1,List.last(indexList));
print("4\n");
nbCref = count2(nbCrefPrevious,strFirst,inList2);
print("5\n");
then
countStrCref(tail,listAppend(indexList,{nbCref}));
end matchcontinue;
end countStrCref;
//-------------------------------------
public function count2
input Integer nbCrefPrevious;
input String strFirst;
input list<list<String>> inList2;
output Integer nbCref;
algorithm
nbCref := matchcontinue(nbCrefPrevious,strFirst,inList2)
local
Integer n,nout;
list<Integer> nbCrefIndex;
case(n,_,_)
equation
print("4.1\n");
nbCrefIndex = count3(strFirst,inList2,{});
print("4.2\n");
nout = Util.if_(n > 1,-n+1,
Util.if_(n < 0 and n != -1,n+1,
Util.if_(n == 1 or n == -1,List.reduce(nbCrefIndex,intAdd), 1)));
print("4.3\n");
then
nout;
end matchcontinue;
end count2;
//-------------------------------------
public function count3
input String strCrefToCompare;
input list<list<String>> inList;
input list<Integer> nbCrefLst;
output list<Integer> nbCref;
algorithm
nbCref := matchcontinue(strCrefToCompare,inList,nbCrefLst)
local
Integer nbFirstCref;
String strCrefIndx;
list<String> elem;
list<list<String>> tail;
case(_,elem :: tail,_)
equation
print("4.1.1\n");
strCrefIndx = listGet(elem,1);
print("4.1.2\n");
nbFirstCref = Util.if_(stringEq(strCrefToCompare,strCrefIndx),1,0);
print("4.1.3\n");
then count3(strCrefToCompare,tail,listAppend(nbCrefLst,{nbFirstCref}));
end matchcontinue;
end count3;
Thanks
Regards
Alain
Re: Error concerning Recursive methods
I just replace this line
nbCrefPrevious = Util.if_(List.isEmpty(indexList),1,List.last(indexList));
by this : nbCrefPrevious = count4(indexList);
with
public function count4
input list<Integer> indexList;
output Integer nbCrefPrevious;
algorithm
nbCrefPrevious := matchcontinue(indexList)
case({}) then 1;
case(_) then List.last(indexList);
end matchcontinue;
end count4;
But I does not run correctly , something happens in count3 method !!
Alain
Re: Error concerning Recursive methods
Nowly, I don't consider <list<list<String> so that my problem is written like that :
But I have alaways a mistake for the line which computes the elemPosition1,
Regards
Alain
//-------------------------------------
public function countStrCref "Specific count about StrCref."
input list<String> inList;
input list<Integer> indexList;
output list<Integer> outList;
algorithm
outList := matchcontinue(inList,indexList)
local
String elem;
list<String> tail;
Integer nbCref,nbCrefPrevious,elemPosition1;
list<Integer> nbCrefIndex;
list<String> inList2;
case({},_)
then {};
case(elem :: tail,_)
equation
nbCrefPrevious = getCountBefore(indexList);
print("3\n");
elemPosition1 = List.position(elem,tail);
print("4\n");
elemPosition1 = elemPosition1 + 1;
//inList2 = Util.if_(elemPosition1>listLength(tail),{},List.lastN(tail,elemPosition1));
inList2 = Util.if_(stringEqual(elem,List.last(tail)),{},List.lastN(tail,elemPosition1));
print("4.1\n");
nbCrefIndex = count3(inList2,elem,{});
nbCref = count2(nbCrefPrevious,nbCrefIndex);
print("5\n");
then
countStrCref(tail,listAppend(indexList,{nbCref}));
end matchcontinue;
end countStrCref;
public function count2
input Integer nbCrefPrevious;
input list<Integer> nbCrefIndex;
output Integer nbCref;
algorithm
nbCref := matchcontinue(nbCrefPrevious,nbCrefIndex)
local
Integer n,nout;
case(n,{})
then 1;
case(n,_)
equation
print("4.2\n");
nout = Util.if_(n > 1,-n+1,
Util.if_(n < 0 and n != -1,n+1,
Util.if_(n == 1 or n == -1,List.reduce(nbCrefIndex,intAdd), 1)));
print("4.3\n");
then
nout;
end matchcontinue;
end count2;
public function count3
input list<String> inList;
input String strCrefToCompare;
input list<Integer> nbCrefLst;
output list<Integer> nbCref;
algorithm
nbCref := matchcontinue(inList,strCrefToCompare,nbCrefLst)
local
Integer nbFirstCref;
list<String> strCrefIndxLst;
String strCrefIndx,elem;
list<String> tail;
case({},_,_)
then {};
case(elem :: tail,_,_)
equation
print("4.1.1\n");
//strCrefIndx = listGet(elem,1);
strCrefIndxLst = Util.stringSplitAtChar(elem,".");
strCrefIndx = listGet(strCrefIndxLst,1);
print("4.1.2\n");
nbFirstCref = Util.if_(stringEqual(strCrefToCompare,strCrefIndx),1,0);
print("4.1.3\n");
then count3(tail,strCrefToCompare,listAppend(nbCrefLst,{nbFirstCref}));
end matchcontinue;
end count3;
/-------------------------------------
public function getCountBefore
input list<Integer> indexList;
output Integer nbCrefPrevious;
algorithm
nbCrefPrevious := matchcontinue(indexList)
case({}) then 1;
case(_) then List.last(indexList);
end matchcontinue;
end getCountBefore;
- Index
- » Developer
- » OpenModelica development
- » Error concerning Recursive methods