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

(defun my-length(x)
    (cond ((null x) 0)
        (t (+ 1 (my-length(cdr x))))
    )
)
 
(print (my-length '(A B C)))

(defun my-memq(a x)
    (cond ((null x) nil)
        ((eq a (car x)) (cdr x))
        (t (my-memq(a (cdr x))))
    )
)

(print (my-memq 'a nil))

(defun my-append(l1 l2)
    (if (null l1)
        l2
        (cons (car l1) (my-append (cdr l1) l2))   
    )
)


(print (my-append '(a b c) '(d e f)))

(defun my-attach(o l)
    (push o (cdr (last l)))
    l
)

(print (my-attach '(d) '(a b c)))


(defun my-assoc(x l)
    (cond ((null l) nil)
        ((eq x (car(car l))) (car l))
        (t (my-assoc x (cdr l)))
    )
)

(print (my-assoc 'a '((a . b)(c e f)(b))))
(print (my-assoc 'c '((a . b)(c e f)(b))))

(defun freq(a l)
    (cond ((null l) 0)
        ((listp (car l))  (+ (freq a (car l)) (freq a (cdr l))))
        ((eq a (car l)) (+ 1 (freq a (cdr l))))
        (t (freq a (cdr l)))
    )
)

(print (freq  'c '((a c) c e)))

(defun mapping(l val)
    (cond ((null l) nil)
        ((> val (car(car l))) (cons (car (cdr (car l))) (mapping (cdr l) val)))
        (t (mapping (cdr l) val))
    )

)

(print (mapping '((35 kim) (67 clinton) (45 emma))  40)  )




(defun check-last(a l)
    (cond ((null l) nil)
        ((eq a (car l)) t)
        (t (check-last a (cdr l)))
    )
)

(defun my-last(a l)
    (cond ((null l) nil)
        ((and (eq a (car l)) (null (check-last a (cdr l)))) l)
        (t (my-last a (cdr l)))
    )
)

(print (my-last 'a '(a b c a b c a b c d e f g)) )


(defun my-reverse(l)
    (cond ((null l) nil)
        ((null (cdr l)) l)
        (t (my-attach (car l) (my-reverse (cdr l))))
        
    )
)

(print (my-reverse '((1 2 3) 4 ((5 6)))) )



(defun check-substr(l1 l2)
    (cond ((null l1) l2)
        ((eq (car l1) (car l2)) (check-substr (cdr l1) (cdr l2)))
        (t (check-substr l1 (cdr l2)))
    )
)


(defun is-pattern?(pat str)
    (cond ((null pat) nil)
        ((null str) nil)
        (t (my-append pat (check-substr pat str)))
    )
)
(print  (is-pattern? '(c a c) '(b a j a c a c t u s)))

(defun first-atom(l)
    (cond ((null l) nil)
        ((atom (car l)) (car l))
        (t (first-atom (car l)))
    )
)

(print (first-atom  '((((s)) o ))))

(print (last '(a b c)))

(defun get-last(a l)
    (eq a (car (last l)))
)

(defun get-next(a l)
    (cond ((null l) nil)
;    ((eq a (car l)) (cons (car (cdr l)) (get-next a (cdr l))))
    ((eq a (car l)) (car (cdr l)))
    (t (get-next a (cdr l)))
    )
)

(defun get-first(l)
    (cond ((listp (car l)) (get-first (car l)))
        (t (car l))
    )
)

(print (get-first '(((b a)) ((c a b)))))

(print (get-next 'a '(b a c a e)))

(defun append-three(l1 l2 a)
    (my-attach a l1)
    (my-append l1 l2)
)

(print (append-three '(a) '(b c) 'd))

(defun find-all-temp(a l)
    (cond ((null l) nil)
       ((and (listp (car l)) (get-last a (car l))) (append-three (find-all-temp a (car l)) (find-all-temp a (cdr l)) (first-atom (cdr l))))
       ((and (listp (car l)) (null (get-last a (car l)))) (cons (find-all-temp a (car l)) (find-all-temp a (cdr l))))
       ((eq a (car l)) (cons (get-next a l) (find-all-temp a (cdr l))))
       (t (find-all-temp a (cdr l)))
    )
)


(defun st (l)
  (cond ((null l) nil)
        ((atom l) (list l))
        (t (loop for a in l appending (st a))))
)

(defun find-all(a l)
    (st (find-all-temp a l))
)

(print (find-all 'a '((b a) c a e)))
(print (find-all 'a '((b a) ((c a b)))))

Advertisements
Loading...

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