0% found this document useful (0 votes)
39 views15 pages

World Problems

The document describes various programs written in the Lisp programming language. It discusses Lisp's data types and functions for output, arithmetic operations, predicates, list manipulation, defining functions, and using arrays. Examples are provided of programs to print "hello world", perform calculations, manipulate lists, calculate areas and temperatures, find largest numbers, compute factorials and Fibonacci sequences, and create and assign values to arrays.

Uploaded by

Neha Gautam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views15 pages

World Problems

The document describes various programs written in the Lisp programming language. It discusses Lisp's data types and functions for output, arithmetic operations, predicates, list manipulation, defining functions, and using arrays. Examples are provided of programs to print "hello world", perform calculations, manipulate lists, calculate areas and temperatures, find largest numbers, compute factorials and Fibonacci sequences, and create and assign values to arrays.

Uploaded by

Neha Gautam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 15

LISP (LIST PROCESSING) developed by JOHN McCarthy. It is used for solving real world problems. DATA TYPES: 1.

ATOM:- Numerical e.g. 21 and symbolic e.g. a 2.LIST-: (a b c d ) (1 2 3 4 ) 3.STRING:- HELLO

1. Write a program in Lisp to print hello world using different output functions.
Print function: Break 1[1] > ( print hello ) hello hello Princ function: Break 1[1]> ( princ hello) hello hello Prinl function: Break 1[1]> ( prinl hello) hello hello

