Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute LISP Online

;1
(defun my-fact (m)
    (cond ((= m 0) 1)
        (t (* m (my-fact (- m 1))))
        )
    )
(print (my-fact 5))

;2
(defun fibonacci(n)
  (cond ((< n 2) 1)
        (t (+ (fibonacci (- n 1)) (fibonacci (- n 2))))))
(print (fibonacci 10))

3;
(defun my-member (el myL)
    (cond ((null myL) nil)
        ((equal (car myL) el) t)
        (t (my-member el (cdr myL)))
        )
    )
(print (my-member 2 '(1 2 3 4 5 )))

;4
(defun trim-head (l n)
    (cond ((= n 0) l)
        (t (trim-head (cdr l) (- n 1 )))
        )
    )
(print (trim-head '(1 2 3 4) 2))

;5
(defun trim-tail (l n)
    (if (< (length l) n) 'ListTooSmall
    (cond ((= n 0) (reverse l))
        (t (trim-tail (cdr l) (- n 1 )))
        )
    )
    )
(defun funct (l n)
    (trim-tail (reverse l) n)
    )
(print (funct '(1 2 3 4 5 6) 3))

;6
(defun count-atoms (l)
    (cond ((null l) 0)
        ((atom l) 1)
        ((listp l) (+ (count-atoms (car l)) (count-atoms (cdr l))))
        )
)
(print (count-atoms '(1 '() 2 (3 4 5))))

;7
(defun my-inc (a b)
    (cond ((= b 0) a)
        (t (my-inc (+ a 1) (- b 1)))
        )
    )
(print (my-inc 1 3))

;8
(defun my-rev (l1 l2)
    (cond ((null l1) l2)
        (t (my-rev (cdr l1) (append (list (car l1)) l2)))
        )
    )
(print (my-rev '(1 2 3 4) '()))

;9
(defun my-member (el myL)
    (cond ((null myL) nil)
        ((atom myL) (equal myL el))
        (t (OR (my-member el (car myL)) (my-member el (cdr myL))))
        )
    )
(print (my-member 2 '(1 (2 3) 3 4 5 )))

;10
(defun squash (l) 
    (cond ((null l) nil) 
        ((atom l) (list l)) 
        (t (append (squash (car l)) (squash (cdr l))))
        )
    )
(print (squash '(1 2 (3 (4 5) 6 (7 8)) 9 10 )))

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.