Skip to content

Latest commit

 

History

History
87 lines (65 loc) · 1.45 KB

elisp.org

File metadata and controls

87 lines (65 loc) · 1.45 KB

Elisp Code

NOTE: Comment your code, and verify it works for “breaking cases” i.e. 0! = 1.

Factorial Code

Code

(+ 2 3 4)
(defun factorial(x)
   (setq total 1)
   (while (> x 0)
     (setq total (* total x))
     (decf x))
   total)

(factorial 4)
(factorial 8)

Test cases

(factorial 0)

How big a number will make your computer freeze?

When the number gets over 60 something it causes wrap around. None of the numbers I have tried have caused my computer to freeze.

“Real-World” function with mapcar

What is mapcar

It goes through a supplied list and calls a function using each item of the list as an input.

Code

(setq cool-list '(2 5 22 15))
(defun square (x)
   (* x x))

(mapcar 'square cool-list)

Loop function

Code

(defun count-numbers (x) ; Will return the sum of all of the numbers from 0 to the number input.
   (setq total 0)
   (while (> x 0)
     (incf total x)
     (decf x))
   total)

(count-numbers 10)