phantomics / april

The APL programming language (a subset thereof) compiling to Common Lisp.
Apache License 2.0
602 stars 31 forks source link

+/⍳ 2 5 thows an error #240

Closed faeredia closed 1 year ago

faeredia commented 2 years ago

The +/⍳ optimisation (iota-sum) doesn't handle array arguments, like 2 5 gives an error:

Value of #:G16678 in (< #:G16678 10000000) is #(2 5), not a REAL.
   [Condition of type SIMPLE-TYPE-ERROR]

The offending < is in spec.lisp, match-function-patterns in the first branch

Dyalog says:

      +/ ⍳2 5
 5 15  10 15
faeredia commented 2 years ago

Something like this seems to work

(defun iota-sum-array (array index-origin)
  (let* ((output (make-array (butlast (april::array-to-list array))))
     (last (aref array (1- (length array))))
     (last-sum (iota-sum last)))
    (across
     output
     (lambda (elm coords)
       (declare (ignore elm))
       (setf (apply #'aref output coords)
         (concatenate 'vector
              (map 'vector (lambda (x)
                     (* last (+ x index-origin)))
                   coords)
              (vector last-sum)))))
    output))

I'll test a bit and include in #239

phantomics commented 1 year ago

This now works.