lispnet - LISP- OUTPUT:-(2) (2 3) (2 3) PROGRAM TO FIND THE...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
MAYANK GUPTA LISP- PROGRAM TO FIND THE FACTORIAL OF A NUMBER (defun factorial (n) (cond ((zerop n) (setq x 1)) (T (setq r1 (- n 1)) (setq x (* n (factorial r1))) ) ) (print x) ) (factorial 5) OUTPUT:- 1 1 2 6 24 120 PROGRAM TO APPEND A LIST (defun append1 (l1 l2) (cond ((null l1) (setq x l2)) (t (setq (cons (car l1) (append1 (cdr l1) l2)))) ) (print x) (append1 ‘(1 2) ‘(3 4)) OUTPUT:- (3 4) (2 3 4) (1 2 3 4) PROGRAM TO FIND SUM OF SQUARES (defun sumsq (x) (cond ((null x) nil) ((null (tail x)) (* (head x) (head x)) (t (+ (* (head x) (head x)) (sumsq (tail x))) (sumsq (1 2 +2 2 +3 2 )) OUTPUT:- 14 14 14 14 PROGRAM TO FIND SET INTERSECTION (defun setmembp(e1 list1) (cond ((null list1) (setq x nil)) ((eq e1 (car list1)) (setq x t)) (T (setmembp e1 (cdr list1))) )) (defun inter1 (list1 list2) (cond ((null list1) (setq x nil)) ((null list2) (setq x nil)) ((setmembp (car list1) list2) (setq x (cons (car list1) list2))) ) (T (inter1 (cdr list1) list2))) (print X)) (inter1 ‘(1 2 3) ‘(2 3 5)) K2CS042
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
MAYANK GUPTA
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: LISP- OUTPUT:-(2) (2 3) (2 3) PROGRAM TO FIND THE SET UNION (defun setmembp(e1 list1) (cond ((null list1) (setq x nil)) ((eq e1 (car list1)) (setq x t)) (T (setmembp e1 (cdr list1))) )) (defun union1 (list1 list2) (cond ((null list1) (setq x list2)) ((null list2) (setq x list1)) ((setmembp (car list1) list2) (union1 (cdr list1) list2)) (t (setq x (cons (car list1) (union1 (cdr list1 ) list2 ))))) (print x) ) (union1 (1 2 3) (2 3 5)) OUTPUT:-(2 3 5) (2 3 5) (2 3 5) (1 2 3 5) WHETHER THE GIVEN SET IS A SUBSET OR NOT (defun setmembp(e1 list1) (cond ((null list1) (setq x nil)) ((eq e1 (car list1)) (setq x t)) (T (setmembp e1 (cdr list1))) )) (defun sub1 (list1 list2) (cond ((null list1) (setq x t)) ((setmembp (car list1) list2) (sub1 (cdr list1) list2)) (t (setq x nil))) (print x) ) (sub1 (1 2 3) (2 3 5)) (sub1 (2 3) (2 3 5)) OUTPUT:-nil t K2CS042...
View Full Document

Page1 / 2

lispnet - LISP- OUTPUT:-(2) (2 3) (2 3) PROGRAM TO FIND THE...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online