"set of": order of elements lazarus / 21-General ---------------------------------------------------------------------- Rik · 2025-02-05 15:40 Is there a way to figure out the order (sequence) of the elements is a set? A simple example to make clear what I am looking for: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  tColour = (cBlack,cBlue,cRed,cWhite,cYellow,cGreen,cOrange);  tFlag = set of tColour; procedure ShowFlag(ThisCountry: string;ThisFlag: tFlag);var  n: DWord;  s: string;begin  n:= PopCnt(DWord(ThisFlag)); // number of colours in the flag  s:= 'the flag of '+ThisCountry+' has '+IntToStr(n)+' colours:';   ....   ShowMessage(s);end; { TMainForm } procedure TMainForm.FormActivate(Sender: TObject);begin  ShowFlag('Ireland',[cGreen,cWhite,cOrange]);  ShowFlag('Ivory Coast',[cOrange,cWhite,cGreen]);end;  The outcome of the ShowFlag procedure should be Quote- the flag of Ireland has 3 colours: green white orange - the flag of Ivory Coast has 3 colours: orange white green I can check for any colour if it is in the flag, eg: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   if cWhite in ThisFlag then s:= s+' white';But so far I didn't find a way to do this for a specific (1st, 2nd, 3rd) element. And I know using "set of" it won't work for flags with twice the same colour, but it's just an example to explain what I am aiming for. ---------------------------------------------------------------------- TRon · 2025-02-05 15:43 Set operators ---------------------------------------------------------------------- Remy Lebeau · 2025-02-05 15:46 You can use tColour's RTTI (GetTypeData(TypeInfo(tColour))) to find out its low (0) and high (6) values, then loop through those values with a for loop, using the 'in' operator to check if each value is present in ThisFlag, and if so then use the RTTI to convert the value to its defined name (GetEnumName()). ---------------------------------------------------------------------- ASerge · 2025-02-05 15:51 Quote from: Rik on February 05, 2025, 04:40:33 pmIs there a way to figure out the order (sequence) of the elements is a set? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$MODE OBJFPC}{$IFDEF WINDOWS}  {$APPTYPE CONSOLE}{$ENDIF} type  TColour = (cBlack, cBlue, cRed, cWhite, cYellow, cGreen, cOrange);  TFlag = set of TColour; procedure PrintFlag(AFlag: TFlag);var  C: TColour;  i: Integer = 0;begin  for C in AFlag do  begin    Writeln('Item ', i, ' = ', C);    Inc(i);  end;end; begin  PrintFlag([cGreen, cWhite, cOrange]);  Readln;end.Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Item 0 = cWhiteItem 1 = cGreenItem 2 = cOrange ---------------------------------------------------------------------- marcov · 2025-02-05 15:52 Sets are not ordered, as you can see here where I assign the values in other order, but the declaration order comes out. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi} uses sysutils;type  tColour = (cBlack,cBlue,cRed,cWhite,cYellow,cGreen,cOrange);  tFlag = set of tColour; var aset : TFlag;    acol : TColour;     s,s2 : string;begin  aset:=[corange,cblue,cyellow];  s:='';  for acol in aset do    begin    writestr(s2,acol);      s:=s+s2+' ';    end;  writeln(s);end. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 15:56 https://gitlab.com/freepascal.org/fpc/source/- /blob/d0b4e8730a687d0bb85fafb773245d34728e5e56/rtl/x86_64/set.inc I assume they are packed in bits, in some order, in ascending order of address or vice versa This can be found out in runtime, but in general you probably shouldn't rely on internal implementation details, today it works, 20 years from now it won't work ) PS: although I don't think that the implementation of a simple set will ever change (without introducing new flags) ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 15:59 Quote from: Rik on February 05, 2025, 04:40:33 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  tColour = (cBlack,cBlue,cRed,cWhite,cYellow,cGreen,cOrange);  tFlag = set of tColour; procedure ShowFlag(ThisCountry: string;ThisFlag: tFlag);begin  n:= PopCnt(DWord(ThisFlag));  In general, you can't do that Set size = 32 bytes, and you set it to DWORD Some data may simply be cut off ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 16:13 Quote from: marcov on February 05, 2025, 04:52:15 pmSets are not ordered, as you can see here where I assign the values in other order, but the declaration order comes out. They are ordered by the declaration in the type, not by how you assign them (but this may be an implementation detail, you need to look at the help) ---------------------------------------------------------------------- Thaddy · 2025-02-05 18:02 As Marcov wrote already: sets are by definition not ordered. Don't look stupid. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-05 18:11 Quote from: Thaddy on February 05, 2025, 07:02:09 pmAs Marcov wrote already: sets are by definition not ordered. Don't look stupid. I have absolutely no interest in talking to you or proving anything to you. Why? Because you have the social skills of a brick and the charm of a malfunctioning alarm clock. Every time you open your mouth (or, in this case, type), it’s like watching someone trip over their own shoelaces—awkward, unnecessary, and painful for everyone involved. Seriously, who let you out of… wherever it is people like you are usually kept away from normal society? Because I refuse to believe you just walked among us this whole time without someone trying to send you back. So do me, and quite frankly everyone else, a favor: stop replying to my messages, stop poisoning conversations with your attitude, and maybe, just maybe, try acting like a decent human being for once in your life. Until then—goodbye, and may common sense find you someday! ---------------------------------------------------------------------- BrunoK · 2025-02-05 18:14 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Country; uses  SysUtils; type  tColour = (cBlack, cBlue, cRed, cWhite, cYellow, cGreen, cOrange);  tFlag = set of tColour;  tColors = array of tColour;   procedure ShowFlag(ThisCountry: string; ThisFlag: tColors);  var    n: DWord;    lFlag: tColour;    lFlagStr: string;  begin    Write('<', ThisCountry, '> ');    for lFlag in ThisFlag do begin      Str(lFlag, lFlagStr);      Write(lFlagStr, ' ');    end;  WriteLn;  end;   { TMainForm } begin  ShowFlag('Ireland', [cGreen, cWhite, cOrange]);  ShowFlag('Ivory Coast', [cOrange, cWhite, cGreen]);  ReadLn;end. cGreen cWhite cOrange cOrange cWhite cGreen ---------------------------------------------------------------------- Mr.Madguy · 2025-02-06 07:17 tColour - is enumerable type. What happens in reality, if you don't specify values explicitly - is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   tColour = (cBlack=0,cBlue=1,cRed=2,cWhite=3,cYellow=4,cGreen=5,cOrange=6); And set is usually about converting enumerable into value of bit via "1 shl x", where x is tColour. So, [cYellow,cGreen,cOrange] is actually something like "(1 shl cYellow) or (1 shl cGreen) or (1 shl cOrange)". As "or" is commutative operator, order of values in set doesn't matter. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 07:59 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test;type  TSet = set of byte; procedure printset(const x: TSet);begin  Write(HexStr(PUInt64(@x + 24)^, 16));  Write(HexStr(PUInt64(@x + 16)^, 16));  Write(HexStr(PUInt64(@x + 8)^, 16));  WriteLn(HexStr(PUInt64(@x + 0)^, 16));end; begin  printset([]);  printset([0]);  printset([1]);  printset([0,1]);  printset([1,0]);  printset([255]);  printset([254]);  printset([255,254]);  printset([254,255]);   ReadLn;end.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 00000000000 0000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000100000000000000000000000 0000000000000000000000000000000000000000200000000000000000000000000000 0000000000000000000000000000000000300000000000000000000000000000000000 0000000000000000000000000000380000000000000000000000000000000000000000 0000000000000000000000040000000000000000000000000000000000000000000000 00000000000000000C0000000000000000000000000000000000000000000000000000 00000000000C0000000000000000000000000000000000000000000000000000000000 00000  The internal representation has a quite definite storage order, and all sets of any type will be internally represented in exactly the same way I.e. there is a one-to-one correspondence between an element and its place in the set Yes, but it is impossible to know from the set in what order the elements were at the moment of assignment ---------------------------------------------------------------------- MarkMLl · 2025-02-06 09:52 Quote from: ALLIGATOR on February 05, 2025, 07:11:41 pmQuote from: Thaddy on February 05, 2025, 07:02:09 pmAs Marcov wrote already: sets are by definition not ordered. Don't look stupid. I have absolutely no interest in talking to you or proving anything to you. Why? Because you have the social skills of a brick and the charm of a malfunctioning alarm clock. Every time you open your mouth (or, in this case, type), it’s like watching someone trip over their own shoelaces—awkward, unnecessary, and painful for everyone involved. I am not a mod, but suggest that being gratuitously rude to another member of the community does not help answer the question raised by the OP. That's particularly the case when the other member's many years of relevant experience do on occasion appear to have exacted a price >:-/ Furthermore, the fact that you're interested in Pascal would be seen by many as a strong suggestion that you too are heading towards the "silly old fool" bitbucket. Marco is a member of the core team. Thaddy was attempting to draw your attention to the point he made. There is nothing in the Object Pascal language definition that mandates any particular order of a set. There is nothing in the FPC implementation that mandates any particular order of a set, and in practical terms it's easy to imagine an implementation that stored elements of a large set sparsely as e.g. an indexed list which didn't have a natural evaluation order. In particular, there is no Succ() and Pred(), and no Inc() and Dec() of a set type. You can of course apply them to a set which has been cast into an integer or enumeration type, but there is nothing in the langauge that defines unambiguous rules for how casting (AKA type transfer etc.) works. So to summarise: sets have no defined order, and relying on a particular order because experimental code suggests a particular pattern is unwise. MarkMLl ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 10:11 Quote from: MarkMLl on February 06, 2025, 10:52:14 amI am not a mod, but suggest that being gratuitously rude to another member of the community does not help answer the question raised by the OP. That's particularly the case when the other member's many years of relevant experience do on occasion appear to have exacted a price >:-/ Are you talking about me or the other person to whom I addressed my words? If you mean me, I am surprised, because this second person is not shy in his choice of words on the forum. I just told him not to talk to me like that. If someone likes his tone - let them listen to him, I don't want to. And about everything else you said - I agree, in fact in several posts earlier I said the same thing. That there is an order in terms of internal storage, but this is the details of implementation and you should not rely on it, and that the order in which values were assigned to the set - this order is not preserved ---------------------------------------------------------------------- 440bx · 2025-02-06 10:32 Quote from: MarkMLl on February 06, 2025, 10:52:14 amI am not a mod, but suggest that being gratuitously rude to another member of the community does not help answer the question raised by the OP. If there is a forum member who has turned being "gratuitously rude" into an art form, that member is Thaddy. It really should come as no surprise that he occasionally gets a stern response.  Actually, the surprise is how much his, too often unpleasant, attitude is tolerated. As far as element ordering in sets, a set is by mathematical definition, unordered.  However, for a compiler to implement sets _efficiently_ ordering the set elements is required, that is necessary for the compiler to represent a set element with a single bit.  Of course, the criteria used by the compiler to order the elements is implementation dependent. Other implementations that do not depend on ordering are possible but, they are significantly less efficient in terms of speed and memory usage, e.g., dictionaries or other hashing dependent structures. ---------------------------------------------------------------------- MarkMLl · 2025-02-06 10:36 Quote from: ALLIGATOR on February 06, 2025, 11:11:50 amAre you talking about me or the other person to whom I addressed my words? If you mean me, I am surprised, because this second person is not shy in his choice of words on the forum. I just told him not to talk to me like that. If someone likes his tone - let them listen to him, I don't want to. I'm talking to you. To summarise: not in here please. QuoteAnd about everything else you said - I agree, in fact in several posts earlier I said the same thing. That there is an order in terms of internal storage, but this is the details of implementation and you should not rely on it, and that the order in which values were assigned to the set - this order is not preserved In that case we're in broad agreement. I'd add that I regularly put startup checks in units, in particular that data structures that are supposed to mimic an OS parameter has the expected size. Which I suppose takes us onto another detail. It's very common in C programming to see an integer parameter defined as individual bits shifted left by a bit number. It's tempting to define that in Pascal as a set based on an enumeration etc., but in my opinion very unwise. MarkMLl ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 10:42 If we shift our focus to real work instead of endless discussions and arguments, I have a piece of code that I’d be happy to recommend to the topic starter. I believe they’ll find it both useful and interesting—at least, I’d like to think so. No false modesty here! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program test; uses SysUtils, TypInfo; type  tColour = (cBlack,cBlue,cRed,cWhite,cYellow,cGreen,cOrange);  tFlag = set of tColour;  tFlagArray = array of tColour; procedure ShowFlag(const ThisCountry: string; const ThisFlag: tFlagArray);var  s,t: string;  c: tColour;begin  s:= 'The flag of ' + ThisCountry + ' has ' + IntToStr(Length(ThisFlag)) + ' colours: ';  for c in ThisFlag do  begin    t:=GetEnumName(TypeInfo(ThisFlag[0]), ord(c));    s:=s + RightStr(t, Length(t)-1) + ' ';  end;  WriteLn(s);end; begin  ShowFlag('Ireland',    [cGreen,cWhite,cOrange]);  ShowFlag('Ivory Coast',[cOrange,cWhite,cGreen]);   ReadLn;end. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i The flag of Ireland has 3 colours: Green White OrangeThe flag of Ivory Coast has 3 colours: Orange White Green ---------------------------------------------------------------------- MarkMLl · 2025-02-06 10:59 Quote from: 440bx on February 06, 2025, 11:32:45 amIf there is a forum member who has turned being "gratuitously rude" into an art form, that member is Thaddy. It really should come as no surprise that he occasionally gets a stern response.  Actually, the surprise is how much his, too often unpleasant, attitude is tolerated. I can assure you that with the combination of Th. (rushing into areas where angels fear to tread) and J. (using every opportunity to try to entice people into her parlour on IRC) I've come very close on occasion to throwing in the towel and abandoning not only this forum but also Pascal. Both of them, in my opinion, are immoderately fond of the sound of their own voice and confuse the "Posts:" count with objective correctness and subjective self-worth. But with a bit of schooling and a few decades practice it's possible to be rude politely. QuoteAs far as element ordering in sets, a set is by mathematical definition, unordered.  However, for a compiler to implement sets _efficiently_ ordering the set elements is required, that is necessary for the compiler to represent a set element with a single bit.  Of course, the criteria used by the compiler to order the elements is implementation dependent. Other implementations that do not depend on ordering are possible but, they are significantly less efficient in terms of speed and memory usage, e.g., dictionaries or other hashing dependent structures. Regrettably, that is something that is regularly overlooked by the CS fraternity who are so intent on generalising that they're quite happy to e.g. implement even a small set using a linked list (and then expend multiple research programs optimising the operations). That was, of course, one of the bad things about APL. MarkMLl ---------------------------------------------------------------------- BrunoK · 2025-02-06 11:48 Quote from: ALLIGATOR on February 06, 2025, 11:42:50 amIf we shift our focus to real work instead of endless discussions and arguments, I have a piece of code that I’d be happy to recommend to the topic starter. I believe they’ll find it both useful and interesting—at least, I’d like to think so. No false modesty here! Mostly what I submitted with the additional complication of    Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i t:=GetEnumName(TypeInfo(ThisFlag[0]), ord(c)); ---------------------------------------------------------------------- Thaddy · 2025-02-06 12:53 There is no order in a set, only membership. Period. So test for membership and do that with a mask, that is one cycle. Alligatorinae seem not to be the smartest of all animals, all be it the longest living species. ---------------------------------------------------------------------- BrunoK · 2025-02-06 12:56 Quote from: MarkMLl on February 06, 2025, 10:52:14 amQuote from: ALLIGATOR on February 05, 2025, 07:11:41 pmQuote from: Thaddy on February 05, 2025, 07:02:09 pmAs Marcov wrote already: sets are by definition not ordered. Don't look stupid. I have absolutely no interest in talking to you or proving anything to you. Why? Because you have the ... I am not a mod, but suggest that being gratuitously rude to another member of the community does not help ... When dealing with Grumpy posts, never forget to quote what he wrote. He is very good at disappearing his most useless contibutions or edit then to avoid excessive embarrassment. ---------------------------------------------------------------------- Thaddy · 2025-02-06 12:58 I did, post crossed. There is no point in being stupid. ---------------------------------------------------------------------- ALLIGATOR · 2025-02-06 14:02 Quote from: BrunoK on February 06, 2025, 12:48:05 pmMostly what I submitted with the additional complication of    Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i t:=GetEnumName(TypeInfo(ThisFlag[0]), ord(c)); 👍Indeed, sorry, didn't notice your post (while I was dealing with a very important person (as he apparently thinks he is 😂)). ---------------------------------------------------------------------- 440bx · 2025-02-06 14:16 Just in case someone is interested, here is a little bit of code that shows the element ordering done by the compiler and the mask associated with each set element. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  {$MODE OBJFPC}{$IFDEF WINDOWS}  {$APPTYPE CONSOLE}{$ENDIF} program _SetElementsOrder; uses StrUtils; type  TColour = (cBlack, cBlue, cRed, cWhite, cYellow, cGreen, cOrange);  TFlag = set of TColour; procedure PrintSetMasks();var  AFlag     : TFLAG = [];  AFlagBits : bitpacked array[0..bitsizeof(longint) - 1] of boolean            absolute AFlag;   C     : TColour; begin  writeln('sizeof TColor: ', sizeof(TColour));  writeln('sizeof AFlag : ', sizeof(AFlag));  writeln;   { figure out how the compiler orders the elements, i.e., from LSB to MSB or }  { MSB to LSB                                                              }   write('bit ordering is ');  AFlag := [low(TColour)];  case AFlagBits[ord(low(TColour))] of  TRUE : writeln('LSB to MSB');    FALSE: writeln('MSB to LSB');  end;  writeln;   for C in TColour do  begin    AFlag := [C];    writeln('Colour: ', C:8, ' binary mask: ', IntToBin(longint(AFlag),                                             bitsizeof(longint),                                             1));  end;end; begin  repeat    { make sure the code can handle a set of TColour                        }     if sizeof(TFlag) <> sizeof(longint) then    begin      writeln('This program cannot handle sets with more than ',            bitsizeof(longint), ' bits');      break;    end;     writeln;    PrintSetMasks();     break;  until TRUE;   writeln;  writeln('press ENTER/RETURN to end this program.');  readln;end.                         On an intel/Windows machine, the bit number is the ordinal value of the enumeration elements that comprise the set (no surprise there.) ETA: Quote from: Rik on February 05, 2025, 04:40:33 pmIs there a way to figure out the order (sequence) of the elements is a set? The answer is yes and the program above shows how to do it.  Questions, if you have any, are welcome. ---------------------------------------------------------------------- Rik · 2025-02-06 15:37 Thanks to all for responding. At the end it turned out to be rather easy to get the order of the set elements (as given in the function call). Combining Quote"set of ..." and Quote"array of ..." as suggested by BrunoK was (at least for what I intended) the way to go. ---------------------------------------------------------------------- CM630 · 2025-02-06 16:49 Almost, you do not need a set at all, all you need is an array. I have commented a line (//  tFlag = set of tColour;) in @BrunoK's code, the app still works, I get he just forget to remove it. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Country; uses  SysUtils; type  tColour = (cBlack, cBlue, cRed, cWhite, cYellow, cGreen, cOrange);//  tFlag = set of tColour;  tColors = array of tColour;   procedure ShowFlag(ThisCountry: string; ThisFlag: tColors);  var    n: DWord;    lFlag: tColour;    lFlagStr: string;  begin    Write('<', ThisCountry, '> ');    for lFlag in ThisFlag do begin      Str(lFlag, lFlagStr);      Write(lFlagStr, ' ');    end;  WriteLn;  end;   { TMainForm } begin  ShowFlag('Ireland', [cGreen, cWhite, cOrange]);  ShowFlag('Ivory Coast', [cOrange, cWhite, cGreen]);  ReadLn;end.  Besides that, I have no idea if dynamic sets exist. ---------------------------------------------------------------------- 440bx · 2025-02-06 18:27 Quote from: CM630 on February 06, 2025, 05:49:05 pmAlmost, you do not need a set at all, all you need is an array. He can have both simultaneously by overlaying a bitpacked array of boolean on top of the variable of type TFlags, then he can use the values in tColour to index into the bitpacked array just as if it were an array of TColour.  That's what the code I posted does. A potentially significant advantage of that method is being able to use the set operators.  Another potential advantage is that, for small sets (32 elements or less), the entire "array" fits in one register.  (it would be nice if that optimization was extended to 64 elements when creating a 64 bit app... but that's a different subject.) ---------------------------------------------------------------------- PascalDragon · 2025-02-06 21:14 Quote from: MarkMLl on February 06, 2025, 11:59:17 amQuote from: 440bx on February 06, 2025, 11:32:45 amIf there is a forum member who has turned being "gratuitously rude" into an art form, that member is Thaddy. It really should come as no surprise that he occasionally gets a stern response.  Actually, the surprise is how much his, too often unpleasant, attitude is tolerated. I can assure you that with the combination of Th. (rushing into areas where angels fear to tread) and J. (using every opportunity to try to entice people into her parlour on IRC) I've come very close on occasion to throwing in the towel and abandoning not only this forum but also Pascal. Both of them, in my opinion, are immoderately fond of the sound of their own voice and confuse the "Posts:" count with objective correctness and subjective self-worth. I tend to just skip over their posts if there's something that needs to be corrected and otherwise ignore them... 🤷‍♀️ Quote from: Thaddy on February 06, 2025, 01:53:49 pmAlligatorinae seem not to be the smartest of all animals, all be it the longest living species. The code that ALLIGATOR showed is essentially the same that BrunoK showed, both of which use an array of the enum type as parameter instead of a set and thus have an order. ---------------------------------------------------------------------- Mr.Madguy · 2025-02-07 07:25 Core problem - is when programmers are taught to code via current popular languages like Java, JS or Python and as result become "monkey" coders, who don't understand what they do, cuz they don't know underlying mechanisms behind their code. I don't try to say, that each programmer should always learn Asm first in order to understand, what really happens in his code. Asm is really bad first language candidate. But I think, that good programmer should be interested in underlying mechanisms behind his code. And that Pascal is the best first language to learn. It's not as complicated, as C/C++, but it's powerful enough. And it's compilable, not interpretable. Overall idea - each language feature doesn't exist just for sake of existence. It's usually just wrapper around some commonly used low level code. Set is wrapper around bitfield. If you need ordered data structure - use array. ---------------------------------------------------------------------- 440bx · 2025-02-07 09:11 Quote from: Mr.Madguy on February 07, 2025, 08:25:29 amAsm is really bad first language candidate. Personally, I think it's likely the best choice if the goal is to learn how a computer really works. What can make it very undesirable to some is the large amount of information that must be learned which contributes to a fairly steep learning curve but, a programmer who is comfortable with assembler can learn a language like non-OOP Pascal or C fairly quickly. ---------------------------------------------------------------------- MarkMLl · 2025-02-07 10:44 Quote from: 440bx on February 07, 2025, 10:11:31 amQuote from: Mr.Madguy on February 07, 2025, 08:25:29 amAsm is really bad first language candidate. Personally, I think it's likely the best choice if the goal is to learn how a computer really works. What can make it very undesirable to some is the large amount of information that must be learned which contributes to a fairly steep learning curve but, a programmer who is comfortable with assembler can learn a language like non-OOP Pascal or C fairly quickly. Practitioners should certainly be exposed to assembly language and to a "typical" machine architecture with registers etc. fairly early on. However I'm unconvinced that that should be right at the start of their training, since unless the opcodes are heavily augmented by "begin", "end" macros etc., i.e. the sort of technology with which Wirth was familiar when designing PL/360 and then Pascal, it risks undermining the importance of the structured programming philosophy that underlies most current languages. MarkMLl ---------------------------------------------------------------------- Mr.Madguy · 2025-02-07 10:49 Quote from: 440bx on February 07, 2025, 10:11:31 amPersonally, I think it's likely the best choice if the goal is to learn how a computer really works. What can make it very undesirable to some is the large amount of information that must be learned which contributes to a fairly steep learning curve but, a programmer who is comfortable with assembler can learn a language like non-OOP Pascal or C fairly quickly. For adults? May be. But it would be way too hard for children. My first language was Basic. What made me learn Asm - was that I wanted to make games and Basic was too slow and limited for it. My guess was - if other developers could do it, then I also could. That leaded to well known "Asm syndrome", when you think that only Asm code can be fastest. But I changed my mind. Even back in old times, when compilers lacked optimization features, Asm was suitable for critical tasks only, while it was better to use high level language for "glue" code. That's why I learned Pascal then. ---------------------------------------------------------------------- 440bx · 2025-02-07 11:09 I won't deny that learning Assembly as a first language is definitely not easy. Another reason I think it may be a good first language is because, writing a program in assembler, pretty much forces the programmer do to at least some design upfront and often reconsider how the code is structured while the code is being written because assembly code can get unmanageable very quickly. IOW, unlike in high level languages where programmers can get away with a poor design (or what is often no design or design on the fly) and keep throwing code at the computer until the program seems to work, that way of "designing" a program will rarely be successful when using assembly.  Basically, coding in assembler teaches a programmer the value of upfront design and the downsides of "code and think later" which is something high level languages somewhat encourage by their being high level. The other thing I like about learning assembly first is that the programmer learns what the _true_ limits are (those imposed by hardware and architecture) instead of whatever mostly artificial limits are imposed by a high level language. All that said, I definitely understand why it may be considered a "less than ideal" choice by many but, I don't consider it a bad choice as a first language to learn (I do consider it a tough one though.) ---------------------------------------------------------------------- Thaddy · 2025-02-07 12:26 Quote from: 440bx on February 07, 2025, 10:11:31 amQuote from: Mr.Madguy on February 07, 2025, 08:25:29 amAsm is really bad first language candidate. Personally, I think it's likely the best choice if the goal is to learn how a computer really works. Here for once I totally agree. (As long you agree there is no order in a set, that is.) ---------------------------------------------------------------------- 440bx · 2025-02-07 14:23 Quote from: Thaddy on February 07, 2025, 01:26:37 pmHere for once I totally agree. (As long you agree there is no order in a set, that is.) I'll quote myself: Quote from: 440bx on February 06, 2025, 11:32:45 amAs far as element ordering in sets, a set is by mathematical definition, unordered. Mathematically sets are collections of _unordered_ items. That said, compilers that implement sets, such as FPC may and usually do impose an order on the set elements to allow the compiler to represent each element in the set with a single bit.  To see that in action, run the code I included in a previous post.  However, that order imposed by the compiler, is _not_ equivalent to ordering the elements in the set, IOW, the sets [cBlack, cOrange] and [cOrange, cBlack] are equal, the different order does not affect equality. If the element sequence is important, e.g, [cBlack, cOrange] <> [cOrange, cBlack], then a set is not a solution.  An array can be a solution for such sequences.