开发者

Breaking a function into multiple lines

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)
  (开发者_Go百科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.

(defun make-edge-list ()
   (apply #'append
          (loop repeat *edge-num*
                collect (edge-pair (random-node)
                                    (random-node)))))

You have to read the code like this:

  • each edge pair is a list of conses. Each cons stores a position.
  • the loop returns a list of edge pairs.
  • Then append is applied to this list and returns a list of conses. The list of conses is really a list of positions.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