Here are some source code for an example from the amazing book "Land of Lisp" :
(defun random-node ()
(1+ (random *node-num*)))
(defun edge-pair (a b)
(unless (eql a b)
(list (cons a b) (cons b a))))
(defun make-edge-list ()
(apply #'append (loop repeat *edge-num*
collect (edge-pair (random-node) (random-node)))))
Since I don't have the Lisp instinct , I find it is useful to break a method into multiple lines (as an imperative style) and then trying to morph it into the functional style.
Would you please help me to break make-edge-list function into multiple lines?
Lines are meaningless in Lisp. Lisp notation is based on s-expressions and the textual line is not seen by Lisp during evaluation. You can break up an expression in any way on whitespace.
You have to read the code like this:
Note that the function has several slight problems:
APPLY works not for lists of arbitrary lengths. So let's hope that the list is not too long.
More important, the apply of the function APPEND is not really needed if we change the LOOP slightly. LOOP can not only COLLECT, but also APPEND.