$ spark -ne '(say "${^LINENUM} ${^LINE}")'
Here we can see the string interpolation of variables in action. ${….}
interpolates a single variable, while $()
is an S-expression. Aside from that spark will also have sprintf, sprintf with named conversions similar to Python and something as similar as possible to Perl’s Template Toolkit (while still being Sparky). I find Common Lisp’s format
to be hard to understand and much less flexible than Template Toolkit so I’m going to drop it.
Like in Perl 5 the ^VARNAME
variables are reserved and are usually in all-capitals. Unlike Perl 5 (or Common Lisp), we are not a Lisp-2 and we use the same symbol namespace for everything (like Scheme). So we can put assign a lambda (could be (fun … )
, (sub … )
, (lambda …)
or (function … )
- all exact synonyms) to a variable and call it with a value:
(my square) (:= square (fun (x) (* x x))) ; Or (<- square) but I’m hazy about (= square) (say (square 5)) ; Prints 25 followed by a newline.
Like in Perl 5, however, a symbol table has an arbitrary amount of slots which we can put values. So we can say:
(say :to (fh STDERR) "Warning, Will Robinson.")
Which will print to STDERR.
In the case of the :to
named parameter to (say)
(or to (print)
or to (printf)
or whatever we have or define) we don’t need the explicit (fh …)
(= file handle) call but it won’t hurt.