#BabelOfCode 2024
Week 4
Language: FORTRAN

Confidence level: High

PREV WEEK: https://mastodon.social/@mcc/113867584791780280
NEXT WEEK: https://mastodon.social/@mcc/113975448813565537
RULES: https://mastodon.social/@mcc/113676228091546556

I was very excited about doing TCL this week, but I told myself the first time I get a two-dimensional array problem I'd go FORTRAN, so I guess this week is FORTRAN.

A friend of mine who did AOC2024 in December noted the early challenges this year were *very* easy. Today's definitely is. I wonder if part 2 will have any depth.

I went into this thinking: C is basically cleaned up FORTRAN, right? I know C? This should be easy, right? Right off the bat I find there will be a lot of difficulties entirely not of the kind I'm used to in programming. After a brief adventure with accidentally naming my file .f and not .f90 causing horrific and baffling errors, I run a hello world off the Internet. There's a space before the printout. Hm, how do I turn that off?

https://stackoverflow.com/a/31236043

Oh my fuck, *what*?

How to get rid of unwanted spacing in Fortran's print output?

It may look like a trivial issue, but I couldn't find any answer through googling. I have this little program : Program Test_spacing_print Integer:: N Real:: A,B N=4; A=1.0; B=100.0 prin...

Stack Overflow

I get frustrated with C all the time for being fundamentally a 70s language. It may be I'm about to learn the pain of using a *50s language*.

(Alternately, I hear modern FORTRAN has all kinds of fancy niceties like operator overloading and might not resemble traditional FORTRAN all that much. But then I have the problem if I pick up a random tutorial it's hard to guess which *decade's* standard it's teaching me from, or if it's the GNU extension, if the GNU extension is that different, etc.)

Just learned FORTRAN has an aint() function

Don't that just beat all

I am getting a weird floaty feeling from FORTRAN. I do not at any point really understand what I'm doing, but I am having little to no problems having any one particular thing. I keep doing google searches and getting which I do not understand the syntax of ( `write(error_unit, *) "String to write"`— wait, what? but which work. I do not know if I'd be able to get anywhere with this language if I didn't have either a more experienced programmer or Google+Stack Overfow.

Here is my current program. At the moment, all it does is take a command line argument (a path) and attempt to open the specified file. I build it with `gfortran src/puzzle.f90 -std=f2023 -o program`

https://github.com/mcclure/aoc2024/blob/822e460f81b944c21ca675303b868c45b22a4c2b/04-01-wordsearch/src/puzzle.f90

I'm having two problems, one serious, one unserious.

The unserious problem: I want to abort if the # of arguments is bad. If I do "error stop", it prints a backtrace, which I didn't ask for. If I do "call abort", gfortran fails to link ("undefined reference to 'abort_').

aoc2024/04-01-wordsearch/src/puzzle.f90 at 822e460f81b944c21ca675303b868c45b22a4c2b · mcclure/aoc2024

Advent of Code 2024 challenge (laid-back/"babel" version) - mcclure/aoc2024

GitHub

Here's my serious FORTRAN problem (code link in previous post):

The recommended GET_COMMAND_ARGUMENT function seems to assume you know the length of the argument ahead of time. Obviously, I don't.

https://gcc.gnu.org/onlinedocs/gfortran/GET_005fCOMMAND_005fARGUMENT.html

It allows me to pass in an `allocatable` string, but I believe it is leaving that string of length 0 if I do not ALLOCATE() it, and the fetched string is ''. Is there a way to get the length of a command-line argument before GET-ing it?

I see GET_COMMAND, but that's… awkward

GET_COMMAND_ARGUMENT (The GNU Fortran Compiler)

GET_COMMAND_ARGUMENT (The GNU Fortran Compiler)

…hm. I am concerned.

According to the FORTRAN working group

https://wg5-fortran.org/N2201-N2250/N2212.pdf

As of the 2023 standard, an un-allocated deferred-length variable may have its length set by calling intrinsic procedures; they give GET_COMMAND as an example specifically, and StackOverflow users assert GET_COMMAND_ARGUMENT is also included.

I don't get this behavior. My argument is being input as ''.

Do you think this means my code (linked above) is wrong, or that gfortran -std=f2023 is nonconformant?

@mcc how do you mean ". ? the shell won't let you enter it.
You'd have to quote it as '".' Using the example proggy from the manual for GET_COMMAND_ARGUMENT gives you then

$ ./a.out 333 '".'
./a.out
333
".

@dimpase The argument is a nonempty sequence of characters, but is being read in as the empty string. I say '' in my post because I don't know how else to represent a string of zero length in text.
@mcc Fortranically speaking, every NULL and empty string (wat? without a terminating character, or with?) are sins, committed by C-minded ppl...
@mcc In even-older versions of FORTRAN, you preallocate the maximum possible length of any such character array (yes character array) of any such value. If you wanted to be less wasteful about it you made a buffer character array at some stupid size and use that repeatedly as a safe buffer, then you could get the length of the string received from the very long buffer character array and copy it safely into a small string.
@moira Okay, but I am using FORTRAN 2023. Is there a less-silly way to do it, given FORTRAN 2023?

@mcc holy gods there's a 2023

