(defun print_matrix (matrix) (format t "~%(~a" (car matrix)) (mapcar (lambda (row) (format t "~% ~a" row)) (cdr matrix)) (format t ")") ) (defun random_matrix (rows columns) ;(mapcar #'random (make-list 10 :initial-element 101)) (mapcar (lambda (row) (mapcar #'random (make-list columns :initial-element (get-universal-time)))) (make-list rows :initial-element '())) ) (defun transpose (matrix) (apply #'mapcar #'list matrix) ) (defun dot (a b) (apply #'+ (mapcar #'* a b)) ) (defun matrix_product (A B) (if (= (length B) (length (car A))) (let ((BT (transpose B))) (mapcar (lambda (rowA) (mapcar (lambda (colB) (dot rowA colB) ) BT ) ) A ) ) (error "invalid matrix shapes") ) ) (defun matrix_sum (A B) (mapcar (lambda (rowA rowB) (mapcar #'+ rowA rowB)) A B) ) (print (transpose '((1 2) (3 4)))) (print (dot '(1 2) '(3 4))) (print_matrix (matrix_product '((1) (2) (4)) '((2 4 1)))) ;(print_matrix '()) ;(print (mapcar #'random (make-list 10 :initial-element 101))) ;(print_matrix (random_matrix 5 3)) ;_______________________________________________________ (defvar inputs '((1 2 3 2.5) (2 5 -1 2) (-1.5 2.7 3.3 -0.8))) (defvar weights '((0.2 0.8 -0.5 1.0) (0.5 -0.91 0.26 -0.5) (-0.26 -0.27 0.17 0.87))) (defvar biases '(2 3 0.5)) (print_matrix (matrix_sum (matrix_product inputs (transpose weights)) (mapcar #'list biases) ) ) (print_matrix (mapcar (lambda (row) (matrix_sum (list row) (list biases))) (transpose (matrix_product weights (transpose inputs) ) ) ) ) (defclass LayerDense () ( (_weights :accessor weights) (_biases :accessor biases) (_output :accessor output) ) ) (defmethod initialize-instance :after ((this LayerDense) &rest initargs &key n_neurons n_inputs) (setf (weights this) (random_matrix n_neurons n_inputs)) (setf (biases this) (list (make-list n_neurons :initial-element 0))) ) (defvar layer1 (make-instance 'LayerDense :n_neurons 4 :n_inputs 2)) (inspect layer1)
Write, Run & Share Common Lisp code online using OneCompiler's Common Lisp online compiler for free. It's one of the robust, feature-rich online compilers for Common Lisp language, running the latest Common Lisp version 5.3. Getting started with the OneCompiler's Common Lisp editor is easy and fast. The editor shows sample boilerplate code when you choose language as Common Lisp and start coding.
OneCompiler's Common Lisp online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Common Lisp program which takes name as input and prints hello message with your name.
(setq name (read))
(princ "Hello ")
(write name)
Common Lisp is a generic language suitable for a wide range of industry applications. It is often referred as Programmable programming language because of it's high extensibility, machine independence, extensive control structures, dynamic updation of programs etc.
Common LISP was invented by John McCarthy in 1958 and was first implemenyted by Steve Russell on an IBM 704 computer.
defvar
keyword and these variables will be in effect until a new value is assigned.(defvar x 10)
(write x)
let
and prog
are used to declare local variables.(let ((var1 value1) (var2 value2).. (varn valuen))<expressions>)
setq
(setq a 10)
This is the simplest looping mechanism in LISP. This allows the execute the set of statements repeatedly until a return statement is encountered.
(loop (s-expressions))
For loop is used to iterate a set of statements based on a condition.
(loop for loop-variable in <a list>
do (action)
)
Do is also used to iterate a set of statements and then check the condition
(do ((var1 val1 updated-val1)
(var2 val2 updated-val2)
(var3 val3 updated-val3)
...)
(test return-value)
(s-expressions)
)
Dotimes is used to iterate for fixed number of iterations.
(dotimes (n val)
statements