I want to group the elements in an arbitrary long list into pairs and implemented the following so far:
(defun group-pairwise (numbers) "Returns a list with the elements of NUMBERS grouped in pairs." (cond ((null numbers) nil) ((null (rest numbers)) nil) (t (append (list (list (first numbers) (second numbers))) (group-pairwise (rest numbers))))))
It works, i.e.
> (group-pairwise '(3 6 0)) ((3 6) (6 0))
but I found
(append (list (list (first numbers) (second numbers) a bit cumbersome. Is there any way to express it more elegant?
Furthermore, I want in my later code not only to return the list of pairs but to perform an action on each pair. To keep the recursion pattern unchanged I was thinking about using
progn instead of append , e.g.
(defun generate-graph-of-digits%%% (numbers &optional (graph *pe79-graph*)) "Returns a graph structure of NUMBERS." (cond ((null numbers) nil) ((null (rest numbers)) nil) (t (progn (add-edge-between-vertexes graph (first numbers) (second numbers)) (generate-graph-of-digits%%% (rest numbers))))))
add-edge-between-vertexes being defined in the
I am unsure about the usage of
progn. Is it recommended to use it here?