1

This is a generic example of a code I'am programming:

(defun test (a b) (loop for x in '(a b) collect ((setf a (+ a b)) (loop for y in '(a b) (setf a (+ a b)) (loop for z in '(a b) (setf a (+ a b)))) (list a b)))) 

When I call the function (test) in the REPL it gives me "illegal function call", as you can see below:

> (test 1 1) debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread #<THREAD "main thread" RUNNING {10005E85B3}>: Execution of a form compiled with errors. Form: ((SETF A (+ A B)) (LOOP FOR Y IN '(A B) (SETF A (+ A B)) (LOOP FOR Z IN '(A B) (SETF A (+ A B)))) (LIST A B)) Compile-time error: illegal function call Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL. restarts (invokable by number or by possibly-abbreviated name): 0: [ABORT] Exit debugger, returning to top level. (FRED5 #<unused argument> #<unused argument>) source: ((SETF A (+ A B)) (LOOP FOR Y IN '(A B) (SETF A (+ A B)) (LOOP FOR Z IN '(A B) (SETF A (+ A B)))) (LIST A B)) 0] 0 > 

It seems I'm doing something wrong with the first (loop for) parameters, but I can't find out what. Can anyone help with this, please? Thanks!

EDIT 1:

So, I've made the adjustments @Rainer Joswig and @Xero Smith suggested:

(defun test (a b) (loop for x in '(a b) collect (setf a (+ x 1)) (loop for y in '(a b) do (setf a (+ y 1)) (loop for z in '(a b) do (setf a (+ z 1)))) (list a b))) 

But now, I'm getting the following error:

> (test 1 1) debugger invoked on a SB-INT:COMPILED-PROGRAM-ERROR in thread #<THREAD "main thread" RUNNING {10005E85B3}>: Execution of a form compiled with errors. Form: (LOOP FOR X IN '(A B) COLLECT (SETF A (+ X 1)) (LOOP FOR Y IN '(A B) DO (SETF A (+ Y 1)) (LOOP FOR Z IN '(A B) DO (SETF A (+ Z 1)))) (LIST A B)) Compile-time error: during macroexpansion of (LOOP FOR X ...). Use *BREAK-ON-SIGNALS* to intercept. (LOOP FOR Y IN '(A B) DO (SETF A (+ Y 1)) (LOOP FOR Z IN '(A B) DO (SETF A (+ Z 1)))) found where a LOOP keyword or LOOP type keyword expected current LOOP context: COLLECT (SETF A (+ X 1)) (LOOP FOR Y IN '(A B) DO (SETF A (+ Y 1)) (LOOP FOR Z IN '(A B) DO (SETF A (+ Z 1)))). Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL. 
0

3 Answers 3

4
(defun test (a b) (loop for x in '(a b) collect ; X unused ((setf a (+ a b)) ; error: not a function, too many parentheses (loop for y in '(a b) ; Y unused (setf a (+ a b)) ; error: no DO (loop for z in '(a b) ; z unused (setf a (+ a b)))) ; error: no DO (list a b)))) 
Sign up to request clarification or add additional context in comments.

Comments

3

Your fix is is still wrong. collect takes a single form as its argument. You now have collect (setf ...) (loop ...).

On the other hand, the do clause does take multiple forms.

Perhaps this function is what you're looking for, or close:

(defun test (a b) (loop for x in '(a b) do (setf a (+ a b)) (loop for y in '(a b) do (setf a (+ a b)) (loop for z in '(a b) do (setf a (+ a b)))) collect (list a b)))) 

If you want to combine the outer setf, for y loop and (list a b) into a single collect clause, then progn is required:

(defun test (a b) (loop for x in '(a b) collect (progn (setf a (+ a b)) (loop for y in '(a b) do (setf a (+ a b)) (loop for z in '(a b) do (setf a (+ a b)))) (list a b)))) 

Comments

2

This is your problem: ((setf a (+ a b)) replace it with (setf a (+ a b)). This is an error because the expression ((setf a (+ a b)) is not a function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.