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

;a)
;sum of two vectors

(DEFUN sum(V)
    (COND
        ((NULL V) 0)
        (T (+ (CAR V) (sum (CDR V))))
    )
    
)

(DEFUN add(A B)
    (+ (sum A) (sum B))
)

(write(add '(1 2 3 4) '(1 1 1 1 1)))
(terpri)

;b)
;get the list of all atoms

;(((A B) C) (D E)) ==> (A B C D E)


(DEFUN fct(L)

    (COND

        ((NULL L) NIL)

        ((ATOM (CAR L)) (CONS (CAR L) (fct (CDR L))))

        ((LISTP (CAR L)) (APPEND (fct (CAR L)) (fct (CDR L))))

    )

)


(write(fct '(((A B) C) (D E) F) ))
(terpri)

;c)
(DEFUN inv(L C)

    (COND

        ((NULL L) C)

        ((ATOM (CAR L)) (SETQ C (CONS (CAR L) C)) (inv (CDR L) C))

        ((LISTP (CAR L)) (APPEND C (CONS (inv (CAR L) ()) (inv (CDR L) ()))))

    )

)



(WRITE(inv '(A B C (D (E F)) G H I) '()))
(terpri)

;d)
;max at superficial level
(DEFUN maxi(A B)

    (COND

        ((< A B) B)

        (T A)

    )

)



(DEFUN supmax(L)

    (COND

        ((NULL L) (SETQ M -1000))

        ((NUMBERP (CAR L)) (SETQ M (maxi (CAR L) (supmax (CDR L)))))

        (T (supmax (CDR L)))

    )

)

(write(supmax '(A 2 (6 B) 1 (5))))



















Advertisements
Loading...

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