; Flatten a list using a map function. ; (1 (2 (3) 2) 1) -> (1 2 3 2 1) ; flatten(l) = null, if l is null ; = (l), if l is atom ; = flatten(l1) U flatten(l2) U ... U flatten(ln), if l is a list and l = (l1..ln) (defun flatten (l) (cond ((null l) nil) ((atom l) (list l)) (T (mapcan #'flatten l)) ; -> (flatten (car l) U flatten (cadr l) U flatten (caddr l) .... ) ) ) (print (flatten '(1 (2 (3) 2) 1))) ; Triple all the numeric atoms from a list. ; (1 (a (3) 2) b) -> (3 (A (9) 6) B) ; triple(l) = null, if l is null ; = 3*l, if l is a numeric atom ; = l, if l is a non-numeric atom ; = triple(l1) U ... U triple(ln), if l is a list and l = (l1..ln) (defun triple (l) (cond ((null l) nil) ((numberp l) (* 3 l)) ((atom l) l) (T (mapcar #'triple l)) ) ) (print (triple '(1 ("a" (3) 2) b))) ; Count all the atoms from a list on the K-th level. Consider that the superficial level is level 0 ; (1 (2 (3 4)) (3 4)), K = 1 -> 3 ; countLevel(l, c, k) = null, if l is null ; = 1, if l is an atom and c == k ; = 0, if l is an atom and c != K ; = countLevel(l1, c+1, k) + ... + countLevel(ln, c+1, k), if l is a list and l = (l1..ln) (defun countLevel (l c k) (cond ((null l) nil) ((and (atom l) (= c k)) 1) ((atom l) 0) (T (apply #'+ (mapcar #'(lambda (x) (countLevel x (+ c 1) k)) l))) ) ) (defun countWrap (l k) (countLevel l -1 k) ) (print (countWrap '(1 (2 (3 4)) (3 4)) 1)) ; remove all numeric atoms divisible by 3 from all levels of a list ; rem3(l) = null, if l is null ; = null, if l is a numeric atom and l % 3 == 0 ; = l, if l is an atom ; = removeNil(rem3(l1) U ... U rem3(ln)), if l is a list and l = (l1..ln) (defun rem3 (l) (cond ((null l) nil) ((and (numberp l) (= (mod l 3) 0)) nil) ((atom l) l) (T (removeNil (mapcar #'rem3 l))) ) ) ; removeNil(l..ln) = null, if n == 0 ; = removeNil(l2..ln), l1 == null ; = l1 U removeNil(l2..ln), otherwise (defun removeNil (l) (cond ((null l) nil) ((null (car l)) (removeNil (cdr l))) (T (cons (car l) (removeNil (cdr l)))) ) ) (print (rem3 '(1 (2 (3) 2) 1))) ; print all the nodes on a given level k from a n-ary tree (type 2); consider that the root is at level 0 ; (a (b (g) (d (f)) (e)) (c (h)(i)(j)(k))) -> (G D E H I J K) ; printTreeLevel(tree1..treen, k) = null, if n == 0 ; = null, if k < 0 ; = (tree1), if k == 0 ; = printTreeLevel(tree2, k-1) U ... U printTreeLevel(treen, k-1), else (defun printTreeLevel (tree k) (cond ((null tree) nil) ((< k 0) nil) ((= k 0) (list (car tree))) (T (mapcan #'(lambda (x) (printTreeLevel x (- k 1))) (cdr tree))) ) ) (print (printTreeLevel '(a (b (g) (d (f)) (e)) (c (h)(i)(j)(k))) 2)) (setq 'p 10)
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