Anonymous user
Pascal's triangle: Difference between revisions
→{{header|Common Lisp}}: improve the second version: naming, don't compute the length of a row
(→{{header|Common Lisp}}: improve the last version to be a bit more idiomatic, really print n lines and not n+1) |
(→{{header|Common Lisp}}: improve the second version: naming, don't compute the length of a row) |
||
Line 1,515:
(defun genrow (n l)
(when (
(print l)
(genrow (1- n) (cons 1 (newrow l)))))
(defun newrow (l)
(if (
'(1)
(cons (+ (
(newrow (rest l)))))</lang>
An iterative solution with ''loop'', using ''nconc'' instead of ''collect'' to keep track of the last ''cons''. Otherwise, it would be necessary to traverse the list to do a ''(rplacd (last a) (list 1))''.
|