0

I have created a function that takes an arbitrarily long list of numbers as an argument. From this list, I wish to create matrices locally using let. The matrices will have rows and columns based on the number sequence inside the list. For example,

>(foo '(2 2 3) arg2 ... argn)

should locally create

M1: #2a((0 0)(0 0))     M2: #2a((0 0 0)
                                (0 0 0))  

that is, the first matrix takes its dimensions from the first two elements adjacent elements in the list, etc.

My question is: Is there a way to iteratively create the arbitrarily long set of local variables Mn in a let expression? Or is this a case where I should use a macro?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
category
  • 2,113
  • 2
  • 22
  • 46

2 Answers2

1

My question is: Is there a way to iteratively create the arbitrarily long set of local variables Mn in a let expression? Or is this a case where I should use a macro?

No, you can't generate the variables of an existing LET expression at runtime.

Yes, you can use a macro for generating a LET expression with "arbitrary" amounts of variables. But only if you know the list of numbers during macro expansion time (for example when you compile the code).

To make sense out of your question you need to show us example code, how you would want to use generated variables?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
1

I actually managed to solve the problem by holding the matrices in a vector:

(defun sum-net (structure)
;make vectoor to hold matrices
  (let ((matrices (make-array `(,(length structure)))))
;fill structures with empty matrices->weights & vectors->Net-Layers
    (dotimes (n (1- (length matrices)))
      (setf (svref matrices n) 
            (make-array `(,(nth n structure)
                          ,(nth (+ n 1) structure))))

Thanks for the suggestions. P.S. Sorry about the delay, I have the flu.

category
  • 2,113
  • 2
  • 22
  • 46