Anonymous user
Pascal's triangle: Difference between revisions
→{{header|Common Lisp}}: improve the last version to be a bit more idiomatic, really print n lines and not n+1
(→{{header|TypeScript}}: Added.) |
(→{{header|Common Lisp}}: improve the last version to be a bit more idiomatic, really print n lines and not n+1) |
||
Line 1,512:
<lang lisp>(defun pascal (n)
(defun genrow (n l)
(defun newrow (l)
'(1)
(cons (+ (car l) (cadr l)) (newrow (cdr l)))))</lang>
Line 1,527:
<lang lisp>(defun pascal-next-row (a)
(defun pascal (n)
Another iterative solution, this time using pretty-printing to automatically print the triangle in the shape of a triangle in the terminal. The print-pascal-
<lang lisp>
(defun next-pascal-triangle-row (
`(1
1))
(defun pascal-
(loop repeat number-of-rows
(defun print-pascal-triangle (number-of-rows)
(format t (format nil "~~{~~~D:@<~~{~~A ~~}~~>~~%~~}" len) pasc)))▼
(let* ((triangle (pascal-triangle number-of-rows))
(max-row-length (length (write-to-string (first (last triangle))))))
(format t
triangle)))
</lang>
For example:
<lang lisp>(print-pascal-
<lang>
</lang>
<lang lisp>(print-pascal-
<lang>
</lang>
|