Re: help executing a list

From: David Wallis (dww_at_cpom.ucl.ac.uk)
Date: 10/19/04


Date: Tue, 19 Oct 2004 11:22:14 +0100


>

(Sorry for long post)

Thanks for the response, but I'm afraid I didn't really understand what
you mean. (This is my first lisp program)
Maybe you could expand a bit?

Also, despite reading all the apropriate bits in Paul Graham's 'Common
Lisp', I'm still stuck.The approach I was taking [based on Svein Ove Aas'
reply] was to pass a function to my argument f and call it in
(dotimes (i (array-total-size a))
   (setf (row-major-aref r i) f))

I have 2 questions.

1. I have a function that can return a list like (+ (row-major-aref (svref
v 0) i) 2 pi) , or just the argument. However the
function contains a loop. I can't figure out how to build the function
(closure?) in a loop, iterating over my arguments. Or, if I just return a
list, ((row-major-aref (svref v 0) i) 2 pi) ) and call (apply #'+ ...), it
tells me that (row-major-aref (svref v 0) i) isn't a number (it hasn't
evaluated the expression, it's just passed it to +). How do I build a
function iteratively? My
code to build the arguments is

(defun make-args (lst)
    (let ((v (list-to-vec lst)) ; list-to-vec could be (coerce lst
'array)
             (r Nil))
       (dotimes (i (length lst))
            (if (arrayp (svref v i))
                   (setf r (append1 r `(row-major-aref (svref v ,i)
i))) ; append1 is just (append lst (list obj))
                 (setf r (append1 r (svref v i))))) r ))

>(make-args '(#(1 2 3) 2))
((ROW-MAJOR-AREF (SVREF V 0) I) 2)

What I want is the function:

(lambda (v i) (+ ((ROW-MAJOR-AREF (SVREF V 0) I) 2))

2. Will this approach invoke the compiler in the loop? The arguments to my
array operator do not change in the loop.
e.g. if I call (.+ #(1 2 3) 2) [where .+ is my array addition operator),
my function would be
(+ (row-major-aref (svref v 0)) 2). So I hoped I could build the function
before going into the loop that iterates over the array #(1 2 3) and it
should be fast?

> The proper solution is to pass around a function and call it with
> the values to be bound.
>
> (defun f (...)
> ...)
>
> (defun g (fn ...)
> ...
> (funcall fn ...)
> ...)
>
> (g #'f ...) or (g #'(lambda (...) ...) ...) for anonymous function.
>



Relevant Pages

  • Re: Iterate through member variables of a class
    ... I did that because the OP was asking about iterating through member ... variables of a class by using a while loop. ... you can add or remove elements from the array and the ... So it would be better if I declared the int as a size_t instead? ...
    (comp.lang.cpp)
  • Re: Holding Returned Data in Arrays
    ... whether or not new items are available whilst you loop depends on the ... implementation of the thing you are iterating. ... In the case of Getfiles() you can see that it returns an array. ...
    (microsoft.public.dotnet.framework.clr)
  • RE: Problems Loading Large String Array into Array variable
    ... Splitfunction rather than iterating the array with a For..Next loop? ... "ExcelMonkey" wrote: ...
    (microsoft.public.excel.programming)
  • Re: missing values from files
    ... This will loop 14 times, ... If you want to loop over an array use the ... If you want to loop over a range you still should use the iterating ...
    (perl.beginners)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)