I would hope so, but since I don't know it, I'm handing you an older-FORTRAN best practice you can use if you can't a better method.

@mcc I rarely use this, but the way I've done it in the past is to allocate the argv character to a sensible buffer size, initialise it to " ", and use trim() on it when using it, which will strip the unused part of the buffer.

Example:

character(len=4096) :: arg
arg = " "
call get_command_argument(1, arg)
call my_code(trim(arg))

@mcc IMHO GET_COMMAND_ARGUMENT assumes you know the maximal length of the argument (wearing my old Fortranista hat, feels very Fortranic). I don't know how it's padded, or truncated, if the length is too big. If it's too small, it gets truncated.
@mcc Sounds like FORTRAN to me.

@mcc

FORTRAN is the way.

@mcc does it have a y'all function? Because if so, I'm switching immediately

@foone pretty sure no

I assume that the "y'all" keyword would be a standin for every variable in scope. So you say

y'all += 4

and it adds 4 to all variables.

It might be possible to implement this in userspace in Python.

@mcc @foone But then what does all y'all do?
@darkling @foone y'all — increments all locals
all y'all — increments all globals and locals
@mcc @darkling @foone this suggests you could type y'all() and it would be the code equivalent of selecting everything on your desktop and pressing enter
@mcc @foone @darkling In some parts of Arkansas and elsewhere, “you ‘uns” is used instead of “y’all.” I assume “you ‘uns” would operate on all variables currently equal to 1.
@mcc
Y'all locals would prefer to go to a bar and not be 'incremented' though.
@darkling @foone
@mcc @foone can it be regionalized for Pittsburgh to be 'yunz'?

@plinth @mcc I recently wrote some code to automatically handle pronouns in a dialogue system, clearly I should extend it to handle the third person plurals.

I'm from the south, so my plural pronouns are ya/ll

@foone @mcc I'm from Northern New Jersey, so it's "youz guyz"
@mcc @foone yeah i can do this in python, one sec

@whitequark @mcc evil!

and it inspires me to do something somewhat similar for my terrible printf-debugging function

@whitequark @mcc @foone I love hate this so much, thank you
@whitequark @mcc @foone omg I am loving this thread so much 😂

@whitequark @mcc @foone
Nice!

That hard-coded `int` type is bugging me, though

For one thing, `yall += 4` should also increment float, Decimal and Fraction variables, as well as third-party types like NumPy arrays and ORM proxy objects

There's also no reason to disallow `yall += "!"` or `yall += [4]`

I don't know how to design it better, though. Maybe increment all variables regardless of type, possibly excluding `self`? Not sure

@sabik @mcc @foone hasattr("__iadd__") and ignore exceptions?
@whitequark @mcc @foone
Maybe, but ignoring exceptions doesn't feel pythonic
@sabik @whitequark @mcc @foone query: does {implicitly altering every variable defined in the local scope} feel pythonic?
@catgirl_so @whitequark @mcc @foone
implicitly altering every variable defined in the local scope is the joke; it's not meant to be pythonic
@catgirl_so @whitequark @mcc @foone
Like in fiction, you're allowed to have unrealistic elements, people changing into animals when that's the plot of the movie; you shouldn't mess up the phase of the moon for no reason
@sabik @catgirl_so @mcc @foone this is an extreme level of overthinking and i'm here to enjoy it
@sabik @whitequark @mcc @foone isinstance(numbers.Number) should work? It does a bunch of abstract class magic to make itself a sub-class of essentially every numeric thingie class in Python

@whitequark @mcc @foone

You chose violence. Seriously Lawful Neutral energy.

I approve.

Now, what kind of practical code can we come up with that needs y'all() ?

@pseudonym @whitequark YouTube series where you bind y to yall += 1 in various programs and document what happens
@mcc I wonder what blessyourheart() would do. Surely something that looks good to the initiated, but is scarring to those who understand. @foone
@mcc however, it ain't what you think.
@mcc Jumping into Fortran is quite an interesting experience. I became one of 6 core developers of a Fortran physics simulation code at CERN back in 2017 until I left in 2020. We converted a lot of the code to Fortran 2003. It's not a bad language once you learn the quirks, and it's relatively easy to interface with C. (I mean, most things are.)

@veronica Where would you recommend starting to ensure I'm learning FORTRAN 2003 or FORTRAN 2023 and not something ancient and unnecessarily inconvenient?

Does anyone use GNU FORTRAN (ie, asking if they use -std=gnu, not asking if they use gfortran) in industry?

@mcc We supported GNU, Intel and the Nagfor compilers. Honestly, my best resource was the Intel Fortran forums, which are not public. I had a university Intel account though.

I also have a draft copy of one of the Fortran standards that I used for reference. It is quite tricky to find good resources online unfortunately.

@mcc
In my org we use fortran pretty heavily, but on a mix of platforms (ie some gfortran, some ifx, etc) 2023 feature use is still kind of rare, so if i were recommending for someone *here*, I'd say focus on 2003 vs 2023, but be aware you will eventually come across 2023 features. If you start from 2023, you might run into situations where features aren't available in the target environment (if it's someone elses)

I believe most of our environments are to 2018.
@veronica
@mcc @veronica One of the best books to get started is "Modern Fortran explained" by Metcalf et al.