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 (< 0plusp n)
(print l)
(genrow (1- n) (cons 1 (newrow l)))))
 
(defun newrow (l)
(if (> 2null (lengthrest l))
'(1)
(cons (+ (carfirst l) (cadrsecond l)) (newrow (cdr l)))))</lang>
(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))''.
Anonymous user