Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-04

newbie questions

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

spir ☣

Hello,

Total Pascal newbie here. Looked for answers in various references and tutorials, but cannot find.

Fore-question: Is there a (free)pascal teaching/learning mailing list? (Like python's "tutor" list.) If not, is this one a proper place?

* How does one declare the type of set items?
numbers : Set of Integer // error

* How does one define the _value_ of a Set or Array?
   numbers := (1,2,3)	// error
   numbers := [1,2,3]	// error

Thank you for your patience!

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Howard Page-Clark
to spir ☣
spir ☣ wrote 9 lines
Hello, Total Pascal newbie here. Looked for answers in various references and tutorials, but cannot find. Fore-question: Is there a (free)pascal teaching/learning mailing list? (Like python's "tutor" list.) If not, is this one a proper place? * How does one declare the type of set items? numbers : Set of Integer // error
type
	Tbyteset = set of byte;
* How does one define the _value_ of a Set or Array? numbers := (1,2,3) // error numbers := [1,2,3] // error
var
	byteset : Tbyteset;
begin
	byteset := []; // empty set
	byteset := [0, 3, 101]; // puts literal values into the set
	Include(byteset, 27]; // or byteset := byteset + [27];
	Exclude(byteset, 3); // or byteset := byteset - [3];
end;

Note that set types are limited to 256 elements of ordinal types (integer, char or enumeration).

Howard

Reply by email

spir ☣
to Howard Page-Clark
Show quoted text 31 lines
On 19/4/10 3:50, spir ☣ wrote: > Hello, > > Total Pascal newbie here. Looked for answers in various references and tutorials, but cannot find. > > Fore-question: Is there a (free)pascal teaching/learning mailing list? (Like python's "tutor" list.) If not, is this one a proper place? > > * How does one declare the type of set items? > numbers : Set of Integer // error > type Tbyteset = set of byte; > * How does one define the _value_ of a Set or Array? > numbers := (1,2,3) // error > numbers := [1,2,3] // error var byteset : Tbyteset; begin byteset := []; // empty set byteset := [0, 3, 101]; // puts literal values into the set Include(byteset, 27]; // or byteset := byteset + [27]; Exclude(byteset, 3); // or byteset := byteset - [3]; end; Note that set types are limited to 256 elements of ordinal types (integer, char or enumeration). Howard

Thank you.
Does this mean that to be able to define a literal value like "byteset := [0, 3, 101]" for a set (and probably for an array) I must have defined a custom type for it; correct? (It's the only difference I see with my trials: numbers in my code is not of a custom type but simply a var of type "Set of Integer".) What I mean is, if the value is a literal, instead of only declaring the var then defining its value, one must first make a custom type for it? I don't understand the need & purpose of custom types except for records & enums; but this may be caused by the fact I come from dynamic languages.

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Alberto Narduzzi
to spir ☣
Thank you. Does this mean that to be able to define a literal value like "byteset := [0, 3, 101]" for a set (and probably for an array) I must have defined a custom type for it; correct? (It's the only difference I see with my trials: numbers in my code is not of a custom type but simply a var of type "Set of Integer".) What I mean is, if the value is a literal, instead of only declaring the var then defining its value, one must first make a custom type for it? I don't understand the need & purpose of custom types except for records & enums; but this may be caused by the fact I come from dynamic languages.

IIRC, you need define a custom type (e.g. TByteSet = Set Of Byte) only if you need to pass it to a function, that is, you cannot write:

Function CheckMySet(Var I:Set Of Byte):Integer;

but

Function CheckMySet(Var I:TByteSet):Integer;

Then, as you've been told, sets are represented as 0/1 bits in an array of 32 bytes (32 x 8 = 256), hence in your set you can only have values from 0 to 255 => no integers here...

For "sets" including bigger values, such as integers (or even reals) you could use a TStringList and its IndexOf(); thou' it may not be performant. Or use an array of sets...

Just my 2c.

A.

Reply by email

Howard Page-Clark
to spir ☣: On Mon, 19 Apr 2010 16:36:06 +0100 Thank you. Does this mean that to be able to define a…
Does this mean that to be able to define a literal value like "byteset := [0, 3, 101]" for a set (and probably for an array) I must have defined a custom

type for it; correct?

(It's the only difference I see with my trials: numbers in my code is

not of a custom type but simply a var of type "Set of Integer".)

What I mean is, if the value is a literal, instead of only declaring the var then defining its value, one must first make a custom type for it? I don't understand the need& purpose of custom types except for records& enums; but this may be caused by the fact I come from dynamic languages.

Yes, you have to use the "typeName = set of .." type declaration to introduce sets.
You can't have a "set of integer" because that would have more than 256 elements, and in FP (and Delphi I think) the implementation of sets is limited by that storage constraint.
Custom set types are often used in Pascal to enable meaningful names to be used for bitmapped values that would otherwise be hard-to-interpret numbers. For instance, the styles of the Font class are defined thus:

TFontStyle = (fsBold, fsItalic, fsStrikeOut, fsUnderline);
TFontStyles = set of TFontStyle;

If a font is both bold and italic its Style property will have the numerical value 3. However it is difficult to figure out what "3" means as a combination of styles. To express it as a combination of named set elements "fsBold" and "fsItalic" is self-explanatory - the very reason most of us prefer Pascal to many other languages.

Drop a TButton on a blank form of a new project. Add typinfo to the uses clause, and put this code in the OnClick event of the button.

procedure TForm1.Button1Click(Sender: TObject);
var styleset: TFontStyles;
     b: byte absolute styleset;
begin
   if Font.Style = [] then
     Font.Style:= Font.Style + [fsBold] + [fsItalic];
   styleset := Font.Style;
  ShowMessage(Format('Font.Style has the value "[%s]" or, as a numerical
value "%d"',
   [SetToString( FindPropInfo(Font, 'style'), integer(Font.Style) ), b ]
) );
end;

See how the numerical value of Font.Style is not very useful?

Reply by email

Marc Weustink
to spir ☣: Total Pascal newbie here. Looked for answers in various references and tutorials, but…
spir ☣ wrote 8 lines
Hello, Total Pascal newbie here. Looked for answers in various references and tutorials, but cannot find. Fore-question: Is there a (free)pascal teaching/learning mailing list? (Like python's "tutor" list.) If not, is this one a proper place? * How does one declare the type of set items? numbers : Set of Integer // error

A set can only have 256 elements, in you case that would have been 4G elements.
So the max is

   TByteSet = set of Byte;

but usually a set (or enum) is given a reable name like

   TMyEnum = (one, two, three);
   TMySet = set of TMyEnum;
* How does one define the _value_ of a Set or Array? numbers := (1,2,3) // error numbers := [1,2,3] // error
const
   Numbers: array[0..3] of integer = (1, 2, 3, 4);
   MySet: TMySet = [one, three];

Somehow I get the idea that you mix the definition/use of sets with arrays.

Marc

Reply by email

spir ☣
to Marc Weustink
Somehow I get the idea that you mix the definition/use of sets with arrays.

Yes, it seems Pascal sets are rather related to enums than collections. I mean they look like packs of kinds of flags, which themselves are named but value-free indicators. If I'm right, enums are sequences of such flags.

Denis
________________________________

vit esse estrany ☣

spir.wikidot.com

Reply by email

Marc Weustink
to spir ☣
spir ☣ wrote 7 lines
On Tue, 20 Apr 2010 01:17:51 +0200 Marc Weustink wrote: > Somehow I get the idea that you mix the definition/use of sets with arrays. Yes, it seems Pascal sets are rather related to enums than collections. I mean they look like packs of kinds of flags, which themselves are named but value-free indicators. If I'm right, enums are sequences of such flags.

Correct.

Marc

Reply by email