HP Forums
Problem with append function - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Problem with append function (/thread-4252.html)



Problem with append function - BruceH - 06-28-2015 10:15 PM

Trying to do some simple things with lists in a program but not getting very far.

Code:
EXPORT test()
BEGIN
  LOCAL xx := {};
  LOCAL yy := 3;
  append(xx, yy);
  RETURN(xx); 
END;

I'd expect that to return { 3 } but actually you get {}. Have I missed something obvious?
(Emulator version 2015 6 17 Rev 8151)

(Edit) And in Home I get some equally strange results:
Code:
append({},3)      {3}
L1                 {}
append(L1,3)      {3}
L1                 {}



RE: Problem with append function - eried - 06-28-2015 10:43 PM

You aren't using append return, try with:

Code:
xx:=append(xx, yy);



RE: Problem with append function - DrD - 06-29-2015 10:24 AM

The append() command doesn't save the results in the first argument. It merely "appends" the two arguments together, and the result is available for whatever need might be next, if anything:

Code:

EXPORT test()
BEGIN
  LOCAL xx := {};
  LOCAL yy := 3;
  RETURN append(xx, yy);   //  Returns the result {3}
END;



RE: Problem with append function - BruceH - 06-29-2015 06:45 PM

Thanks Eried and DrD - it makes perfect sense now it's pointed out.

@TW: Can the user guide be amended to include the word "returns" somewhere in the definition of 'append' (c.f. 'apply' which immediately follows it)?


RE: Problem with append function - Tim Wessman - 06-30-2015 05:22 PM

You can also just do xx(0) := <something> to append.


RE: Problem with append function - eried - 07-01-2015 02:42 AM

(06-30-2015 05:22 PM)Tim Wessman Wrote:  You can also just do xx(0) := <something> to append.

Cool, it seems you can insert with negative numbers too:
Code:
xx(-n):=m // inserts m in position n

Strange thing that list seems to be the real translation of { } syntax:
Code:
list[1 2 3] == { 1,2,3 }