< Back to IRCAM Forum

Recursion and lispfunction

Good afternoon,

I have a question in relation with recursion. Is it possible to use the lispfunction in order to call a function that uses a recursive process?
I ask that why… I have a lisp function in cl-user that every time I load the patch the function appears dead. I forget sometimes to compile the lisp file in the editor in advance…
The code:
(in-package :cl-user)

(defun noconseqrep (lista)
(cond
((null lista) nil)
((null (cdr lista)) lista)
((eql (first lista) (first (rest lista)))
(noconseqrep (rest lista)))
(t (cons (first lista) (noconseqrep (rest lista))))
)
)

It is not my code but it is useful to clean consecutive repetitive atoms in a list…

Thank you in advance!

Dimitris

Dear Dimitris,

Yes it is possible. However you need to use a trick!
let’s say your code in the lispfunction is named noconseqrep0:

Now this indeed will yield this error:

02

Since as you noticed it is a recursive function, and in the lispfunction box it is called from a “lambda” function. So there is no way the recursion inside the lispfunction box will know that ‘noconsseqrep0’ is a function.

What we will do is use the ‘label’ macro, and define this function inside the lambda like so:

This then will work.
In lisp code these functions are equivalent:

(defun noconseqrep (lista)
(cond
((null lista) nil)
((null (cdr lista)) lista)
((eql (first lista) (first (rest lista)))
(noconseqrep (rest lista)))
(t (cons (first lista) (noconseqrep (rest lista))))
)
)

(noconseqrep '(1 2 3 3 4 5 4 4 5 5 6 7 8))

(defun no-repet (list)
(labels ((noconseqrep (lista)
(cond
((null lista) nil)
((null (cdr lista)) lista)
((eql (first lista) (first (rest lista)))
(noconseqrep (rest lista)))
(t (cons (first lista) (noconseqrep (rest lista))))
)))
(noconseqrep list)))

(no-repet '(1 2 3 3 4 5 4 4 5 5 6 7 8))

Hope this answers your question.

here are the lisp files and non-working and working lispfunctions:

lispfunctions.zip (2.2 KB) noconseqrep.lisp (688 Bytes)

Best
K

Thank you a lot Karim!!
Always great to learn!!!

All the best!!

Dimitris