Basic Tips
- Most whitespace is optional:
FORiFROM1TO9DOprint(i)OD
- You can omit different parts of
FOR:
FORiTO9DOprint(i)OD - loops from 1 to 9
TO9DOprint(i)OD - loops 9 times
FORiDOprint(i)OD - counts up from 1 forever
- Use
(…|…|…) instead of IF … THEN … ELSE … FI or CASE … IN … OUT … ESAC
MOD is %*, OVER is %, ** is ^
- There are a few useful undocumented functions. Browse through prelude.c.
Numerics
You can set the precision of LONG LONG INT and LONG LONG REAL to arbitrary numbers of digits by using a pragma like PRprec=1001PR (1001 digits). This can appear absolutely anywhere in your code, so put it somewhere that doesn't require any whitespace around it. You can also use 1k for 1000.
Consider whether you might be able to use evaluate. eg if each arg is a single integer:
INTn;FORiTO100DOevaluate("n:="+argv(i+4))
# ... do things with n ... #
OD
For simple inputs that can't work with evaluate, you might want to use gets. You could parse two integers separated by a space like this:
STRINGa;INTx,y;FORiDOgets(a:=argv(i+4),(x,y))
# ... do things with x and y ... #
OD
Finally, for formats that involve more than just whitespace, you can use getsf. For example, to parse YYYY-MM-DD dates:
STRINGa;INTy,m,d;FORiDOgetsf(a:=argv(i+4),($gx$,y,m,d))
# ... do things with y, m, and d ... #
OD
The x in the format skips a single character (- in this case). If the format pattern runs out while there are still variables, it will start over from the beginning, so all three variables will use the same pattern. (This works for output formats too!)
Output
- C-Style format specifiers are useful: use
printf(($%dl$,i)) instead of printf(($g(0)l$,i))
- Use
q instead of " "
- Use
n(...) to replicate something, e.g. printf($%n(j)q$) to print j spaces (page 155 of the manual)
- Format specifiers can sometimes provide useful ways to crash