I started teaching my oldest 8 years old son programming. So we naturally used Fortran. Here is some feedback that I noticed while doing that. We did three sessions so far.
First one a few months ago in Python on Linux, Gedit as an editor, using
python a.py in a terminal to run. It works, but not having the compilation step actually kind of hurts, it’s good to have the compiler check the syntax and semantics and provide good feedback. The Python runtime errors are not always the most helpful for a complete beginner. But it does the job.
Second one on Linux, using LFortran, VSCode with the built-in terminal, where we always recompiled and ran by hand. I had to turn off my Vim bindings for VSCode so that he can actually use it.
The third session we did on a macOS, I decided to use
fpm, which doesn’t work with LFortran yet, so I installed GFortran using Conda, had to activate the environment. We used the default editor (
TextEdit) on macOS when you type
open some_file.f90 in a terminal. It was auto-correcting to upper case at the beginning of the “sentence”, so this was not a good experience. Also we had to save every time before switching to the terminal to
fpm run. (I like to setup my Neovim in a terminal and VSCode to automatically save the file when losing focus, so I just switch to the terminal and it gets saved.)
I spent about 20 minutes installing Conda, first I installed
conda install fpm gfortran into an environment, and linked them into
$HOME/bin which I put into
fpm works, but
gfortran failed to link when invoked that way… So I removed the
$HOME/bin and installed
gfortran into the
base Conda environment and loaded it by default when a new terminal starts. That works.
Suggestion: document on fortran-lang.org how to get started on each platform using Conda in 5 minutes. Conda is nice because it does not require root access and installs locally, in a non-invasive manner. And the usage from the user perspective is almost identical on all platforms. (#280)
Suggestion It would be nice to settle on the same editor on all platforms, probably VSCode and document how to autosave, as that is really nice and helpful, as my son forgets to save before recompiling.
fpm works great, but here are a few suggestions:
- Hide the compilation commands, unless requested with
fpm -v, and instead just show the file being compiled and a progress bar. In colors. Nice and clean. Could even show the Fortran module dependency graph and highlight in color where in the dependency graph we are.
- When an error happens, it shows a stacktrace, but on macOS it just shows hashes, no lines.
fpmshould be compiled with
-gand ensure it shows a nice stacktrace on macOS. The same when an error happens in the compiled program.
fpm is a real life saver — everything just works and it hides the details. Previously I didn’t want to go into explaining CMake, so we compiled using
lfortran a.f90 && ./a.out, but it’s just not as nice and it does not really scale well once we use more than one module.
I didn’t even bother trying to explain why
implicit none has to be in every file (we only used one file, the main program so far). The good news is that my son didn’t seem to be bothered by this “noise”.
Suggestion: perhaps make
implicit none by default using a compiler option in
fpm, so that users don’t have to type it? I think that should work, as if
cmake files, it would include the option, and if somebody uses the file in another project by copying it, it would still work because all variables would be declared.
Otherwise Fortran was very easy to teach, we used exponentiation, +, *, do loop,
print * and
read (*,*). Integer variables. The compiler helps — my son tried to assign to a loop variable and GFortran correctly did not allow it.
LFortran works fine for this, but the error messages are not the best yet. But we will fix it.
GFortran has decent error messages, but it has some stuff that should be improved, for example when a variable is not declared, it says:
$ gfortran expr2.f90 expr2.f90:6:15: 6 | x = (2+3)*5 + z | 1 Error: Symbol ‘z’ at (1) has no IMPLICIT type
I usually don’t read the messages when developing, I know right away that it was not declared. But with my son I actually read it. This is completely cryptic – I know what it means (the variable is not declared, and there is no implicit type). But good luck explaining this. I don’t want an “implicit type”. I want this to be declared. Suggestion: It should say:
Variable or symbol z
at (1) is not declared.
Everything works and we will continue. I explained that there is a source code, the compiler checks the code and produces a machine code. We looked at the executable using Vim, I explained that these are machine instructions that the CPU executes, we found the string that we printed in the machine code, etc. I explained that
fpm run just runs this compiled executable. He got all that at this high level, it’s not complicated.
There is no problem using the terminal, he got that really quickly. We used the following commands:
fpm new and
Is anyone else teaching their kids? I’d be interested in hearing your experience.