Re: hp prime  programming with lists Message #3 Posted by Patrice on 9 Nov 2013, 4:05 p.m., in response to message #1 by Giancarlo
1+2) Here is a couple utilities about list handling in a programming point of view. The name come from the language Clipper (xBase)
with examples at the end
EXPORT AAdd(Lst, Val_) // add element at end of list
BEGIN
RETURN CONCAT(Lst, {Val_});
END;
EXPORT AIns(Lst, Pos_, Val_) // insert element in list
BEGIN
IF Pos_ == 1 THEN RETURN CONCAT({Val_}, Lst); END;
IF Pos_ > SIZE(Lst) THEN RETURN CONCAT(Lst, {Val_}); END;
RETURN CONCAT(CONCAT(Sub(Lst, 1, Pos_1), {Val_}), Sub(Lst, Pos_, (SIZE(Lst))));
END;
EXPORT ADel(Lst, Pos_) // delete element of list
BEGIN
LOCAL Tmp;
IF Pos_ == 1 THEN
IF SIZE(Lst) == 1 THEN Tmp:= {};
ELSE Tmp:= SUB(Lst,2,SIZE(Lst)); END;
ELSE
IF Pos_ == SIZE(Lst) THEN Tmp:= SUB(Lst,1,SIZE(Lst)1);
ELSE Tmp:= CONCAT(Sub(Lst, 1, Pos_1), Sub(Lst, Pos_+1, (SIZE(Lst))));
END;
END;
RETURN Tmp;
END;
EXPORT ADelTail(Lst) // delete last element of list
BEGIN
IF SIZE(Lst) == 1 THEN RETURN {};
ELSE RETURN SUB(Lst,1,SIZE(Lst)1); END;
END;
EXPORT ASize(Lst, NewSize) // resize list
BEGIN
LOCAL Tmp;
IF NewSize == 0 THEN Tmp:= {}; END;
IF SIZE(Lst) > NewSize THEN Tmp:= SUB(Lst,1, NewSize); END;
IF SIZE(Lst) == NewSize THEN Tmp:= Lst; END;
IF SIZE(Lst) < NewSize THEN Tmp:= CONCAT(Lst, MAKELIST(0, A, SIZE(Lst)+1, NewSize)); END;
RETURN Tmp;
END;
EXPORT ATail(Lst) // Last element of list
BEGIN
RETURN Lst(SIZE(Lst));
END;
EXPORT ASort(Lst, Col) // Sort a list of lists
BEGIN
LOCAL Scan, Tmp, Pos, Db, Fn, Ml;
IF SIZE(Lst) > 0 THEN
Tmp:= {Lst(1)};
FOR Scan FROM 2 TO SIZE(Lst) DO
IF Lst(Scan,Col) < Tmp(1,Col) THEN Tmp:= AIns(Tmp, 1, Lst(Scan)); ELSE
IF Lst(Scan,Col) >= Tmp(SIZE(Tmp),Col) THEN Tmp:= AAdd(Tmp, Lst(Scan)); ELSE
Db:= 2; Fn:= SIZE(Tmp)1;
WHILE Db <= Fn DO
Ml:= IP((Db+Fn)/2);
IF Tmp(Ml,Col) <= Lst(Scan,Col) THEN Db:= Ml+1;
ELSE Fn:= Ml1; END;
END;
Tmp:= AIns(Tmp, Db, Lst(Scan));
END;
END;
END;
ELSE
Tmp:= {};
END;
RETURN Tmp;
END;
EXPORT TestList()
BEGIN
LOCAL Cnt, List;
List:= {};
FOR Cnt FROM 1 TO 15 DO
List:= AAdd(List, Cnt);
MSGBOX(ATail(List)+":"+STRING(List));
END;
List:= MAKELIST(A, A, 1, 15, 1);
FOR Cnt FROM 1 TO 15 DO
List:= ADel(List, 1);
MSGBOX(List);
END;
List:= MAKELIST(A, A, 1, 15, 1);
FOR Cnt FROM 1 TO 10 DO
List:= ADel(List, 3);
MSGBOX(List);
END;
List:= MAKELIST(A, A, 1, 15, 1);
FOR Cnt FROM 1 TO 15 DO
List:= ADelTail(List);
MSGBOX(List);
END;
END;
