3 ms·
I've been working through the Advent of Code, doing it all in Lazarus/Free Pascal... the main limitation I've hit so far is in working out the logic, some of th
by mikewarot 6y ago
I've been working through the Advent of Code, doing it all in Lazarus/Free Pascal... the main limitation I've hit so far is in working out the logic, some of the puzzles are tricky. https://github.com/mikewarot/Advent_of_Code_in_Pascal https://github.com/mikewarot/Advent_of_Code_in_Pascal
[Edit]
One of the nice things about pascal strings, and variable parameters, is that you can write functions like
getinteger(var s : string):integer;
Which literally pulls a sign plus digits from s, skipping spaces, until it hits a space, comma, etc... s gets shorter, you get the integer, and it just works.
I don't think that type of thing is even possible in C because of null terminated unmanaged strings.
- rzzzt 6y agoFor "out" variables, you can increase the indirection and pass a char** (pointer to a pointer) to the method as the argument. Then the pointed-at pointer can be manipulated within the method body and changes will be visible on the outside as well, giving you both the numeric return value as well as the shortened string as the output. This is what the second argument of the strto{d,f,ld} function does: https://devdocs.io/c/string/byte/strtof https://devdocs.io/c/string/byte/strtof
- mikewarot 6y agoI appreciate that in C pointers are the coin of the realm, I really do appreciate not having to deal all the time, stuff just works. [Edit] For example, you can also insert into a string, making it longer, without having to worry about it.
- benibela 6y agoHowever, such pointers are unsafe, since someone could pass an invalid or null pointer var/out variables are much safer, since the variable exists at the calling site , is valid and not null. It is also much safer than C++'s & references. It is very subtle, but rather amazing if you think about it. Because C++ can return a & reference, it can return a reference to a value on the stack frame of the callee, which then becomes invalid. But the var/out variable always exists on caller's site, so it is always safe. (at least if it a reference to the stack. a reference to the heap could be freed indirectly through another reference. )
- rzzzt 6y agoI am not a heavyweight C++ user (also a very lightweight C and a, uhm... featherweight Pascal user), but I'd think using a C++ reference as a method argument should be as safe as Pascal's var parameters. Is returning references frequently done in C++? I have only seen it in ostream's << operator-like constructs where you want to chain multiple calls to the same object.
- pjmlp 6y agoNot necessarily, although it is seldom seen I the wild, you can cast a null pointer into a reference. Another source of issues might be the creation of temporaries. Yes, in index operator as well.
- benibela 6y ago>Is returning references frequently done in C++? It is rather common to avoid copying returned objects. (perhaps it becomes less common with std::move ?) For example, the [] operator on the standard collections returns a reference, so the item can be modified There Pascal has another clever idea. You can overload [] for reading and writing separately. Then x[k] := v becomes x.setItem(k, v)
- hiker 6y agoMaybe int getint(const char **s) { int res = 0; for (; **s && isspace(**s); (*s)++); for (; **s && isdigit(**s); (*s)++) res = 10 * res + **s - '0'; return res; }
- mikewarot 6y agoHere's the same code in pascal. function getnum(var s : string):integer; var c : char; value : integer; begin value := 0; while (length(s) <> 0) and NOT(s[1] in ['0'..'9']) do delete(s,1,1); while (length(s) <> 0) and (s[1] in ['0'..'9']) do begin value := value * 10 + (ord(s[1])-ord('0')); delete(s,1,1); end; getnum := value; end;
- benibela 6y agoAnd that would be faster than the Pascal version But it is unsafe. Someone might mistype * * s as *s and then bad things happen. Pascal strings are basically memory safe. With range checking enabled, you can write whatever and it can only access characters in the string. Ofc, then people disable the range checking in release builds to make it faster
- benibela 6y agoHowever, you should not actually do that, because changing s reallocates it, so you get O(n^2) runtime Should be like, getinteger(const s : string; var startPos: sizeint):integer; Pascal always has the best possible ideas, and then fucks up the implementation.
- mikewarot 6y ago> However, you should not actually do that, because changing s reallocates it, so you get O(n^2) runtime Confirmed by experiment. Parsing 1,2,...1000 string 1000 times 0.355 seconds Parsing 1,2,....10000 string 1000 times 42.835 seconds A given string might have 1-10 parameters in it, so the actual execution time isn't going to be that bad. I always think in terms of those 10 Mhz 286s, and how much faster it is these days. ;-) Obviously, if I were reading Gigabytes of text, I might optimize a bit.