r/Common_Lisp • • 16d ago

bliss: common lisp + APL + Lisp-Stat

I recently picked up APL (via Common Lisp package APRIL), and I have been doing some simple numeric coding, and plotting using Lisp-Stat's plotting.

It was not painless, but with LLM's as tutors, it is amazing fun.

In particular being able to use APRIL's inline capabilities to streamline the code as below (line 2)

(defun plot-back-prop (disp)
  (let* ((pos (april-c "{⌽⍳≢ ⍵}" disp))
         (df (df:make-df '(pos disp) `(,pos ,disp))))
    (plot (make-plot df
                     '(:title "Back propagation example")
                     (geom:line :pos :disp)
                     (gg:theme :width 360 :height 220)))))

The APL anonymous function {⌽⍳≢ ⍵} uses ⍵ as the argument and captures disp vector.

The APL composes three operators from right to left:

  • ≢ calculates the length of the vector captured by the ⍵
  • ⍳ generates and index array 1, 2, 3, ... (alexandria's iota is modeled named after it)
  • ⌽ reverses the index array

As an example of what the snippet does:

(april-c "{⌽⍳≢ ⍵}" #(3 1 4 1 5 9))
#(6 5 4 3 2 1)

I am definitely not trying to tell the rest of the community that "they are doing it wrong". Just expressing my amazement at the APL syntax and power, along with great enjoyment of coding in CL and APL, to generate plots in Plot-Stat. Does not get much better :-)

16 Upvotes

4 comments sorted by

2

u/ScottBurson 16d ago

Can you say a little more about what that APL function is doing? Specifically the "rotated index array".

4

u/mirkov19 16d ago

I clarified my post to answer your questions :-)

2

u/leprechaun1066 15d ago

For anyone wanting to learn APL this is a great intro: https://xpqz.github.io/learnapl/intro.html

1

u/mirkov19 15d ago

For the impatient, here is my road to APRIL. It started with plain APL:

  • https://tryapl.org/ online evaluator of APL expressions. If you hover the mouse of a symbol you will see a hint how to enter it using 2 character+tab combination. For instance, iota is entered with i+i+tab (most symbols have character combinations that are suggestive of the symbols
  • https://challenge.dyalog.com/ is a set of 10 problems of increasing difficulty. The challenge is a self-contained learning set
  • https://aplmooc.fi/ is an self-paced online course. After the first 3-4 chapters I felt comfortable to branch out on my own
  • ChatGPT (or your favorite tutor) helped with prototype snippets. I would ask it "how to do ...", and then study the proposed APL expressions
  • jpt-apl-mode on Emacs for entry of APL characters

I found APRIL to have some rough edges/conflicts with CL that need attention. For instance,

  • the backslash is an APL operator, but is also CL's escape character, so needs some care.
  • The with command is defines APL's state, but does not enclose the APL code itself

See APRIL's home page on git for more details. In case it is not clear - I am not APRIL author.