2. Write a program in Lisp to use all arithmetic operations. Addition function: To add the numbers Break 3[4]> ( + 3 4 ) 7 Break 4[5]> ( + 3 5 10 ) 18 Break 4[5]> ( + (+ (3 5) (10) ) 18 Subtraction function: To subtract the numbers Break 4[5]> ( - 5 8 ) -3 Multiplication function: To multiply the numbers Break 4[5]> ( * 4 5 ) 20 Division function: To divide the numbers Break 4[5]> ( / 4 2 ) 2 Maximum function: Finds the largest number Break 4[5]> ( max 3 6 4 8 7 ) 8 Minimum function: Finds the smallest number Break 4[5]> ( min 3 4 2 8 ) 2

3. Write a program in Lisp to use predicate functions. Atom function: Break 4[5]> ( atom 3 ) T Break 4[5]> ( atom 9 ) T Break 4[5]>( atom ( p ) ) NIL Number function: Break 4[5]> ( numberp 2 ) T Break 4[5]> ( numberp h ) NIL List function: Break 4[5]>( list ( p ) ) T Break 4[5]>( list p ) NIL Symbol function: Break 4[5]>( symbol @ ) T Break 4[5]>( symbol a ) NIL

Zero function: Break 4[5]>( zerop 0 ) T Break 4[5]>( zerop 1 ) NIL Plus function: Break 4[5]>( plusp 2 ) T Break 4[5]>( plusp -2 ) NIL Minus function: Break 4[5]>( minusp -2 ) T Break 4[5]>( minusp 2 ) NIL Even function: Break 4[5]>( evenp 2 ) T Break 4[5]>( evenp 3 ) NIL Odd function: Break 4[5]>( oddp 8 ) NIL

Break 4[5]>( oddp 3 ) T Greater & Lesser function: Break 4[5]> ( > 4 3 2 ) T Break 4[5]> ( > 4 5 2 ) NIL Break 4[5]> ( < 5 6 9 ) T Break 4[5]> ( < 2 3 4 1 ) NIL Length function: To find out the length of given list Break 4[5]> ( length '( a b c d ) ) 4 Float function: Break 4[5]> ( float ( / 22 4)) 5.5 Round function: Break 4[5]> ( round ( / 22 4)) 6 Exponent Function: Break 4[5]> ( expt 2 3)) 5

Square root function: Break 4[5]> (sqrt 9) 3 Equal Function: Returns the given set is equal or not Break 4[5]> (equal '(a b) '( a b) ) T Break 4[5]> (equal '(a b) 1) NIL Break 4[5]> ( eq 1 1 ) T Break 4[5]> ( eq 1 'a ) NIL Break 4[5]> ( eq 'a 'a ) T

4. Write a program in Lisp to manipulate list. CAR: Print the first element of the list. Break 4[5]> ( car ( a b c d ) ) A CDR: Leave the first element and print the remaining elements of the list. Break 4[5]> ( cdr ( a b c d ) ) BCD CONS: Concatenates an atom and a list. Break 4[5]> ( cons a ( a b c d ) ) (AABCD) LIST: Prints the list itself. Break 4[5]> (list ( a b ) ( c d ) ) ((AB)(CD)) APPEND: Concatenates two lists. Break 4[5]> (append ( a b ) ( c d ) ) (A B C D ) FIRST: Print the first element of the list. Break 4[5]> (first ( a b c d ) ) A LAST: Print the last element of the list. Break 4[5]> ( last (a b c d ) ) D

REVERSE: Reverse the order of the elements in the list. Break 4[5]> (reverse ( a f t h ) ) (HTFA) MEMBER: Break 4[5]>( member b ( a s b u k ) ) (BUK) REST: Break 4[5]>( rest ( a b c d ) ) (BCD) SET: Break 4[5]>( setq a 21 ) 21

5. Write a program in lisp to calculate the area of circle.

( defun area ( ) ( print " Enter the radius " ) ( setf rad ( read ) ) ( print " Area of circle " ) ( * 3.14 rad rad ) )

Output: Break 4[5]> AREA Break 4[5]> ( area ) " Enter the radius " 5 " Area of circle " 78.5

6. Write a program to convert temperature from Fahrenheit to Celsius degree

( defun F-C () ( print "Enter the temp in Fahrenheit" ) ( setq fahre ( read ) ) ( princ "converted temp is :" ) ( * ( - fahre 32 )( float ( / 5 9 ) ) ) )

Output: F-C Break 4[5]> (f-c) "Enter the temp in Fahrenheit" 100 converted temp is : 37.777778

7. Write a program in Lisp to find largest number from 3 numbers.

( defun large() ( print enter three numbers ) ( setf a ( read ) b ( read ) c ( read ) ) ( cond ( ( > a b ) ( cond ( ( > a c ) a ) ( t c )) ) ( ( > b c ) b ) (tc)) )

Output:LARGE Break 4[6]> ( large 5 7 3 ) 7

8. Write a program in Lisp to calculate factorial of a given number.

( defun fact ( n ) ( cond (( zerop n ) 1 ) ( + ( * n ( factorial ( - n 1 ) ) ) ) ) ) Output:FACT Break 1 [3]> ( fact 5 ) 120

9. Write a program in Lisp to print Fibonacci series.

(defun fib ( n ) ( if ( or ( zerop n ) ( = n 1 ) ) 1 (+ ( fib ( - n 1 ) ) ( ( fib (- n 2 ) ) ) ) )

Output: FIB Break 4[5]> ( fib 4 ) 5 Break 4[5]> ( fib 1 ) 1

10.Function for table of any given numbers. ( defun table( ) ( princ enter any number ) ( setq a ( read ) ) ( princ table of given number is ) ( loop for i from 1 to 10 do ( print ( * i a ) ) ) )

Output:TABLE Break 1[8]>( Table ) Enter any number 6 Table of given number is 6 12 18 24 30 36 42 48 54 60 NIL

11. Write a program in Lisp to find out user input is a character or numeric value using if function. ( defun num() ( print " enter any value ") ( setf a ( read ) ) ( if ( numberp a ) ( print " number " ) ( print "character" ) ) )

Output:Break 2[1]> NUM Break 2[2]> ( num ) " enter any value " 7 " number " " number "

12. Create a list of items using make array and assign value to each. ( setf af ( make-array '( 5 ) ) ) #(NIL NIL NIL NIL NIL) Break 4 [5]> ( setf ( aref af 0 ) 10 ) 10 Break 4 [5]> ( aref af 0 ) 10 Break 4 [5]> ( setf ( aref af 1 ) 25 ) 25 Break 4 [5]> ( aref af 1 ) 25 Break 4 [5]> ( setf ( aref af 2 ) 50 ) 50 Break 4 [5]> ( aref af 2 ) 50 Break 4 [5]> ( setf ( aref af 3 ) 60 ) 60 Break 4 [5]> ( aref af 3 ) 60 Break 4 [5]> ( setf ( aref af 4 ) 70 ) 70 Break 4 [5]> ( aref af 4 ) 70

You might also like