LISP


LISP Programs

Q. Write a LISP program to compute the area of a circle. (Dec. 01)

Ans.

(defun carea()

(terpri)
(princ "Please enter the radius: ")
(setq radius (read))
(princ "The area of circle is: ")
(princ (* 3.1416 radius radius))
(terpri))

Q. Write a LISP program that reads the radius of a circle and finds out the circumference of the circle and prints a suitable message. (June 02, June 03)

Ans. NOTE: circumference = 2p r

(defun cperi()

(terpri)
(princ "Please enter the radius: ")
(setq radius (read))
(princ "The circumference of circle is: ")
(princ (* 2 3.1416 radius))
(terpri))

Q. Write a LISP program that calculates the cubed value of a list of numbers.

Ans.

(defun list-cube(lst)
(mapcar # '(lambda (x) (* x x x)) lst))

Q. Write a LISP function that reads a natural number n and computes and returns n!, the factorial of n. (June 00)

Ans.

(defun fact()

(terpri)
(princ "Please enter the number: ")
(setq n (read))
(do ((count n (- count 1))
(product n (* product (-count 1))
((equal 0 count) product)))

Q. Define a LISP function FACT-SUM-SQ that reads two integers and returns the factorial of the sum of their squares. (Dec. 00)

Ans.

(defun FACT-SUM-SQ()

(terpri)
(princ "Please enter the first number: ")
(setq n1 (read))
(setq n1 (* n1 n1))
(princ "Please enter the second number: ")
(setq n2 (read))
(setq n2 (* n2 n2))
(setq sum1 (+ n1 n2))
(do ((count sum1 (- count 1))
(product sum1 (* product (-count 1))
((equal 0 count) product)))

Q. Write a LISP function which returns the maximum of three given numbers. (Dec. 00)

Ans.

(defun max3(n1 n2 n3)

(cond ((> n1 n2) (cond ((> n1 n3) n1)
(t n3)))
((> n2 n3) n2)
(t n3)))

Q. Write a LISP program that converts temperature in centigrades to equivalent temperature in Fahrenheit. (June 01, Dec. 01, June 03)

Ans.

(defun convert-centigrades()

(terpri)
(princ "Please enter the value for centrigades: ")
(setq centi (read))
(princ "The temperature in Fahrenheit is: ")
(princ (* (- centi 32) (/ 5 9)))
(terpri))

Q. Write a LISP program to convert number of miles into number of kilometers (you may assume 5 miles = 8 kilometers) (June 02)

Ans.

(defun convert()

(terpri)
(princ "Please enter the value for miles: ")
(setq miles (read))
(princ "The number of kilometers is: ")
(princ (* (/ 8 5) miles))
(terpri))

 
          AI Contents  
©Universal Teacher Publications Web: www.universalteacherpublications.com.