diff options
author | Daniel Santos <dacs.git@brilhante.top> | 2023-01-10 21:31:37 +0000 |
---|---|---|
committer | Daniel Santos <dacs.git@brilhante.top> | 2023-01-10 21:37:26 +0000 |
commit | a0025c8f5fb8a1ca5486bf6e5c5f7b8270219bfb (patch) | |
tree | b408974be4c1463e172987888137e21919389a54 | |
parent | 91fcf477721c1571f20b25de633e5cf8314bd81e (diff) | |
download | cl-math-a0025c8f5fb8a1ca5486bf6e5c5f7b8270219bfb.tar.gz |
cleanup, add symbol-to-number, plus-* correct number system
Clean up various variable names, namely the ones with '*reverse*'. Add the function symbol-to-number. Make the functions plus-decimal and plus-earth with numeral system with symbols, instead of numbers. This is the correct way for this program.
-rw-r--r-- | cl-math.lisp | 82 |
1 files changed, 27 insertions, 55 deletions
diff --git a/cl-math.lisp b/cl-math.lisp index 86c7a81..481eeed 100644 --- a/cl-math.lisp +++ b/cl-math.lisp @@ -1,5 +1,4 @@ ;;; plus one function -;;; it is the (next-number) and the (next-number-reversed) (defvar *number-units-beginning* nil) @@ -10,12 +9,6 @@ ((not (eql (car numerals) digit)) (next-digit (cdr numerals) digit)) (t (cadr numerals)))) -(defun split-number-reversed (number) - "Given a number, it returns a list of the number in reverse (because the decimal number system a reversed number system)." - (cond - ((zerop number) nil) - (t (cons (mod number 10) (split-number-reversed (truncate number 10)))))) - (defun may-reverse (number) "If units is placed on the end, reverse; else keep it." (if *number-units-beginning* @@ -30,81 +23,60 @@ "Given a list of a number (which can have letters), it returns the number" (intern (apply #'concatenate 'string (mapcar #'symbol-name (may-reverse number))))) -(defun next-list-number (numerals number-list-reversed) +(defun next-list-number (numerals number-list) "Given a list of a numeral system and a list of a number, it returns a list of the next number." (cond - ((null number-list-reversed) - (cons (if (zerop (car numerals)) + ((null number-list) + (cons (if (eq (car numerals) '|0|) (cadr numerals) (car numerals)) nil)) - ((eql (car number-list-reversed) (car (last numerals))) - (cons (car numerals) (next-list-number numerals (cdr number-list-reversed)))) - (t (cons (next-digit numerals (car number-list-reversed)) (cdr number-list-reversed))))) - -(defun next-number (numerals number) - "Given a list of a numeral system and a number, it returns the list of the next number." - (reverse (next-list-number numerals (split-number-reversed number)))) - -(defun next-number-reversed (numerals number) - "Given a list of a numeral system and a number, it returns the reversed list of the next number (returns list, because the interpreter cuts zero's on the left)." - (next-list-number numerals (reverse (split-number-reversed number)))) + ((eql (car number-list) (car (last numerals))) + (cons (car numerals) (next-list-number numerals (cdr number-list)))) + (t (cons (next-digit numerals (car number-list)) (cdr number-list))))) (defun previous-digit (numerals digit) "Given a list of a numeral system and a digit, it returns the previous digit." (next-digit (reverse numerals) digit)) -(defun previous-list-number (numerals number-list-reversed) +(defun previous-list-number (numerals number-list) "Given a list of a numeral system and a list of a number, it returns a list of the previous number." (cond ((and - (null (cdr number-list-reversed)) - (eql (car number-list-reversed) - (if (zerop (car numerals)) + (null (cdr number-list)) + (eql (car number-list) + (if (eql (car numerals) '|0|) (cadr numerals) (car numerals)))) nil) ((and - (eql (car number-list-reversed) (car numerals)) - (not (null (cdr number-list-reversed)))) - (cons (previous-digit numerals (car number-list-reversed)) (previous-list-number numerals (cdr number-list-reversed)))) - (t (cons (previous-digit numerals (car number-list-reversed)) (cdr number-list-reversed))))) - -(defun previous-number (numerals number) - "Given a list of a numeral system and a number, it returns the list of the previous number." - (reverse (previous-list-number numerals (split-number-reversed number)))) + (eql (car number-list) (car numerals)) + (not (null (cdr number-list)))) + (cons (previous-digit numerals (car number-list)) (previous-list-number numerals (cdr number-list)))) + (t (cons (previous-digit numerals (car number-list)) (cdr number-list))))) -(defun plus-args-list (numerals a-reversed b-reversed) - "Given a list of a numeral system and two lists of numbers, it returns the list of it's sum." +(defun plus-args-list (numerals a b) + "Given a list of a numeral system and 2 lists of numbers, it returns the list of it's sum." (cond ((and - (null (cdr b-reversed)) - (eql (car b-reversed) (if (zerop (car numerals)) + (null (cdr b)) + (eql (car b) (if (eql (car numerals) '|0|) (cadr numerals) (car numerals)))) - (next-list-number numerals a-reversed)) - (t (plus-args-list numerals (next-list-number numerals a-reversed) (previous-list-number numerals b-reversed))))) + (next-list-number numerals a)) + (t (plus-args-list numerals (next-list-number numerals a) (previous-list-number numerals b))))) (defun plus (numerals a b) - "Given a list of a numeral system and two numbers, it returns the list of it's sum." - (reverse (plus-args-list numerals (split-number-reversed a) (split-number-reversed b)))) - -;; (defun next-number (numerals number) -;; "Given a list of a numeral system and a number, it returns the next number." -;; (reduce (lambda (a b) (+ (* b 10) a)) -;; (next-list-number numerals (split-number-reversed number)) -;; :initial-value 0 -;; :from-end t)) + "Given a list of a numeral system and 2 numbers, it returns the list of it's sum." + (may-reverse (plus-args-list numerals (may-reverse (explode a)) (may-reverse (explode b))))) -(defun plus-number (numerals number1 number2) - "Given a list of a numeral system and 2 numbers, it returns it's sum." - (reduce (lambda (a b) (+ (* a 10) b)) - (plus numerals number1 number2) - :initial-value 0)) +(defun symbol-to-number (symbol) + "Given a symbol, returns it's number." + (values (parse-integer (symbol-name symbol)))) (defun plus-decimal (&rest args) "Given decimal numbers, it returns it's sum." - (reduce (lambda (a b) (plus-number '(0 1 2 3 4 5 6 7 8 9) a b)) args)) + (values (implode (reduce (lambda (a b) (plus '(|0| |1| |2| |3| |4| |5| |6| |7| |8| |9|) a b)) args)))) (defun plus-earth (&rest args) "Give earthal numbers, it returns it's sum." - (reduce (lambda (a b) (plus-number '(1 2 3 4 5) a b)) args)) + (values (implode (reduce (lambda (a b) (plus '(|1| |2| |3| |4| |5|) a b)) args)))) |