[fpc-pascal] a few trivial questions
denis.spir at gmail.com
Wed May 12 19:22:15 CEST 2010
On Wed, 12 May 2010 16:45:47 +0200 (CEST)
Michael Van Canneyt <michael at freepascal.org> wrote:
> On Wed, 12 May 2010, spir ☣ wrote:
> > Hello,
> > * TFPList
> > Is there another way to traverse a list than
> > for i :=0 to (list.count - 1) do ...
> You can use an enumerator with the latest SVN:
> foreach P in List do
> > What about list.high?
? In my code count=high+1. I mean list[list.count] does not exist.
> > Also, is it possible to set a list's base index to 1 (so that last_index=count)?
> No. That would break all other existing code.
I don't understand. If I set myList.baseIndex := 1, how can it affect other people's code? It's a new feature that does not touch existing codebase.
> > There is a super handy forEachCall method (calling a proc for each item), but I cannot find a matching map method (calling a func and storing results).
> What do you mean with 'Map' ?
It's the conventional name in most PLs of a higher-order function that creates a new sequence from an original one and a mapping func:
cubes := map(numbers, cube)
or in OO style:
cubes := numbers.map(cube)
But you certainly know that better than me...
> > * string <-> number
> > I could not find _functions_ to operate such conversions, only _procedures_ str and val. So that I always end up with superfluous statements and temps variables:
> > str(n,text);
> > writeln(text);
> In SysUtils: StrToInt() and IntToStr()
Thank you. Hadn't found them.
> > * array value notation
> > I can set a static array's value on declaration line, but could not find a way to do it later (even less to change it). And I cannot find a way to set the value of a dynamic array --when it's not fed from a loop. End up with series of arr[i]:=v statements (code from the http://en.wikipedia.org/wiki/Middle_ages ;-). Same for lists, indeed.
> > var
> > arr : array [1..2] of Integer;
> > ...
> > i:=1 ; j:=2;
> > arr := (i,j);
> > ==> Syntax error, ")" expected, but "," found
> This is only possible with dynamic arrays (no declared length), and there you should use setlength.
I haven't managed to do that with dynamic arrays, even after setting length.
a : array of Integer;
a := (1,2,3);
==> same error message as above
> > Finally: is there a wiki version of the official docs, to collectively enhance them (with notes, examples, explanations...).
> > (I'm dreaming, but after all fpc is free software, and it's the #1 reason why I chose it.) (*)
> No, there is no wiki version, and there never will be.
vit esse estrany ☣
More information about the fpc-pascal