Functions inside loops

Discussion of Common Lisp

Functions inside loops

Postby christian123 » Sat Nov 19, 2011 6:50 pm

Hello, I am writing a code to create a tree in lisp but I am facing the following problem

Here is my code
(defun create_tree (M N D)
(if (> N M)
nil
(loop for i from 1 to D for j from 1 to D
if (< (+ i j) (+ D 1))
(create_tree M N i j)
)
))

This gives a syntax error, however when I comment the "(create_tree M N i j)" line, this works. Please let me know what the problem is...

I believe the problem is calling a function inside a loop. More generally, is it possible to create a function inside a loop?

Thank you
BS
christian123
 
Posts: 2
Joined: Sat Nov 19, 2011 6:41 pm

Re: Functions inside loops

Postby edgar-rft » Sun Nov 20, 2011 1:32 am

The create_tree function is defined to take three arguments (M N D) but inside the loop it is called with four arguments (create_tree M N i j).

- edgar
edgar-rft
 
Posts: 157
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Functions inside loops

Postby christian123 » Sun Nov 20, 2011 8:11 am

Here is my code
(defun create_tree (M N D)
(if (> N M)
nil
(loop for i from 1 to D for j from 1 to D
if (< (+ i j) (+ D 1))
(create_subtree M N i j)
)
))
Sorry for the trouble. However, I found a way around it using dotimes inside of loop.
christian123
 
Posts: 2
Joined: Sat Nov 19, 2011 6:41 pm


Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 5 guests