Lisp ![]() |
similar languages: | Scheme | |
Hello World | Michael Neumann |
(print "Hello World")
|
Prints "Hello World" onto the
screen. |
Factorial (iterative) | Common Lisp | Marco Baringer |
(defun fact (n) (loop for i from 2 to n for fact = 1 then (* fact i) finally (return fact))) (format t "Factorial of 6 is: ~A~%" (fact 6)) |
Calculates the factorial. Results
720 . |
Factorial (recursive) | Common Lisp | Marco Baringer |
(defun fact (n) (if (zerop n) 1 (* n (fact (1- n))))) (format t "Factorial of 6 is: ~A~%" (fact 6)) |
Calculates the factorial. Results
720 . |
Hello World | Common Lisp | Marco Baringer |
(write-line "Hello World!") |
Prints "Hello World" onto the
screen. |
OO - Shape, Circle, Rectangle | Common Lisp | Marco Baringer |
;; define the various classes (defclass shape () ((pos-x :type real :accessor pos-x) (pos-y :type real :accessor pos-y)) (:documentation "A generic shape in a 2 dimensial plane")) (defclass rectangle (shape) ((width :type real :accessor width) (height :type real :accessor height)) (:documentation "A rectangle")) (defclass cirle (shape) ((radius :type real :accessor x-radius)) (:documentation "An cirlce")) ;; define the methods (defmethod move-to ((thing shape) new-x new-y) "Move the shape THING to the new position (new-x, new-y)" (setf (pos-x thing) new-x (pos-y thing) new-y)) (defmethod shift-to ((thing shape) delta-x delta-y) "Move the shape THING by (delta-x, delta-y)" (incf (pos-x thing) delta-x) (incf (pos-y thing) delta-y)) (defmethod scale ((rec rectangle) factor) "Scale the rectangle REC by factor" (with-slots (width height) rec (setf width (* factor width) height (* factor height)))) (defmethod scale ((cir circle) factor) "Scale the circle CIR by factor" (setf (radius cir) (* (radius cir) factor))) |
Squares (1) | Common Lisp | Friedrich Dominicus |
(defun print-squares (upto) ( loop for i from 1 to upto do (format t "~A^2 = ~A~%" i (* i i)))) |
Outputs the squares from 1 to
10. |
Squares (2) | Common Lisp | Marco Baringer |
(dotimes (i 10) (format t "~D " (expt i 2))) |
Outputs the squares from 1 to
10. |
Squares (3) | Common Lisp | Marco Baringer |
(loop for i from 1 upto 10 for i^2 = (expt i 2) do (format t "~D " i^2)) |
Outputs the squares from 1 to
10. |
Squares | Emacs Lisp | Michael Neumann |
(let ( (a 1) ) (while (<= a 10) (princ (* a a)) (princ " ") (setq a (+ a 1)) ) ) |
Outputs the squares from 1 to
10. |