Pascal's triangle: Difference between revisions

Content added Content deleted
(add BQN)
m (Emacs Lisp: Improve formatting, use cl-lib)
Line 2,027: Line 2,027:
=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
===Using mapcar and append, returing a list of rows===
===Using mapcar and append, returing a list of rows===
<lang lisp>(defun next-row (row)
<lang lisp>(require 'cl-lib)

(mapcar* #'+ (cons 0 row)
(append row '(0))))
(defun next-row (row)
(cl-mapcar #'+ (cons 0 row)
(append row '(0))))


(defun triangle (row rows)
(defun triangle (row rows)
(if (= rows 0)
(if (= rows 0)
'()
'()
(cons row (triangle (next-row row) (- rows 1)))))
(cons row (triangle (next-row row) (- rows 1)))))</lang>
</lang>


{{Out}}
{{Out}}
Line 2,056: Line 2,057:
(setq c (/ (* c (- i k))
(setq c (/ (* c (- i k))
(+ k 1))))
(+ k 1))))
(terpri))))
(terpri))))</lang>
</lang>
{{Out}}
{{Out}}
From the REPL:
From the REPL:
Line 2,080: Line 2,080:
(+ k 1))))
(+ k 1))))
(setq out (concat out "\n"))))
(setq out (concat out "\n"))))
out))
out))</lang>
</lang>
{{Out}}
{{Out}}
Now, since this one returns a string, it is possible to insert the result in the current buffer:
Now, since this one returns a string, it is possible to insert the result in the current buffer: