Source Code 211

write an unnecessary program in lisp of at least 25 lines 

This program is written in LISP and contains a number of functions commonly used in programming, such as computation of factorial, Fibonacci sequence, finding the largest value in a list, determining whether a number is prime, inverting a string, concatenating strings, counting items in a list, adding items in a list, determining whether a number is even or odd, and finding the smallest value in a list.

However, these functions are already built into LISP, so this implementation is unnecessary. It's just an example of how these functions can be written in LISP.

(defun factorial (n) (if (eq n 0) 1 (* n (factorial (1- n))))) (defun fibonacci (n) (if (< n 2) n (+ (fibonacci (- n 1 )) (fibonacci (- n 2))))) (defun find-largest (lst) (if (null (cdr lst))) (car lst) (let ((x (car lst))) (y (find-largest (cdr lst)))) (if (> xy) xy)))) (defun is-prime (n) (if (= n 2) t (if (or (= n 1) (= n 0)) nil (let ((bound (sqrt n))) (do ((i 2 (1+ i))) ((> i bound) t) (if (= (mod ni) 0) (return nil))))) )) (defun reverse-string (s) (if (string= s "") "" (concat (reverse-string (substring s 1)) (substring s 0 1)))) (defun concatenate-strings (lst) (if (null lst) "" (concat (car lst) (concatenate-strings (cdr lst))))) (defun count-list (lst) (if (null lst) 0 (1+ (count-list (cdr lst))))) (defun sum-list (lst) (if (null lst) 0 (+ (car lst) (sum-list (cdr lst))))) (defun is-even (n) (= ( mod n 2) 0)) (defun is-odd (n) (not (is-even n))) (defun find-smallest (lst) (if (null (cdr lst))) (car lst) (let (( x (car lst)) (y (find-smallest (cdr lst)))) (if (< xy) xy))))

This program contains many functions already built into LISP, such as reverse-string, concatenate-strings, count-list, sum-list, is-even, is-odd, find-smallest, find-largest, is-prime, fibonacci , factorial, which are not necessary, and can be called directly.

coding

Leave a Reply

Proudly powered by WordPress

Up ↑

en_USEnglish

Discover more from Mijn NiemandsLand

Subscribe now to keep reading and get access to the full archive.

Continue reading