I’m a beginner in lisp and I need somebody to explain to me how the prog form works, step by step. What is the initial value of l1 ? Nil ?
The problem outputs T if the list has an even number of elements on the first level, nil if not.
(defun nr_par (l)
(prog ((l1 l))
ciclu
(cond
((null l1) (return T))
((null (cdr l1)) (return NIL))
((null (cddr l1)) (return T))
(T (setf l1 (cddr l1))
(go ciclu)))))
On console:
(nr_par '(1 2 3 4 5 6 7 8))
T
The program is straightforward, but not very idiomatic lisp (it is rather imperative instead of functional). Step by step goes as follows.
proguses a series of variable bindings, in this case,l1is assigned the value oflinitially. Then, a series of statements in which a loop starts (again, not very lisp idiomatic).This type of loops use a tag (
ciclu) and a goto instruction (go), again, not recommended, but it is there. After that, thecondchecks a series of cases. When the list is empty (null), you return true, in other cases, you check if the length is even or odd, and return the value in consequence.In the case that the list is longer than one or two elements (neither of the cases is null), the
l1list is adjusted to point to the next of the next element of itself (thecddrfunction).Finally, the
gofunction turns the program back to theciclutag.The program will finish when any of the
condclauses is met, returning eitherTorNIL.