Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
homework
;;; Provides functions for addition, finding minimum element in ;;; list, averaging all elements in list, counting instances ;;; of symbol in list, iterative- and recursive-factorial ;;; calculations, finding nth number in Fibonacci sequence, ;;; trimming list, and Ackermann function calculations. ;;; Completion time: 3 Hours ;;; ;;; Josh Pattillo, Dr. Ajay Bansal ;;; 1.0 ; returns the sum of two numbers number-one and number-two (defun add (number-one number-two) (+ number-one number-two)) ; returns the minimum value in the list (defun minimum (list) (cond ((null list) nil) ((null (rest list)) (first list)) ((< (first list) (second list)) (minimum (cons (first list) (rest (rest list))))) (t (minimum (rest list))) ) ) ; average of numbers in a list (defun average (list) (when list (/ (reduce #'+ list) (length list))) ) ; returns the number of instances the given symbol in the given list (defun count-of (symbol list) (if (null list) 0 (if (equalp (first list) symbol) (1+ (count-of symbol (rest list))) (count-of symbol (rest list)) ) ) ) ; returns factorial calculated iteratively (defun iterative-factorial (number) (let ((sum 1)) (dotimes (i number) (setf sum (* sum (+ i 1)))) sum ) ) ; returns factorial calculated recursively (defun recursive-factorial (number) (cond ((<= number 1) 1) (t (* number (recursive-factorial (1- number))))) ) ; returns the nth number in the Fibonacci sequence (defun fibonacci (number) (cond ((equalp number 0) 0) ((equalp number 1) 1) (t (+ (fibonacci (1- number)) (fibonacci (- number 2)))) ) ) ; returns new list starting from first occurrence of input symbol in list (defun trim-to (symbol list) (cond ((null list) nil) ((equalp symbol (first list)) list) (t (trim-to symbol (rest list))) ) ) ; returns results of Ackermann function (defun ackermann (number-one number-two) (cond ((equalp number-one 0) (+ number-two 1)) (t (cond ((equalp number-two 0) (ackermann (- number-one 1) 1)) (t (ackermann (- number-one 1) (ackermann number-one (- number-two 1))))) ) ) ) ; instructor-provided driver (defun test() (print (add 3 1)) (print (average '(1 2 3 4 5 6 7 8 9))) (print (minimum '(5 78 9 8 3))) (print (count-of 'a '(a '(a c) d c a))) (print (iterative-factorial 5)) (print (recursive-factorial 4)) (print (fibonacci 6)) (print (trim-to 'c '(a b c d e))) (print (ackermann 1 1))) (test)
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
#1MIN-2
syntax keyword in defmacro
Project Euler Problem 491
[LISP] Recursive process - Ackermann function
title2
(defun (ins ‘( 2 5 6) ‘4)
L1.9b
Sum_of_evens
18rec2
5
Please log in to post a comment.